While there are hundreds of Unix commands, fortunately it is not necessary to know all of them. In fact, one can achieve a significant level of productivity knowing just a couple of dozen. Here are some of the most common and useful commands.
All commands are case sensitive.
Spaces are a BIG DEAL in Unix
When issuing commands, in order for Unix to tell when a command finishes and when parameters and file names start and end, every item (or token) on a command line must be separated by whitespace (one or more space characters). One of the most common causes of frustration is failure to put whitepace between items on the command line, or putting whitespace where it should not be.
Just as in English, there is a big difference in meaning between “no table” and “notable”, so is the case in Unix.
Unix commands are a single word requesting an action. Sometimes the action is standalone, but most actions are applied to some object like a file or directory. At the point in the command where it expects the name of a file, say, this is where you specify the file in the form of an absolute or relative reference as described in the previous chapter.
What does it do?
|list directory contents
|List files (and directories) in my current directory
List files in top level etc directory
List all files beginning with g in the current directory. (The * is a wildcard.)
List and give details about files in the current directory
List all files including hidden files (those having a name beginning with a period character) in the current directory
|concatenate files and print on the standard output
|show contents of file report (in current directory)
show contents of files chapter1 and chapter2
cat chapter1 chapter2
|output the first part of files
|show the first few lines of file longfile
|output the last part of files
|show the last few lines of file logfile
|copy files and directories
|copy file report to report_v2 in the work directory
cp report work/report_v2
|move (rename) files
|rename file report to presentation
mv report presentation
move file mydata to yourdata one level up
mv mydata ../yourdata
|remove files (permanent)
|delete file report in current directory
Caution: There is no recycling bin in Unix. You should consider all deletions as permanent. Exercise care when using wildcards (e.g. *).
|change the working directory
|change directory to work directory (one level down)
change directory to /bin directory
change directory to parent (one level up)
change directory to home directory of user ahmed
|print name of current/working directory
|Are you lost?
|create new directory called unix_exercises in the current directory
|remove empty directories
|remove (permanently delete) the directory unix_exercises from current directory
|show who is logged on
|display who else is logged in right now
|print effective userid
|display one’s one userid (short login name)
|print the system date and time
|an interface to the on-line reference manuals
|display the manual entry for the ls command
Not sure of what command to look for? Try a keyword search. Display all commands pertaining to “directory”
man -k directory
- Spaces are a BIG DEAL in Unix: They are needed between commands, parameters, and filenames.
- All commands are case-sensitive (usually all lowercase)
- Command options (e.g. -l, or -d, etc.) are specific to the command. For example, while both the ls and and cp command both have a “-l” option, the option means different things in each command. Command options are not mix and match.
- Command options may be listed separately or combined. The following are equivalent:
ls -l -d
More Unix Commands
What does it do?
|Print selected parts of lines from each FILE to standard output.
|display columns 1-10 and 20-23 of myfile
cut -c1-10,20-23 myfile
display the 3rd and 5th fields of the /etc/passwd file
cut -f3,5 -d: /etc/passwd
|merge lines of files
If cat joins vertically, think of paste as a horizontal version of cat.
|display file1, file2, and file3 side-by-side
paste file1 file2 file3
|print newline, word, and byte counts for each file
|display the number of lines, words, and characters for the file chapter3
display only the number of lines
wc -l chapter3
|print lines matching a pattern
|display lines matching the string Total in the file sales
grep Total sales
|sort lines of text files
|display a sorted version of the file namelist