! n cd dir mv file dir Command Reference Repeat recent command n Change to directory dir Move file to directory dir Frequently used Commands Ctrl/p (previous) cd mv dir1 dir2 Move up in history list Return to home directory If directory dir2 exists, move dir1 into dir2; otherwise, rename dir1 as dir2 Ctrl/n (next) pwd Words in monospace type are commands and Move down in history list Display working directory mv file1 file2 should be typed as they are printed Rename file1 as file2 Ctrl/b (backward) file file Words in bold type should be substituted with Cursor left to edit command Determine file type #!/bin/sh the appropriate or directory for i in * Ctrl/f (forward) du -ks *| -nr|more do Unix is case-sensitive — UPPER and lowercase Cursor right to edit command Show all directory sizes in order, largest first $i letters have different meanings mv $i `basename $i`.ext Ctrl/d (delete) done Delete character in command List the contents of the current directory Rename a number of files General clear ls dir file Clear terminal screen List the contents of the directory dir Remove file man command Display the Unix manual entry describing a lock ls -l rm -f file given command Lock terminal Show permissions, owner, size, and other Force, remove files without prompting file info apropos command reset rm -r file Locate commands by keyword Reset / initialize terminal ls -a Remove files, directories, and recursively, Show all files, including (hidden) files that begin any subdirectories alias name1 name2 set with a dot Create command alias name Show environment rmdir dir ls -R Remove empty directory dir alias name env Show files recursively, for all subdirectories Display command alias Show current settings vi file ls -d Vi fullscreen editor unalias name sentenv name v List directories like other files, without Remove command alias name Set env var to value v (csh/tcsh) displaying their contents emacs file Emacs fullscreen editor passwd export name="v" ls -k Change password Set environment variable to value v List file sizes in kilobytes pico file (ksh/) Pico text editor quota ls -X Display amount of disk space used exit Sort files by file extension wc file Terminate current session Count lines, words, & chars df ls -1 Show available system disk space Display the listing in 1 column cat file Make a typescript of everything printed on List contents of file du the terminal ls -t Show disk space being used up by folders Show files in order, newest to oldest more file sudo /usr/libexec/locate.updatedb Display contents of a file one screen at a bc Update the locate database ls -l | "^d" time Basic calculator List all directories in the current directory without any of the files file bc obase=16 255 File System Navigation Opposite of more Displays FF ls -l | grep ^d | wc -l * Find the number of subdirectories in the head -n file bc ibase=16 obase=10 Wild card: match zero or more characters current directory Display first n lines of file Hex to Dec ? ls -ls|sort -nr|more tail -n file date List files by size, largest first Display last n lines of file Display date & time Wild card: match zero or one character . cmp file1 file2 cal month year Data Manipulation Compare two files Show calendar Shorthand for the current directory mkdir dir diff file1 file2 whoami .. Create new directory dir Show file differences Display current user Shorthand for the parent of the current directory cp file1 file2 cp file1 file2 history Copy file(s) Copy file file1 into file2 Display recent commands ~ Home directory cp file dir sort file !! Copy file(s) into a directory Display the lines of text file alphabetically Repeat last shell command ~username Home directory of user username cp -r dir1 dir2 sort -r file !string Sort in reverse order Repeat last shell command that began Copy a directory and, recursively, its subdi- with string rectories sort -n file cmd1 || cmd2 File Compression ssh host Sort numerically (2 before 10) cmd2 is executed only if the execution of rsh host cmd1 does not end up successfully Log into and execute commands on a sort +n file file remote machine Sort on n+1st field cmd1 ; cmd2 Reduce the size of a file Execute cmd2 after execution of cmd1 stop- lpr -P printer file cat file1 file2 > file3 ped uncompress file Output file to line printer Concatenate file1 & file2 into file3 Restore a compressed file nohup command < file.in >> file.out& mail user split [-n] file ’No hangup’: execution of command will tar cf - /home/file | compress > Send mail to user Split file into n-line pieces continue even if the user logs off the file.tar.Z system (exit). Run command in the bak- tar and compress a file biff y/n grep sample file kground (&), taking input from file.in and Instant notification of mail Output lines that match sample string or appending output to file.out. tar cf - /home/file | gzip > pattern file.tar.Z tar and gzip a file Control grep -i Permissions Case-insensitive search ls -al | '$0!~/^d/ {print $9}' | tar cvf archive_name.tar sleep n -rwxr-xr-x Sleep for n seconds grep -n Archive only regular files in a directory, Directories have a d in the first column; omitting subdirectories and hidden files Show the line # along with the matched regular files have a –. jobs line The remaining 9 characters indicate the Display list of jobs owner, group, and world permissions of the grep -v Make an index file of the contents file. of the tar file Ctrl/c Invert match: find all lines that do not An r indicates that the file is readable; w is Interrupt process / stop execution of a match writable, and x is executable. tar cvf - /home/file command A dash in the column instead of a letter grep -w 2>file.idx | compress > file.tar.Z means that particular permission is turned For sh, ksh Ctrl/d Match entire words, rather than substrings off. End of typed input (End of File Key) t is the ’sticky bit’ for directories; pre- (tar cvf - /home/file | compress > touch file vents files from being deleted by anyone Ctrl/q Update the timestamp on a file, if the file /file.tar.Z) >&file.idx other than the owner. For csh Start / resume terminal output doesn’t exist, touch creates an empty file s is the ’setuid-bit’ for files; execute a pro- gram using the owner’s permissions (rather Ctrl/s I/O than those of the one who calls it). A simple backup script Stop terminal output sh: Ctrl/z The shell expects input from; and sends out- Setting Permissions with Letters % pico ~/bin/backup.sh Suspend execution of a command put to, a terminal. To write command output to files or read input from files, redirection is chmod u+rwx,go+rx file #!/bin/sh used. UNIX defines three I/O units with corre- u is the user’s (owner) permissions; g is the echo "Backup of Folder:" Show process status statistics sponding file descriptors: group permissions, and o is world (other) tar cvf - /home/file 2>file.idx | permissions. gzip > home/file.tar.Z ps aux 0: stdin (standard input) The + sign turns the stated permissions on; Show complete process listing 1: stdout (standard output) a – sign turns them off. Save the script in ~/bin 2: stderr (standard error) Directories should always have, at least for top the owner, the x permission set. % chmod +x ~/bin/backup.sh Show system usage statistics dynamically; prog > file A directory doesn’t have to be readable for Make it executable stop with q Redirect (write) stdout of prog to file the web server to read and execute files within that directory. Only the files them- % rehash kill -9 n prog >> file selves must be readable. Force the shell to rebuild its list of known Remove process n Append stdout of prog to file executables stop %n prog < file Numeric Permissions Suspend background job n Read stdin for prog from file chmod 711 file Networking & Communications command& prog < file1 > file2 Change permissions on a file. Run command in background Read stdin for prog from file1, redirect The first number translates to permissions who stdout to file2 by the owner (logon account). The second List logged in users bg %n is permissions for the group (a possibly Resume background job n prog 2>file empty group of logon accounts). The third finger user Write stderr of prog to file is permissions for everyone. Display user information fg %n Resume foreground job n prog 2>&1 0: --- (no permissions) chfn With : write stderr of prog 1: --x (executable only) Change finger information to stdout 2: -w- (writable only) 3: -wx (writable and executable) ping host cmd1 | cmd2 4: r--- (readable only) Send ICMP ECHO_REQUEST packets to net- : use cmd1’s output as input for cmd2 5: r-x (readable and executable) work hosts 6: rw- (readable and writable) cmd1 && cmd2 7: rwx (readable, writable, and executable) telnet hostname cmd2 is executed only if the execution of Connect to another remote system using Maintained by Alexander Becker the telnet protocol Published by kokhaviv press cmd1 ends up successfully www.kokhavivpublications.com/help/unix/ print 0.92 / 2004-10-19