at—at, batch—execute commands at a later time
at [–csm] [–f script] [–qqueue] time [date] [+ increment] at –l [ job...] at –r job... batch
at and batch read commands from standard input to be executed at a later time. at allows you to specify when the commands should be executed, while jobs queued with batch will execute when system load level permits. Executes commands read from stdin or a file at some later time. Unless redirected, the output is mailed to the user.
Example A.1
1 at 6:30am Dec 12 < program 2 at noon tomorrow < program 3 at 1945 pm August 9 < program 4 at now + 3 hours < program 5 at 8:30am Jan 4 < program 6 at -r 83883555320.a
EXPLANATION
1. At 6:30 in the morning on December 12th, start the job. 2. At noon tomorrow start the job. 3. At 7:45 in the evening on August 9th, start the job. 4. In three hours start the job. 5. At 8:30 in the morning of January 4th, start the job.
6. Removes previously scheduled job 83883555320.a.
awk—pattern scanning and processing language
awk [ –fprogram–file ] [ –Fc ] [ prog ] [ parameters ] [ filename...]
awk scans each input filename for lines that match any of a set of patterns specified in prog.
Example A.2
1 awk '{print $1, $2}' file 2 awk '/John/{print $3, $4}' file 3 awk -F: '{print $3}' /etc/passwd 4 date | awk '{print $6}'
EXPLANATION
1. Prints the first two fields of file where fields are separated by whitespace. 2. Prints fields 3 and 4 if the pattern John is found. 3. Using a colon as the field separator, prints the third field of the /etc/passwd file.
4. Sends the output of the date command to awk and prints the sixth field.
banner—make posters
banner prints its arguments (each up to 10 characters long) in large letters on the standard output.
Example A.3
banner Happy Birthday
EXPLANATION
Displays in banner format the string Happy Birthday.
basename—with a directory name delivers portions of the pathname
basename string [ suffix ] dirname string
basename deletes any prefix ending in / (forward slash) and the suffix (if present in string) from string, and prints the result on the standard output.
Example A.4
1 basename /usr/local/bin 2 scriptname="'basename $0'"
EXPLANATION
1. Strips off the prefix /usr/local/ and displays bin.
2. Assigns just the name of the script, $0, to the variable scriptname
bc—processes precision arithmetic
bc [ –c ] [ –l ] [ filename...] bc is an interactive processor for a language that resembles C but provides unlimited precision arithmetic. It takes input from any files given, then reads the standard input.
Example A.5
1 bc << EOF scale=3 4.5 + 5.6 / 3 EOF Output : 6.366 -----------------------------
2 bc ibase=2 5101 (Output) 20 10100 (Output ^D
EXPLANATION
1. This is a here document. From the first EOF to the last EOF input is given to the bc command. The scale specifies the number of digits to the right of the decimal point. The result of the calculation is displayed on the screen.
2. The number base is two. The number is converted to binary (AT&T only).
bdiff—compares two big files
bdiff compares two files that are too large for diff.
cal—displays a calendar
cal [ [ month ] year ] cal prints a calendar for the specified year. If a month is also specified, a calendar just for that month is printed. If neither is specified, a calendar for the present month is printed.
Example A.6
1 cal 1997 2 cal 5 1978
EXPLANATION
1. Prints the calendar year 1997. 2. Prints the month of May for 1978.
cat—concatenates and displays files
cat [ –bnsuvet ] filename... cat reads each filename in sequence and writes it on the standard output. If no input file is given, or if the argument – is encountered, cat reads from the standard input file.
Example A.7
1 cat /etc/passwd 2 cat -n file1 file2 >> file3
EXPLANATION
1. Displays the contents of the /etc/passwd file. 2. Concatenates file1 and file2 and appends output to file3. The –n switch causes each line to be numbered.
chmod—change the permissions mode of a file
chmod [ –fR ] mode filename... chmod [ugoa ]{ + | – | = }[ rwxlsStTugo] filename...
chmod changes or assigns the mode of a file. The mode of a file specifies its permissions and other attributes. The mode may be absolute or symbolic.
Example A.8
1 chmod +x script.file 2 chmod u+x,g-x file 3 chmod 755 *
EXPLANATION
1. Turns on execute permission for user, group, and others on script.file. 2. Turns on execute permission for user, and removes it from group on file. 3. Turns on read, write, and execute for the user, read and execute for the group, and read and execute for others on all files in the current working directory. The value is octal ( 111 101 101 ).
4. rwxxr-xr-x
chown—changes owner of file
chown [ –fhR ] owner filename ... chown changes the owner of the files to owner. The owner may be either a decimal user ID or a login name found in /etc/passwd file. Only the owner of a file (or the superuser) may change the owner of that file.
Example A.9
1 chown john filex 2 chown -R ellie ellie
EXPLANATION
1. Changes the user ID of filex to john. 2. Recursively changes the ownership to ellie for all files in ellie directory.
- clear—clears the terminal screen
- cmp—compares two files
cmp [ –l ] [ –s ] filename1 filename2 The two files are compared. cmp makes no comment if the files are the same; if they differ, it announces the byte and line numbers at which the first difference occurred.
Example A.10
cmp file.new file.old
EXPLANATION
If the files differ, the character number and the line number are displayed.
compress—compress, uncompress, zcat compress, uncompress files, or display expanded files
compress [ –cfv ] [ –b bits ] [ filename... ] uncompress [ –cv ] [ filename... ] zcat [ filename... ]
compress reduces the size of the named files using adaptive Lempel-Ziv coding. Whenever possible, each file is replaced by one with a .Z extension. The ownership modes, access time, and modification time will stay the same. If no files are specified, the standard input is compressed to the standard output.
Example A.11
1 compress -v book
book:Compression:35.07% -- replaced with book.Z
2 ls book.Z
EXPLANATION
1. Compresses the book into a file called book.Z and displays the percentage that the file was compressed and its new name.
cp—copies files
cp [ –i ] [ –p ] [ –r ] [ filename ... ] target The cp command copies filename to another target, which is either a file or directory. The filename and target cannot have the same name. If the target is not a directory, only one file may be specified before it; if it is a directory, more than one file may be specified. If target does not exist, cp creates a file named target. If target exists and is not a directory, its contents are overwritten. If target is a directory, the file(s) are copied to that directory.
Example A.12
1 cp file1 file2 2 cp chapter1 book 3 cp -r desktop /usr/bin/tester
EXPLANATION
1. Copies the contents of file1 to file2. 2. Copies the contents of chapter1 to the book directory. In the book directory, chapter1 has its original name. 3. Recursively copies the entire desktop directory into /usr/bin/tester.
cpio—copy file archives in and out
cpio –i [ bBcdfkmrsStuvV6 ] [ –C bufsize ] [ –E filename ]
[ –H header ] [ –I filename [ –M message ] ] [ –R id ] [ pattern ... ] cpio –o [ aABcLvV ] [ –C bufsize ] [ –H header ]
[ –O filename [ –M message ] ] cpio –p [ adlLmuvV ] [ –R id ] directory
Copies file archives according to the modifiers given, usually for backup to a tape or directory.
Example A.13
find . -depth -print | cpio -pdmv /home/john/tmp
EXPLANATION
Starting at the current directory, find descends the directory hierarchy, printing each entries of the directory even if the directory does not have write permission, and sends the filenames to cpio to be copied into the john/tmp directory in the /home partition.
cron—the clock daemon
cron executes commands at specified dates and times. Regularly scheduled jobs can be specified in the /etc/crontab file. In order to use cron, one of the following must be true: (1) you are superuser; (2) you are regular user, but your user ID is listed in the /etc/cron.allow file; (3) you are regular user, but your system contains a file /etc/cron.deny, which is empty.
crypt—encodes or decodes a file
crypt [ password ] crypt encrypts and decrypts the contents of a file. The password is a key that selects a type of transformation.
cut—removes selected fields or characters from each line of a file
cut –clist [ filename ... ] cut –flist [ –dc ] [ –s ] [ filename ... ]
The cut command cuts out columns or characters from a line of a file; if no files are given, uses standard input. The –d option specifies the field delimiter. The default delimiter is a tab.
Example A.14
1 cut -d: -f1,3 /etc/passwd 2 cut -d: -f1-5 /etc/passwd 3 cut -c1-3,8-12 /etc/passwd 4 date | cut -c1-3
EXPLANATION
1. Using the colon as a field delimiter, displays fields 1 and 3 of the /etc/passwd file. 2. Using the colon as a field separator, displays fields 1 through 5 of the etc/passwd file. 3. Cuts and displays characters 1 through 3 and 8 through 12 of each line from the /etc/passwd file. 4. Sends the output of the date command as input to cut. The first three characters are printed.
date—displays the date and time or sets the date
[ –u ] [ –a [ – ] sss.fff ] [ yymmddhhmm [ .ss ] ] [+format ]
Without arguments, the date command displays the date and time. If the command line argument starts with a plus sign, the rest of the argument is used to format the output. If a percent sign is used, the next character is a formatting character to extract a particular part of the date, such as just the year or weekday. To set the date. the command line argument is expressed in digits representing the year, month, day, hours, and minutes.
Example A.15
1 date +%T 2 date +20%y 3 date "+It is now %m/%d /%y"
EXPLANATION
1. Displays the time as 20:25:51 2. Displays 2096. 3. Displays It is now 07/25/96.
diff—compares two files for differences diff [–bitw] [–c | –Cn
Compares two files and displays the differences on a line-by-line basis. Also displays commands that you would use with the ed editor to make changes.
Example A.16
diff file1 file2 1c1 < hello there --- > Hello there. 2a3 > I'm fine.
EXPLANATION
Shows how each line of file1 and file2 differs. The first file is represented by the < symbol, and the second file by the > symbol. Each line is preceded by an ed command indicating the editing command that would be used to make the files the same.
du—summarizes disk usage
du [–arskod] [name ...] The du command reports the number of 512-byte blocks contained in all files and (recursively) directories within each directory and file specified.
Example A.17
1 du -s /desktop 2 du -a
EXPLANATION
1. Displays a summary of the block usage for all the files in /desktop and its subdirectories. 2. Displays block usage for each file in this directory and subdirectories.
echo—echoes arguments
echo [ argument ] ... echo [ –n ] [ argument ]
echo writes its arguments separated by blanks and terminated by a newline on the standard output. System V echo options: \b \c backspace suppress newline
\f form feed \n \r \t newline return tab
\v \\ vertical tab backslash
\0n n is a 1, 2, or 3, octal value
egrep—searches a file for a pattern using full regular expressions
egrep [ –bchilnsv ] [ –e special–expression ][ –f filename ] [ strings ] [ filename ... ]
egrep (expression grep) searches files for a pattern of characters and prints all lines that contain that pattern. egrep uses full regular expressions (expressions with string values that use the full set of alphanumeric and special characters) to match the patterns.
Example A.18
1 egrep 'Tom|John' datafile 2 egrep '^ [A-Z]+' file
EXPLANATION
1. Display all lines in datafile containing the pattern either Tom or John.
2. Display al lines starting with one or more uppercase letters.
expr—evaluates arguments as an expression
expr arguments The arguments are taken as an expression. After evaluation, the result is written to the standard output. The terms of the expression must be separated by blanks. Characters special to the shell must be escaped. Used in Bourne shell scripts for performing simple arithmetic operations.
Example A.19
1 expr 5 + 4 2 expr 5 \* 3 3 num=0 num='expr $num + 1'
EXPLANATION
1. Prints the sum of 5 + 4
2. Prints of result of 5 * 3. The asterisk is protected from shell expansion.
3. After assigning 0 to variable num, the expr command adds 1 to num and result is assigned to num.
fgrep—search a file for a character string
fgrep [ –bchilnsvx ] [ –e special string ] [ –f filename ] [ strings ] [ filename ... ]
fgrep (fast grep) searches files for a character string and prints all lines that contain that string. fgrep is different from grep and egrep because it interprets regular expression metacharacters as literals.
Example A.20
1 fgrep '***' * 2 fgrep '[ ] * ? $' filex
EXPLANATION
1. Displays any line containing three asterisks from each file in the present directory. All characters are treated as themselves; i.e., metacharacters are not special.
2. Displays any lines in filex containing the string enclosed in quotes.
file—determines the type of a file by looking at its contents
file [[ –f ffile ] [ –cl ] [ –m mfile ] filename... file performs a series of tests on each filename in an attempt to determine what it contains. If the contents of the file appear to be ASCII text, file examines the first 512 bytes and tries to guess its language.
Example A.21
1 file bin/ls
/bin/ls:sparc pure dynamically linked executable
2 file go
go: executable shell script
3 file junk
junk: English text
EXPLANATION
1. ls is a binary file dynamically linked when executed.
2. go is an executable shell script. 3. junk is a file containing ASCII text.
find—finds files
find path–name–list expression find recursively descends the directory hierarchy for each pathname in the pathname list (i.e., one or more pathnames) seeking files that match options. First argument is the path where the search starts. The rest of the arguments specify some criteria by which to find the files, such as name, size, owner, permissions, etc. Check the UNIX manual pages for different syntax.
Example A.22
1 find . -name \*.c -print 2 find .. -type f -print 3 find . -type d -print 4 find / -size 0 - exec rm "{}" \; 5 find ~ -perm 644 -print 6 find . -type f -size +500c -atime +21 -ok rm -f "{}" \; 7 find . -name core -print 2> /dev/null (Bourne and Korn Shells)
( find . -name core -print > /dev/tty ) >& /dev/null ( C shell)
8 find / -user ellie xdev -print 9 find ~ -atime +31 -exec mv {} /old/{} \; -print
EXPLANATION
1. Starting at the present working directory (dot), finds all files ending in .c and prints the full pathname of the file. 2. Starting at the parent directory (dot dot), finds all files of type file; i.e., files that are not directories. 3. Starting at the present directory (dot), finds all directory files. 4. Starting at the root directory, finds all files of size zero and removes them. The {} are used as a place holder for the name of each file as it is found.
5. Starting at the user's home directory ~ (Korn and C shells), finds all files that have permissions 644 (read and write for the owner, and read permission for the group and others).
6. Starting at the present working directory, finds files that are over 500 bytes and have not been accessed in the last 21 days and asks if it is okay to remove them.
7. Starting at the present working directory, finds and displays all files named core and sends errors to /dev/null, the UNIX bit bucket.
8. Prints all files on the root partition that belong to user ellie.
9. Moves files that are older than 31 days into a directory, /old, and prints the files as it moves them.
finger—displays information about local and remote users
finger [ –bfhilmpqsw ] [ username... ] finger [–l] username@hostname...
By default, the finger command displays information about each logged-in user, including login name, full name, terminal name (prepended with a * if write permission is denied), idle time, login time, and location if known.