Go to: LING2050 home page   Lab pages index

Unix Command Reference Sheet

Finding your way around

pwd display current directory
echo $HOME display your home directory (likely the directory your terminal starts in)
cd dir change current directory to dir
cd .. change current directory to the parent directory
cd c: [cygwin only] move into C:
cd /cygdrive/c [cygwin only] move into C:
cd change current directory to your home (default) directory
cd ~ same as above ('~' refers to home directory)
cd ~/dir move into dir directory under your home directory
ls list the files and directories in the current directory
ls . same as above ('.' refers to current directory)
ls dir list the files and directories in dir
ls file list the name of file
ls *.txt list all files whose name matches '*.txt'
ls -l ... and some information on each file
ls -a list hidden files (name starts with a ".", as in .bashrc) as well
ls -F indicate file attributes (directory with /, executable with *)
ls / list the content of your root directory (cygwin: C:/cygwin, OS-X: system disc root)
clear clear the terminal screen
man command print manual page for the unix command named 'command'. SPACE to forward, q to quit
Ctrl+c cancel executing current operation and return to command prompt (press 'c' key while holding down 'Ctrl' key)
TAB
PageUp/PageDown

File/directory, input/output handling

touch file create a file named file in current directory
mkdir dir create a directory named dir in current directory
rm file(s)delete file(s)
rm -i file(s)prompt for confirmation before deleting file(s)
rmdir dirremove directory (only works with empty directories)
rm -r dirremove recursively ('-r'); remove the directory and all files and directories in it
cp file1 file2copy file1 to file2, in current directory
cp dir/file .copy 'file' in 'dir' to current directory ('.'). File name stays the same as 'file'.
cp file dircopy 'file' in current directory to the directory 'dir'. File name stays the same as 'file'.
cp file1 file2 file3 dircopy the files in current directory into the directory 'dir'
mv file dirmove 'file' in current directory to another directory named 'dir'
mv file1 file2rename 'file1' as 'file2' (*Warning: if there's a file named 'file2' in the directory already, it will be overwritten!)
mv dir1/file dir2move 'file' in directory 'dir1' to another directory named 'dir2'
mv dir/file .move 'file' in 'dir' to current directory ('.')
mv file1 file2 file3 dirmove the files in current directory into the directory 'dir'
mv dir1 dir2move directory 'dir1' into another directory named 'dir2'
wc file(s) print # of lines, words, and characters in file(s), in that order
wc -l print # of lines only
wc -w print # of words only
diff file1 file2 print differences between two text files
> filedirect STDOUT (Standard Output) into a new file named 'file'
>> fileappend STDOUT at the end of existing 'file'
&> filedirect STDERR (Standard Error) into a new file named 'file'
| commandpipe STDOUT from the preceding command into the next command as STDIN (Standard Input)
< fileread text content of file as STDIN (Standard Input) and feed it into the preceding command
command & run command in the background and return immediately to your command prompt
echo "text..."print 'text...' to STDOUT (i.e., terminal window)

Printing file content

cat file concatenate (=print) file content to Standard Output (i.e., terminal window)
cat file1 file2 ... concatenate contents of the files to Standard Output (i.e., terminal window)
more file print file, one screenful at a time (SPACE to forward, q to get out)
less file print file, one screenful at a time (SPACE/PageUp to forward, b/PageDown to go back, q to get out)
head file print first 10 lines of file
head -m file print first m lines of file
tail file print last 10 lines of file
tail -m file print last m lines of file
tail -n +m file print file starting from line m
*NOTE: the old syntax tail +m is no longer supported in newer versions of tail.
Printing the 6th line and on, old syntax: tail +6; new syntax: tail -n +6.

Text searching

grep pattern file(s) prints out all lines in file(s) that match pattern
grep -i pattern file(s) does the same, but ignores case (so 'the' and 'The' are both matched)
grep -w restricts the search to whole words only
grep -n precedes each line with the line number
grep -h stops preceding each line with the file name (searching multiple files)
grep -l displays a list of files that contain the string (actual lines are not shown)
grep -v prints the lines that do NOT match pattern
grep --color prints the matched portion in color (extremely handy!)
grep -iw --color pattern file(s) '-' options can be strung together; '--' options cannot
grep "word1 word2" file(s) pattern must be in quotes if it contains space
grep -C n prints out n lines before and after each matching line
grep -iw -C n --color pattern file(s) only simple '-' options can be strung together

Text processing

tr 'char1' 'char2' < filereplace character 1 with character 2 in file text, and print to STDOUT
tr 'abcdef' 'opqrst'
tr '[A-Z]' '[a-z]'
sed 's/string1/string2/' file Prints out lines of file while substituting ('s') string1 with string2 once per line
sed 's/string1/string2/g' file Same as above, but string replacement is done globally ('g') throughout line
sed -r 's/string1/string2/g' file Same as above, but strings contain (extended) regular expression ('-r')
sed 's/.../.../g; s/.../.../g' file Separate multiple transformations with ';'
cut
paste
uniq
uniq -c
sort
sort -n
sort -r
sort -k m,n