Quick Reference
This page collects commands we have (or will) discuss throughout the course. Click on a command for more details on its operation and some examples of common or useful operations. We encourage students to contribute to this page through the advanced homework on git and open source contributions.
Basics
cat
cat
is a command that concatenates files and prints the concatenated files to the standard output.
$ echo "This is an:" > beginning.txt
$ echo "example" > end.txt
$ cat beginning.txt end.txt
This is an:
example
cd
cd
is used to change directory. Unlike most commands, cd
is not
a separate program, it is a shell built-in. cd
is a useful tool to navigate up and down the hierarchy of the file systems on your machine, and move into a given directory.
$ cd ~
$ cd /home
cp
cp
is used to copy files or directories.
$ cp source destination
$ cp file1.txt file2.txt
$ cp -b file1.txt file2.txt
$ cp -v file1.txt file2.txt
$ cp * ~/Desktop
echo
echo
is used to output whatever it is given into stdout. Often useful see the value of environment variables.
$ echo "Hello World!"
Hello World!
$ echo $USER
<your username>
ls
ls
is used to list the contents of a directory. By default, ls
will simply print file names.
$ ls
directory file
man
man
allows users to format/display the user “manual” that is built into Linux. This manual holds a catalogue of commands and other aspects of the Linux OS.
$ man [command]
$ man ls
$ man strcmp
mkdir
mkdir
is used to create directories (if they do not already exist) on a file system.
The general format is
$ mkdir [options] directories
mv
mv
is used to move a source file or directory into a destination directory. It is also commonly used to rename files.
$ mv source desination
passwd
passwd
is a command that is used to change the password of system users.
$ passwd
Changing password for user [current user]
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
pwd
pwd
stands for print working directory. pwd
outputs the full pathname of the current work directory.
$ pwd
/home/bo/Desktop
pwd
can also be used to store the full path to the current directory.
x=$(pwd)
rm
rm
is used to delete one or multiple files from your current directory. Be careful when using because this does not move the item to trash, it permanetly deletes it.
$ rm file1.txt file2.txt
rmdir
rmdir
is used to remove empty directories.
The format is
$ rmdir [options] directories
Scripting
> (standard output redirection operator)
command1 -[options] [arguments] > <output file name>
The standard output redirection operator is used in bash to redirect the output of one command to a specified file.
./a.out > output.txt
>>
‘>>’ is a shell operator that you can use to append the output in an existing file.
$ echo line 1 >> example.txt
$ cat example.txt
line 1
$ echo line 2 >> example.txt
$ cat example.txt
line 1
line 2