Basic Unix Commands

pwd

  • This command name stands for "print working directory".

  • It displays on the terminal the absolute path name of the current (working) directory.

  • It is useful to locate where you are currently in the Unix tree.

# print working directory
pwd

cd

  • This command name stands for "change directory".

  • It changes your current working directory to the specified location.

  • Your home directory is referred to as "~" (tilde).

  • The current directory is referred to with a single dot ( ".").

  • The directory located one level up the current directory (also known as "parent directory") is referred to with two dots ("..").

  • The last visited directory is referred to with an hyphen ("-").

# go to root directory of the system and print the working directory
cd /
pwd
# go to the home directory and print the working directory
cd ~
pwd
# change directory using the absolute path and print the working directory
cd /net/bmc-pub14/data/
pwd

ls

  • This command stands for "list".

  • It displays the content of a directory.

  • By default, the content is listed in lexicographic order.

# list the content of the current directory
ls

# list content of the parent directory
ls ../
  • Command switches allows to list the directory's content with more or less detail and sorted according to different criteria.

    • The switch "a" lists all files and directories, even those starting with "." that are normally hidden.

    • The switch "l" lists the content in "long" format, including the total size (in blocks), mode (permission), the number of links, the owner, the size, the date of last modification, and the file or subdirectory name. Note that the first hyphen is replaced with a “d” if the item is a directory.

    • The switch "t" sorts the content of the directory by time modified (most recently modified first).

    • The switch "S" sorts the content of the directory by size (smaller first).

    • The switch "r" reverses the order of the sort.

    • The switch "R" recursively lists subdirectories encountered.

    • The switch "u" specifies the use of time of last access of the file, instead of last modification, for sorting the content.

    • The switch "U" specifies the use of time of file creation, instead of last modification, for sorting the content.

    • The switch "F" displays a slash ("/") immediately after a directory, an asterisk ("*") after an executable, an at sign ("@") after a symbolic link.

# list all files
ls -a
# list in long format
ls -l
# list in long format, sorting by date
ls -lt 
# list in reverse lexicographic order
ls -r
# list by size 
ls -S
# list in long format according to last access
ls -lu 
# list in long format according to the time of creation
ls -lU
# display “/” after a directory, “*” after an executable, “@” after a symbolic link
ls -F 
  • To view the commands available on luria, execute the following (note the space-separated list of directories to list):

ls /bin/ /usr/bin/ /usr/local/bin/

mkdir

  • This command name stands for "make a directory".

  • It creates a new folder (or directory). If no path is specified, the new directory is created in the current directory.

  • The switch "-v" specifies a verbose mode: a message with the folder(s) created is printed on the screen.

# create a directory named "testdir1" with a subdirectory named "testdir2"
mkdir testdir1
mkdir testdir1/testdir2

# change current directory directly to "testdir2"
cd testdir1/testdir2 
# go to the parent directory (i.e. testdir1) and print the working directory
cd ..
pwd
# create a new directory named "testdir3" with the verbose mode on
mkdir -v testdir3

cp

  • This command name stands for "copy".

  • It makes copies of files and directories to the specified location.

  • The switch "v" enables the verbose mode: messages describing the files copied are displayed on the screen.

  • The switch "i" enables the interactive mode: confirmation is requested before overwriting files.

  • Wildcards symbols such as "*" or "?" are commonly used to copy multiple files with a single command.

    • The symbol "*" stands for any number of alphanumeric characters.

    • The symbol "?" stands for a single alphanumeric character.

The following examples assume you have a directory named "unix_class" in your home directory:

# create a directory "unix_class" in your home directory and access it 
mkdir ~/unix_class
cd ~/unix_class
# copy the file named arrayDat.txt into your unix_class directory
cp /net/ostrom/data/dropbox/arrayDat.txt ~/unix_class/
ls

# the above command is equivalent to the following: 
cp /net/ostrom/data/dropbox/arrayDat.txt ./
ls
# use the interactive mode and type "y" to confirm the choice 
cp -i /net/ostrom/data/dropbox/arrayDat.txt ~/unix_class/
ls
# use the verbose mode to see the file being copied 
cp -v /net/ostrom/data/dropbox/arrayDat.txt ~/unix_class/
# make a local copy of the file call it "arrayDat1.txt" 
cp arrayDat.txt arrayDat1.txt
ls
# copy all the files with suffix "array”  into the current directory 
cp /net/ostrom/data/dropbox/UNIX/array* ./
ls

# copy any file whose extension is "txt" 
cp -i /net/ostrom/data/dropbox/UNIX/*.txt ./
ls

mv

  • This command name stands for "move".

  • It moves (renames) files and directories.

  • Several switches are available to specify the behavior of this command, including "-i" (interactive mode) and "-v" (verbose).

# cp arrayDat.txt into arrayDat1.txt, then rename arrayDat1.txt as arrayDat2.txt 
cp arrayDat.txt arrayDat1.txt
mv arrayDat1.txt arrayDat2.txt
ls
# rename in interactive mode, i.e. ask for confirmation before overwriting existing file 
cp arrayDat2.txt arrayDat3.txt
ls
mv -i arrayDat2.txt arrayDat3.txt
ls
# rename in verbose mode, i.e. print information on the screen 
mv -v arrayDat3.txt arrayDat4.txt
ls

rmdir

  • This command stands for "remove directory".

  • It deletes the specified directory.

  • The switch "-v" specifies a verbose mode: a message with the folder(s) deleted is printed on the screen.

  • Note that in general you cannot delete a directory that is not empty. The content of such directory has to be deleted before the directory itself can be deleted.

# remove the folder "testdir2" in the location specified by the path
rmdir ~/testdir1/testdir2
# remove the folder "newdir1"
rmdir testdir1
# remove the folder "newdir3" using the verbose mode
rmdir -v testdir3

rm

  • This command name stands for "remove".

  • It deletes files and directories.

  • Several switches are available to specify the behavior of this command, including "-i" (interactive mode) and "-v" (verbose).

# create copies of arrayDat.txt 
cp arrayDat.txt arrayDat1.txt
cp arrayDat.txt arrayDat2.txt
cp arrayDat.txt arrayDat3.txt
cp arrayDat.txt arrayDat4.txt
ls
# delete file 
rm arrayDat2.txt
# delete file in interactive mode, i.e. ask for confirmation before removing 
rm -i arrayDat3.txt
# delete file in verbose mode, i.e. prints the name of file deleted 
rm -v arrayDat4.txt
  • The "rm" command can be used in conjunction with wildcard symbols to delete multiple files at once. Extreme caution should be used, as action are not often reveresible. It is good practice either to use the interactive/verbose mode or use the command "ls" with the intended pattern, before invoking the command "rm".

# create a copy of arrayDat1.txt, then delete files with suffix “arrayDat”,
  followed by a digit and extension “txt”
cp arrayDat1.txt arrayDat2.txt
rm -i arrayDat?.txt
  • The switch "-r" deletes the content of a directory recursively,

i.e. deletes the directory's files and subdirectories, including their content.

#create a subdirectory and copy *.txt files 
mkdir testdir
cp *.txt testdir/
ls 
ls testdir/*

# remove directory and its content 
rmdir testdir
rm -r testdir
ls

cat

  • This command name stands for "concatenate".

  • It concatenates the content of file(s) and prints it out.

  • The switch "-n" specifies to number each line, starting from 1.

  • The "cat" command is often used in conjuction with the pipe ("|") to view the content of long files.

  • The "cat" command can be used to copy and append the content of files using output redirection (">").

# view the content of myfile.txt 
cat arrayDat.txt
# numbers each line of myfile.txt 
cat -n arrayDat.txt
# copy the content of myfile.txt using output redirection
cat arrayDat.txt > file1.txt
cat arrayDat.txt > file2.txt
ls

# concatenate the content of the two files
cat file1.txt file2.txt
# append the content of file2.txt to file1.txt
cat file2.txt >> file1.txt
cat file1.txt
  • When used without argument, the "cat" command takes STDIN as default argument. You can use this behavior to type in brief information, rather than invoking a standard text editor.

# type the following command 
cat > testfile.txt
# type any text, then hit Return, then press Ctrl-C 
#view content of newly created file
cat testfile.txt

less

  • This command is a "pager" that allows the user to view (but not modify) the contents of a text file one screen at a time.

  • The space-bar is used to advance to the next page.

  • The key "q" is used to "quit" the pager.

  • The switch "-n" allows to view the content of a file starting at the specified line.

# view the content of arrayDat.txt, hit the space bar to advance, type "q" to quit
less arrayDat.txt 
# view content of file starting at line 5
less +5 arrayDat.txt

head

  • This command displays the top part of a file.

  • By default, the first 10 lines of a file are displayed.

  • The switch "-n" specifies the number of lines to display.

  • The switch "-b" specifies the number of bytes to display.

# display the first lines of a file (10 lines by default)
head arrayDat.txt
#display the first 2 lines of a file
head -n 2 arrayDat.txt

tail

  • This command displays the bottom part of a file.

  • By default, the last 10 lines of a file are displayed.

  • The switch "-n" specifies the number of lines to display.

  • The switch "-b" specifies the number of bytes to display.

# display the last lines of a file (10 lines by default)
tail arrayDat.txt
#display the last 2 lines of a file
tail-n 2 arrayDat.txt

top

  • This command displays Linux tasks

  • The top program provides a dynamic real-time view of a running system

du

  • This command stands for Disk Usage

  • It estimates file space usage

# print sizes in human readable format
du -h

df

  • This command stands for Disk Free

  • It reports file system disk space usage

# print sizes in human readable format
df -h

Last updated

Massachusetts Institute of Technology