Main Body
2 Common Unix Commands
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.
Right:
ls /etc
Wrong:
ls/etc
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.
Command |
What does it do? |
Example usage |
---|---|---|
ls |
list directory contents | List files (and directories) in my current directory
ls List files in top level etc directory ls /etc List all files beginning with g in the current directory. (The * is a wildcard.) ls g* List and give details about files in the current directory ls -l List all files including hidden files (those having a name beginning with a period character) in the current directory ls -a
|
cat |
concatenate files and print on the standard output | show contents of file report (in current directory)
cat report show contents of files chapter1 and chapter2 cat chapter1 chapter2
|
head |
output the first part of files | show the first few lines of file longfile
head longfile
|
tail |
output the last part of files | show the last few lines of file logfile
tail logfile
|
cp |
copy files and directories | copy file report to report_v2 in the work directory
cp report work/report_v2
|
mv |
move (rename) files | rename file report to presentation
mv report presentation move file mydata to yourdata one level up mv mydata ../yourdata
|
rm |
remove files (permanent) | delete file report in current directory
rm report Caution: There is no recycling bin in Unix. You should consider all deletions as permanent. Exercise care when using wildcards (e.g. *). |
cd |
change the working directory | change directory to work directory (one level down)
cd work change directory to /bin directory cd /bin change directory to parent (one level up) cd .. change directory to home directory of user ahmed cd ~ahmed
|
pwd |
print name of current/working directory | Are you lost?
pwd
|
mkdir |
make directories | create new directory called unix_exercises in the current directory
mkdir unix_exercises
|
rmdir |
remove empty directories | remove (permanently delete) the directory unix_exercises from current directory
rmdir unix_exercises
|
who |
show who is logged on | display who else is logged in right now
who
|
whoami |
print effective userid | display one’s one userid (short login name)
whoami
|
date |
print the system date and time |
date |
man |
an interface to the on-line reference manuals | display the manual entry for the ls command
man ls Not sure of what command to look for? Try a keyword search. Display all commands pertaining to “directory” man -k directory
|
Key Takeaways
- Spaces are a BIG DEAL in Unix: They are needed between commands, parameters, and filenames.
- All commands are case-sensitive (usually all lowercase)
More Key Takeaways
- 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 -ld
-
ls -l -d
-
More Unix Commands
Command |
What does it do? |
Example usage |
---|---|---|
cut |
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
|
paste |
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
|
wc |
print newline, word, and byte counts for each file | display the number of lines, words, and characters for the file chapter3
wc chapter3 display only the number of lines wc -l chapter3
|
grep |
print lines matching a pattern | display lines matching the string Total in the file sales
grep Total sales
|
sort |
sort lines of text files | display a sorted version of the file namelist
sort namelist
|