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 (wildcard *):
ls g*

List and give details about files:
ls -l

List all files including hidden:
ls -a

cat concatenate files and print on the standard output Show contents of file report:
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. 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:
mkdir unix_exercises
rmdir remove empty directories Remove (permanently delete) the directory unix_exercises:
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 own userid (short login name):
whoami
date print the system date and time date
man an interface to the on-line reference manuals Display manual entry for ls command:
man ls

Keyword search for commands related 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

All the commands below require some sort of input, typically a file, but the commands do not modify the input file. The outputs will contain portions of the input files, but the input files are never changed.

 

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

 

 

 

License

Icon for the Creative Commons Attribution-NonCommercial 4.0 International License

Productivity in Common Operating Systems Copyright © 2022 by Lester Hiraki is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License, except where otherwise noted.

Share This Book