Section B.1 Bash Shell Cheat Sheet
Getting help with shell commands.
man <command-name>-
Use the built-in manual.
- e.g.
man cdretrieves the manual for the change directory command.
<command-name> --help-
Request the help page (when it exists) for the specified command. Note that not every command supports
--help.- e.g.
cd --helpretrieves help for the change directory command.
File and Directory Commands.
pwd-
Print working directory displays the path of the current working directory.
- e.g.
pwdprints the path of the current working directory.
whoami-
The whoami prints the userid of the current user.
- e.g.
whoamiprints the userid.
ls-
List displays basic information about files and directories.
- e.g.
lslists directories and files in the current directory. - e.g.
ls -llists directories and files in the current directory using a long listing. - e.g.
ls ~lists directories and files in the user’s home directory.
touch <file-name>-
The touch command is commonly used for file creation. Its intended primary function is to update its timestamp, by "touching" it. See
man touchfor more information on the intended use.- e.g.
touch newfile.txtcreates an empty file named newfile.txt.
cd <directory-name>- Change directory to
<directory-name>.- e.g.
cd /changes the current directory to the root directory. - e.g.
cd ~changes the current directory to the user’s home directory. - e.g.
cd ..changes the directory to the immediate parent directory.
mv <old-name> <new-name>- Move (rename) files or directories.
- e.g.
mv old.txt new.txtchanges the name of old.txt to new.txt.
rm <file-name>- Remove deletes a file or directory.
- e.g.
rm junk.txtremoves the file named junk.txt.
mkdir <directory-name>- Make directory with name <directory-name>.
- e.g.
mkdir newdirmakes a new directory with the name newdir.
rmdir <directory-name>- Remove directory with specified name <directory-name>
- e.g.
mkdir olddirremoves (deletes) the directory with the name olddir.
The Basics: Reading, Writing, Counting, etc.
echo <text>- The echo command displays a line of text and/or requests the value of a variable from the shell and displays its value. Often used with output redirection.
- e.g.
echo 'Hello World!'print the text ’Hello World!’ on the standard output. - e.g.
echo $USERprints the value of the USER environment variable on the standard output.
cat <file-name>- The concatenate prints file contents on the standard output after concatenation. Note that with a single file, it just prints that file. It is often used with output redirection.
- e.g.
cat file.txtprints the contents of file.txt on the standard output. - e.g.
cat file1.txt file2.txtprints the contents of the concatenation of file1.txt and file2.txt on the standard output.
read <variable-name>- The read command reads a line or variable from the keyboard. It is often used with scripts or input redirection.
- e.g.
read MYVARtakes input from the keyboard and directs it into a variable called MYVAR.
wc <file-name>- The word count command performs a count of lines, words, and bytes for each file.
- e.g.
wc file.txtreports the count of lines, words, and bytes in file.txt.
history- The history command displays a list of previously executed shell commands, allowing users to review their command history.
-
e.g.
historycould display:1 git init 2 git add main.c 3 git commit -m "Initial commit" 4 git remote add origin https://github.com/username/repo.git 5 git push -u origin master 6 history
-
Input and Output Redirection.
- Input redirection using
< - Input redirection uses using
<to allow the user to redirect the input from a file rather than the keyboard.- e.g.
wc < info2count.txtperforms thewccommand on the information in the file info2count.txt.
- Output redirection using
>or>> - Output redirection allows the user to redirect the output from the standard output to a file using
>for overwriting or>>for appending.- e.g.
echo 'I love open source!' > file.txtwrites the line ’I love open source!’ into the file file.txt replacing the current contents or making a new file if it doesn’t already exist.
- Piping
| - A pipe
|in the bash shell allows you to redirect (pipe) the output of one command into the input of another command.- e.g.
ls | wcruns the commandls>and uses the output of thelscommand as the input into thewccommand.
File Permissions.
chown <newuser:newgroup> <file-name>- The chown command is used to change the file owner and/or group.
- e.g.
chmod pearcej file.txtchanges the owner of file.txt to pearcej. - e.g.
chmod :friends file.txtchanges the group of file.txt to friends.
chmod <change><which> <file-name>-
The
chmodcommand is used to change permissions. The following symbols are the most commonly used:+change by adding permission-change by removing permissionrwhich permission: readwwhich permission: writexwhich permission: execute- e.g.
chmod +x helloworld.shadds execute permission for all users to the helloworld.sh file.
Need more detail?
For more information on any of these commands, see Section 3.2 or use the
man pages.