Linux commands with useful options
Ever find yourself thinking the following:
"What was that -X option that I need to use with this command?"
"What date format do I have to use with this comamnd?"
This file contains a list of those hard to remember -x command line
options -- when the man page contains too much info to look at.
I just want a quick reference I can keep open in my editor and
lookup those options which escape my memory.
Commands featured here:
touch
man
ps
ls
find
lsof - list who/what has open files
make
mpage - format multiple pages per sheet
tar
httpd
a2ps
cvs
netstat
cat
rpm2cpio
nc, curl - get HTTP pages
tcpdump - packet sniffer
# Printing to postscript printer on Windows machine
mpage -1 -c -f > file.ps
ftpmuld hp2100s
put file.ps
# Change a file's date
touch --date='Oct 23, 1999'
# use -m to change just the modification time
# man manual page information
-a display all manual pages for a topic
-K search for string in all man pages -- slow
-w list locations of man pages that would match
List processes not part of normal operation
ps auxw | grep -Ev 'init|kswapd|kflushd|kupdate|syslogd|klogd' | more
# List all (important) files and their sizes
ls -FABRQv1
-F Show file type character /-dir @-link *-exe |-FIFO =-socket
-a List all files including those beginning with .
-A List all files except . and ..
-B Ignore backup files i.e. *~
-R Recurse into subdirectories
-Q Place filenames in Quotes and escape non-printing characters
-v Sort by filename keeping version suffixes ordered
-1 Display only one column of files
Other useful additions
-l Long format print all information about a file
-d List just directory names, no files
-I PATTERN Do not list files matching shell pattern specified
-s Print disk allocation for file
-G Don't show group information
-X sort by extension
--block-size=1 Display file sizes in bytes
-k Show file sizes in Kb
# General purpose ls
ls -RalF --block-size=1
# add -S to sort by file size
# Sorting options for ls
ls -cfrtuvSUX --sort=word
U - unsorted
r - reverse
S - size
X - by extension
v - by version
t - modification time
c - change time
u - access time
f - unsorted
# Good for seeing how much space files take up
ls --block-size=1 -1sSR
# List all files (important) with their sizes and modification time
find . -not -name '*~' -printf "%13s %TYy%Tmm%Tdd %Th %Ta %TH:%TM:%TS %p\n"
%13s Size of file in field width 13
Modification Date/Time
%TY Year
%Tm Month
%Td Day
%Th Short Month Name
%Ta Short Weekday Name
%TH Hour 00-23
%TM Minute
%TS Seconds
%p Full file name and path
Other useful formatting options
%m File permissions in octal
%g File group name
%u File user name
Other useful options
-maxdepth 1 Prevents searching subdirectories
-depth Process directory contents before directory
# Display a nice directory listing which is somewhat DOSish
find . -not -name "*~" \
\( -type f \
\( \
-perm -ug=x -printf "%13s %TYy%Tmm%Tdd %Th %Ta %TH:%TM:%TS \"%p\"*\n" , \
-not -perm -ug=x -printf "%13s %TYy%Tmm%Tdd %Th %Ta %TH:%TM:%TS \"%p\"\n" \
\) , \
-type l -printf "%13s %TYy%Tmm%Tdd %Th %Ta %TH:%TM:%TS \"%p\"@\n" , \
-type p -printf "%13s %TYy%Tmm%Tdd %Th %Ta %TH:%TM:%TS \"%p\"|\n" , \
-type s -printf "%13s %TYy%Tmm%Tdd %Th %Ta %TH:%TM:%TS \"%p\"=\n" , \
-type d -printf " %TYy%Tmm%Tdd %Th %Ta %TH:%TM:%TS \"%p\"/\n" \
\) \
| more
# Display all time values for a file
echo " last modified last status changed last access"
find . -maxdepth 1 -printf '%TYy%Tmm%Tdd %TH:%TM:%TS | %CYy%Cmm%Cdd %CH:%CM:%CS | %AYy%Amm%Add %AH:%AM:%AS | "%p"\n'
# Find files modified today, before today,
find . -mtime -1 -daystart
find . -mtime +1 -daystart
# Grep for something in all the .c or .h files below in current sub-tree
find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
-print0 and -0 causes it to work with filenames that have spaces in it
# Find all files except the ones in ./src/emacs
find . -path './src/emacs' -prune -o -print
# Copy a directory tree from one machine to another efficiently
find . -depth -print0 | cpio -0o -Hnewc |
rsh OTHER-MACHINE "cd `pwd` && cpio -i0dum"
# Use TAR to archive a directory tree
find . -depth -print0 |
tar --create --null --files-from=- --file=/dev/nrst0
# And to extract:
tar --extract --null --preserve-perm --same-owner \
--file=/dev/nrst0
# Clean up your clutter in /tmp - put it in your logout script
find /tmp -user $LOGNAME -type f -print0 | xargs -0 -r rm -f
# Remove backup files created by emacs and others
find ~ \( -name '*~' -o -name '#*#' \) -print0 |
xargs --no-run-if-empty --null rm -vf
# Removing old files from `/tmp' is commonly done from `cron':
find /tmp /var/tmp -not -type d -mtime +3 -print0 |
xargs --null --no-run-if-empty rm -f
find /tmp /var/tmp -depth -mindepth 1 -type d -empty -print0 |
xargs --null --no-run-if-empty rmdir
The second `find' command above uses `-depth' so it cleans out empty
directories depth-first, hoping that the parents become empty and can
be removed too. It uses `-mindepth' to avoid removing `/tmp' itself if
it becomes totally empty.
# Ensure all directories in a directory tree are group writable
find . -type d -not -perm -ug=w | xargs chmod ug+w
# Protect spaces in a file name
find . -exec something `{}` \;
# If you want to classify a set of files into several groups based on
# different criteria, you can use the comma operator to perform multiple
# independent tests on the files. Here is an example:
find / -type d \( -perm -o=w -fprint allwrite , \
-perm -o=x -fprint allexec \)
echo "Directories that can be written to by everyone:"
cat allwrite
echo ""
echo "Directories with search permissions for everyone:"
cat allexec
`find' has only to make one scan through the directory tree (which
is one of the most time consuming parts of its work).
# lsof command to list who's got a file open
lsof -v -- version
lsof -c file
lsof +d dir
lsof +D dir -- recursive
lsof -p proceessID
lsof -r time -- redisplay continuously
lsof +r time -- redisplay until no files open
lsof -u username,...
lsof -- file file file
# plus a whole lot more options
# make command for building projects
make -f file
-d print debug information
-n print commands but don't do anything
-q question - check if project is up to date but don't build
-w prints working directory before and after processing a command
# apache httpd
httpd -d /path/ starts the web server with a particular ServerRoot Directory
httpd -v shows version number
httpd -V shows compile settings
httpd -l shows which modules were compiled in
httpd -t runs syntax test on config files
httpd -X runs a single copy - useful for debugging
# lpr for printing documents
lpr -P printer specifies which printer to print to
# a2ps for pretty postscript printing
a2ps -P display filename -- will preview output
a2ps -4 will print 4 virtual pages on a physical page.
a2ps --line-numbers=5 prints a line number every 5 lines
a2ps --underlay=CONFIDENTIAL use string CONFIDENTIAL as a background underlay
a2ps --chars-per-line=132 scale the font so 132 columns fit on each page
a2ps --list=media will list all configured media
a2ps --list=defaults will list the default settings from /etc/a2ps.cfg
a2ps --guess file will display it's guess as to the file type to format as
a2ps --glob "*.pro" display full path of .pro files that a2ps uses for formatting
a2ps --list=features lists paper styles, formatting types, etc available to a2ps
a2ps --strip-level=3 no comments printed
a2ps --strip-level=1 regular comments not printed
a2ps --output=- send output to standard output
# cvs Version Control
cvs --help command display help about a cvs command
cvs checkout -c display the modules file
cvs checkout -s display module state (Exp Stable etc) as recorded in the modules file
cvs checkout -d dir module checkout the module into a differently named directory dir
cvs export -P checkout without the CVS admin directories being created
useful for offsite distribution
cvs release -d module release a checked out module and delete the directory
cvs update -A get head revision, clearing sticky tags
cvs update -d create any directories that are in repository but not in working directory
cvs update -kk file prevents keyword expansion in working file useful for diff
cvs admin -ko file change the default keyword substitution for a file
cvs update -rBASE file gets the original revision of the file
cvs update -rHEAD file gets the head revision of the file
cvs update -p -r1.1 file prints out revision 1.1 of file.
cvs update -p -r1.1 file > file.1.1 checkout previous revision, non-sticky
cvs history -T report all tags
cvs history -c report history of all commits
cvs history -m module report actions on a module
cvs history -o report modules which are checked out
cvs history -x C report of merges resulting in conflict
cvs history -x R report of removed files
cvs history -x A report of when files added
cvs history -x M report of files modified
cvs history -u name report of history for user name
ident filename displays $Id$ and other CVS keywords for file named
cvs rdiff -s -rtag display a summary change report of from revision 'tag' to current
cvs log -h file less information than the full file log
cvs -n -q update prints how an update would happen but doesn't do it
good for finding new files, conflicts, etc
cvs diff -u | less view all changes so you can write a change log entry
cvs diff -D "1 hour ago" file do a diff with current revision and the revision that is one hour old
cvs rdiff -t module/file diff the top two revisions i.e. see the last change made
cvs commit -F file.log files commits a file reading message from file.log
cvs commit -r5.0 change revision number of all files to 5.0
cvs update -A (will then clear the sticky 5.0 tag)
cvs tag -c tag_name check for modified files before tagging - won't tag if any
cvs rtag -r old_name new_name module rename a tag from old_name to new_name for module
cvs rtag -d old_name module delete a tag (be careful)
cvs tag -r1.6 -Fstable filename move an existing tag to a new revision
i.e. always assign the 'stable' tag to the most stable revision
cvs remove -f *.c removes files and removes from CVS
rm *.c; cvs remove another way to remove a number of files
rpm2cpio - for extracting/listing single files from RPM
rpm2cpio logrotate-1.0-1.i386.rpm | cpio -t - lists the files in an RPM
rpm2cpio logrotate-1.0-1.i386.rpm | cpio -ivd usr/man/man8/logrotate.8 - extracts a single file from the rpm
In this case, the cpio options -i, -v, and -d direct cpio to:
Extract one or more files from an archive.
Display the names of any files processed, along with the size
of the archive file, in 512-byte blocks.1
Create any directories that precede the filename specified in
the cpio command.
So where did the file end up? The last option (-d) to cpio
causes the entire parent directory tree to be created if it
doesn't exist for the files as they are being extracted.
Using cat to view special non-printing characters (Windows Newlines)
cat -vet file
perl -pi -e 's/\cM//g' file ... remove dos/windows Ctrl-M from file - in place edit
2
Network debugging
netstat -atuv lists which services are listening on ports
netstat -atuvp also lists which program is listening on the port (if root)
Rename files (bash)
for d; do mv $d `basename $d .pgm`.jpg; done
# lots of typing and possibly wrong if file name has pgm in it
for file in *.pgm; do mv $file `ls $file | sed -e s/\.pgm/\.jpg/`; done
(tcsh - faster for lots of files)
foreach d (*.pgm)
mv $d $d:gr.jpg
end
Rename a bunch of files after grepping through a previously stored file listing
grep something files.lst | xargs --replace mv {} {}.old
Read standard input into a variable
pwd | read path
Get HTTP pages
printf "GET /index.html HTTP/1.0\r\nUser-Agent: NetCat\r\n\r\n" | nc www.milinx.com 80
OR
nc www.milinux.com 80 < nowhere.html
OR telnet www.host.com 80
GET /index.html HTTP/1.0
OR telnet www.host.com 80
GET /index.html HTTP/1.1
Host: www.host.com
HTTP packet sniffer
tcpdump -w file.dump port 80
will dump the raw HTTP packets to file.dump. Then you can look at what
got received with a program such as Ethereal (check freshmeat.net).
Ethereal can also do packet-dumping on its own. Dug Song wrote something
called the dsniff suite that can do a whole lot of nifty stuff, but I
couldn't get it to compile... Remember these programs are for diagnostic
and educational purposes only. h4x0ring can get you in a lot of trouble,
and invading peoples' privacy is Not Nice.
               (
geocities.com/gurucoder/Tools)                   (
geocities.com/gurucoder)