Introduction
This is part of a series about Bash and the Linux Shell.
Other posts in the series introduce and explain several basic terminal commands used in Bash and the Linux Shell.
This post serves as a quick reference for those commands.
Context
In this post we’ll assume that Bash is installed and that all commands are entered in a Bash terminal.
While much of the information applies generally to Linux, some of the instructions will not apply to all systems. Specifically, program installation instructions focus on utilizing the package manager used by Debian and Ubuntu. Other distributions will require different installation steps.
The majority of these commands were tested on Bash 5.1.16 on Ubuntu 22.04.3 or later. They may behave differently on other systems.
Finally, much in Linux is based around the user and user-specific files. For our examples, we’ll reference the user name “Emerson”. Wherever you see Emerson substitute your user name.
Bash Commands
Basic shell interactions via key combinations
open a new terminal window
Press the keys:
CTRL
+ ALT
+ T
close the terminal window
While in the terminal window you want to close, press the keys:
ALT
+ F4
stop a running command
While in the terminal window running the command you want to stop, press the keys:
CTRL
+ C
copy highlighted text
Highlight some text in the terminal and press the keys:
CTRL
+ SHIFT
+ C
paste text from the clipboard
CTRL
+ SHIFT
+ V
cycle through previous commands
↑
(the UP ARROW) and ↓
(the DOWN ARROW)
auto-complete a partially-typed command
Type the first part of the command and then press the TAB
key.
Commands to learn about commands
--help option
To learn more about a specific command one approach that often works is to type the command followed by the --help option.
Command form:
{{command name}} --help
Example Command:
history --help
--version option
To learn version information about a command type the command followed by the --version option.
Command form:
{{command name}} --version
Example Command:
bash --version
which command
It’s possible for multiple programs with the same name to be installed on a system. To see which one is called when you enter the program name, use the which command followed by the name of the command you want to check. This will display the path to the program that is run when the command is entered.
See which program is run by a command
Command form:
which {{command name}}
Example Command:
which echo
man command
Many commands come with their own manual page, which can be displayed with the man command followed by the name of the command.
Open the manual page for a command
Command form:
man {{command name}}
Example Command:
man date
Note: To exit the manual page, press q
.
tldr command
Not installed by default on most systems.
The tldr command displays short, simple help pages for command line tools from the user-maintained tldr-pages project.
It is also available via the web app and can be downloaded as a PDF version for offline use.
NOTE: More information is available in our post about the tdlr command.
See the tldr page for a command
Command form:
tldr {{command name}}
Command Example:
tldr echo
Navigating the file system and examining files
pwd command
The pwd command (short for print working directory) from the GNU project tells us where we’re currently located within our file system in the terminal.
See the working directory
Command:
pwd
ls command
The ls command from the GNU project lists the contents of a directory.
List the contents of a directory
Command:
ls
The ls command has several helpful options:
-
-a
displays all content, including hidden files. -
-1
displays items in a single column. -
-l
displays items in “long” format. -
-lh
displays items in “long” format and the file sizes in “human-readable” units, like kilobytes and megabytes. -
-lt
displays items in “long” format and sorts by modification time. -
-lt --time=birth
displays items in “long” format and sorts by creation time.
cd command
The cd command changes directories - that is it changes the working directory of the terminal.
It requires additional arguments (most of the time) to change to a specified directory.
Command Form:
cd {{arguments}}
The cd command has several helpful options:
-
..
go up one directory (to the parent directory). -
../..
go up two directories (to the parent directory’s parent). -
-
go back to the previous working directory. -
/
go to the root directory.
Change the working directory to the home directory
To go to the user home directory, use the command without any arguments.
Command:
cd
Change the working directory to a specific subdirectory
To change to a specific subdirectory enter its relative path from your current working directory.
Example Task:
Move to the /Pictures subdirectory.
Example Command:
cd Pictures
Example Task:
Move to the /Pictures/Wallpapers subdirectory.
Example Command:
cd Pictures/Wallpapers
Change the working directory to a specific directory
To change to a specific directory enter its full path.
Example Task:
Move to the /Pictures/Wallpapers subdirectory of the home directory of a user called Emerson.
Example Command:
cd /home/Emerson/Pictures/Wallpapers
tree command
Not installed by default on most systems.
The tree command from Steve Baker displays a visual tree of the directory, it’s subdirectories, and all their content.
NOTE: More information is available in our post about the tree command.
Installing the tree command
To install tree on Ubuntu:
Command:
sudo apt install tree
View the contents of the working directory and all subdirectories
To see all contents of the working directory and all its subdirectories use the command without any arguments.
Command:
tree
View the contents of the working directory and all subdirectories to a specific depth
To see all contents of the working directory and all the contents of its subdirectories but only to a certain depth of subdirectories use the -L
option.
Example Task:
Limit to 1 level of subdirectories.
Command:
tree -L 1
Example Task:
Limit to 2 level of subdirectories.
Command:
tree -L 2
View the contents of the working directory looking only at subdirectories
Example Task:
Output only directories and not files use the -d
option.
Command:
tree -d
View the contents of the working directory looking only at specific subdirectories
To see the contents of only specific subdirectories use the -a
option.
Example Task:
Output only the contents of the /Pictures subdirectory.
Example Command:
tree -a Pictures
Working with Directories
mkdir command
The mkdir command from the GNU project makes directories.
Command Form:
mkdir {{path_to_directory/directory_name}}
Make a subdirectory in the working directory
Example Task:
Make a subdirectory called projectx in the working directory
Example Command:
mkdir projectx
See output from the command
To see output about what the command did use the -v
(verbose) option.
Example Task:
Make a subdirectory called projectx in the working directory.
Example Command:
mkdir -v projectx
Example Output:
mkdir: created directory 'projectx'
Make a subdirectory in a specific directory
Example Task:
Make a subdirectory called projectx in the /home/Emerson/Documents/ directory.
Example Command:
mkdir -v /home/Emerson/Documents/projectx
Example Output:
mkdir: created directory '/home/Emerson/Documents/projectx'
rmdir command
The rmdir command from the GNU project removes directories, with the caveat that it only works on empty directories. (You can first delete the files or use the trash command, which can remove directories that contain files.)
Command Form:
rmdir {{path_to_directory/directory_name}}
Delete a subdirectory in the working directory
Example Task:
Delete a subdirectory called projectx in the working directory.
Example Command:
rmdir projectx
See output from the command
To see output about what the command did use the -v
(verbose) option.
Example Task:
Delete a subdirectory called projectx in the working directory.
Example Command:
rmdir -v projectx
Example Output:
rmdir: removing directory 'projectx'
Delete a subdirectory in a specific directory
Example Task:
Delete a subdirectory called projectx in the /home/Emerson/Documents/ directory.
Example Command:
rmdir -v /home/Emerson/Documents/projectx
Example Output:
rmdir: removing directory '/home/Emerson/Documents/projectx'
open command (for directories)
The open command from freedesktop.org opens files and directories using the default application for the file type. (For Ubuntu systems using GNOME desktop from the GNOME project this is GNOME Files (formerly called Nautilus).)
Command Form:
open {{path_to_directory/directory_name}}
Open a specific directory in the default graphical file manager
Example Task:
Open the /home/Emerson/Documents directory.
Example Command:
open /home/Emerson/Documents
Open the working directory in the default graphical file manager
To open the working directory use .
(a period), which tells the shell to run the command on the working directory.
Command:
open .
Working with files
touch command
The touch command can be used for other things, but most often it is used to create files.
Command Form:
touch {{path_to_file/file_name}}
Create a file
Example Task:
Create a file called test123.txt.
Example Command:
touch test123.txt
open command (for files)
As mentioned above, the open command from freedesktop.org opens files and directories using the default application for the file type.
Command Form:
open {{path_to_file/file_name}}
Open a file in the default program for that file type
Example Task:
Open the favorites.html file.
Example Command:
open favorites.html
Working with files or directories
mv command
The mv command from the GNU project can move and/or rename files and directories.
Recommendation: Use the -n
(no overwrite) option to prevent any possible overwriting of existing files and the -v
(verbose) option to get output from the command to see what it has done. (Even in cases when these are not necessary, it’s a good habit to build to avoid unnecessary mistakes.)
Command Form:
mv -nv {{source}} {{target}}
Move a file
Example Task:
Move the quotations.txt file to the Archive subdirectory.
Example Command:
mv -nv quotations.txt Archive/quotations.txt
Example Output:
renamed 'quotations.txt' -> 'Archive/quotations.txt'
Note that the output is a little odd because it says the file was “renamed” though really it was just moved. It’s a quirk of the output to be aware of.
Rename a file
Example Task:
Rename the quotations.txt file to the quotes.txt.
Example Command:
mv -nv quotations.txt quotes.txt
Example Output:
renamed 'quotations.txt' -> 'quotes.txt'
cp command (to copy files)
The cp command from the GNU project allows you to copy files and directories.
Recommendation: Use the -i
(interactive) option to prompt before overwriting of existing files and the -v
(verbose) option to get output from the command. Even in cases when these are not necessary, it’s a good habit to build to avoid unnecessary mistakes.
Command Form:
cp -iv {{source}} {{target}}
Copy a file to a different directory while retaining the file name
Example Task:
Copy the config.txt file to the /Archive subdirectory.
Example Command:
cp -iv config.txt Archive/config.txt
Example Output:
'config.txt' -> 'Archive/config.txt'
Copy a file to the same directory with a different file name
Example Task:
Copy the config.txt file to the same directory and change its name to config.txt.bak.
Example Command:
cp -iv config.txt config.txt.bak
Example Output:
'config.txt' -> 'config.txt.bak'
Deleting files or directories
trash vs rm
The command to delete files or directories that comes standard with nearly all Linux distributions is rm. But, as Rob Griffiths points out, the rm command can be dangerous:
Even if you’re brand new to Terminal, I hope someone has given you this valuable advice:
Be very careful when using the rm command to remove files and folders!
The Unix file and folder deletion tool rm is one of the most dangerous commands around. After all, no one likes to obliterate precious files by accident.
With rm it is possible to accidentally delete the wrong files and directories. And when using rm it can be difficult or impossible to recover those files.
As the Ubuntu command line tutorial cautions:
Unlike graphical interfaces, rm doesn’t move files to a folder called “trash” or similar. Instead it deletes them totally, utterly and irrevocably. You need to be ultra careful with the parameters you use with rm to make sure you’re only deleting the file(s) you intend to. You should take particular care when using wildcards, as it’s easy to accidentally delete more files than you intended.
And as Dave McCay puts it:
The rm command can delete files, groups of files, directories, or complete directory trees. That’s why it must be used with caution. Using rm isn’t difficult, but the penalty for failure is high.
When a file is deleted with rm , it is gone. It isn’t moved to the trash. It is obliterated immediately. That doesn’t mean you should avoid using rm . But to use it safely, you need to be aware of what it can do, and ensure you’re using it properly.
Some tools are more dangerous than others, and far less forgiving of mistakes. That’s why there’s never been a movie called The Texas Wrench Massacre. rm isn’t a wrench, it’s definitely a chainsaw.
People use chainsaws all day everyday, and as long as they use one responsibly and mindfully, they’re fine. It’s the same deal with rm . When you pull rm out of your tool bag, you ought to slow down and check, then double-check, your command line.
We think this is a good analogy and helpfully illustrates the power of rm. However, we respectfully disagree with the assertion that users shouldn’t avoid using rm.
We think you should avoid using rm unless it’s exactly what you need.
We think you should avoid using rm in the same way that you should avoid using a chainsaw for most household tasks. You don’t use a chainsaw to slice bread. You don’t use a chainsaw to open cardboard boxes. You don’t use a chainsaw to cut wrapping paper. A chainsaw is too dangerous and powerful of a tool for most tasks. The cost of a mistake is too high. And there are completely suitable alternatives for most tasks.
Yes, people use chainsaws everyday. And every day some people are hurt badly by chainsaws. It would be senseless to use such a dangerous tool without a need to use it. And typically that just isn’t the case with either chainsaws or rm. It is important to know about the rm command, but we don’t recommend making it your day-to-day tool for deleting files.
Leave the chainsaw in the workshop on the shelf for most daily tasks, and use the safer and simpler tools when they work just as well. Bring out the chainsaw only when that is what you need.
Furthermore, even though the rm command may be helpful with some tasks and might be good to learn eventually, we don’t believe it is the tool that new users should focus on.
Instead, we recommend learning and using the trash command for most situations.
trash command
Not installed by default on most systems.
The trash command from Andrea Farncia moves files to the system trash can.
The trash command operates in line with contemporary expectations that files are simply moved to a temporary location where they can be recovered instead of instantly deleted forever. This gives you a chance to see (and potentially restore) deleted files.
It does not come standard on most Linux distributions, so you will likely need to install it.
NOTE: More information is available in our post about the trash command.
Installing the trash command
To install trash on Ubuntu:
Command:
sudo apt install trash-cli
Moving a file or directory to trash
Command Form:
trash {{path_to_content/content_name}}
Example Task:
Delete the recipes.txt file.
Example Command:
trash recipes.txt
Restoring a deleted file or directory from trash
The trash-restore command is interactive. Navigate to the directory from which the file or directory was removed and enter the following command:
Command:
trash-restore
Example Output:
0 2023-10-18 12:47:21 /home/Emerson/Documents/recipe.txt
What file to restore [0..0]:
To restore the recipe.txt file type 0
and press Enter
.
Selectively delete contents from the trash folder by removal date
The trash command doesn’t permanently delete files so they remain on your system taking up space until you delete them permanently.
If you want to remove those files, you have several options. Here we’ll look at permanently deleting files based on how long they have been in the trash. (See the tldr pages for trash to see some additional options.)
A note of caution: This step will permanently delete files. Be sure of what you are removing before proceeding.
Example Task:
Permanently delete files that have been in the trash longer than 120 days.
Example Command:
trash-empty 120
rm command
The rm command from the GNU project removes files or directories and comes standard with nearly all Linux distributions, but, as discussed above, it can be dangerous because it’s easy to delete the wrong thing and it can be difficult or impossible to recover files.
Recommendation: If you are going to use the rm command, it usually makes sense to use the -i
(interactive) option to see prompts before files are deleted and the -v
(verbose) option so that you are made aware of what the command has done.
As mentioned above, we recommend using trash instead of rm whenever possible.
aliasing the rm command
While the rm command itself can be dangerous, some suggested strategies for aliasing rm are also dangerous. If you want to avoid accidental use of rm, alias to a different name than rm so you don’t create a dangerous habit, or consider aliasing to an explanatory echo command.
Text editors
gedit text editor
gedit from the GNOME project is a text editor with a simple GUI.
Open a file in gedit
Command Form:
gedit {{path_to_file/content_name}}
Example Task:
Open the summary.txt file.
Example Command:
gedit summary.txt
nano text editor
nano from the GNU Project is a command line text editor.
There are a few commands that are helpful to know:
CTRL
+ G
opens the nano help menu.
CTRL
+ X
closes the help menu.
CTRL
+ S
saves the file.
CTRL
+ X
exits nano.
You can refer to the overview of nano’s shortcuts to find additional text commands.
Open a file in nano
Command Form:
nano {{path_to_file/content_name}}
Example Task:
Open the summary.txt file.
Example Command:
nano summary.txt
Vim / Neovim
Vim from Bram Moolenaar is a command line text editor.
Neovim from the Neovim team is a command line text editor based on Vim.
Some introductory information:
- Vim and Neovim are command line text editors that are largely compatible with one another.
- You’ll find Vim on many Linux systems.
- They both have a bit of a learning curve and most users don’t find them intuitive at first.
- Consequntly, you might find yourself unintentionally in one of them. So, it’s helpful to know how to exit them, even if you don’t plan to use them.
- There are helpful resources for if/when you’d like to learn more about them.
Exit Vim / Neovim
Exiting Vim is so non-intuitive that many new users feel trapped.
The basic sequence to quit Vim without saving is:
Esc
:
q
!
Enter
And the basic sequence to save and quit Vim is:
Esc
:
w
q
Enter
Learn more about Vim / Neovim
If you decide to learn Vim or Neovim there are numerous resources available Below are two:
- The Interactive Vim Tutorial is exactly what it sounds like and provides a guided learning path.
- In Vim Adventures you can learn Vim while playing a game.
Terminal Output
echo command
The echo command from the GNU project displays user-specified text to the screen.
Command Form:
echo {{Arguments}}
Display a specific string of text to the console
To display a specific string enter it as an argument.
Example Task:
Display the text Hello World.
Example Command:
echo Hello World
Example Output:
Hello World
Display a system variable to the console
To display a system variable enter it as an argument.
Example Task:
Display the SHELL variable.
Example Command:
echo $SHELL
Example Output:
/bin/bash
clear command
The clear command clears the output from the terminal window.
Clear the console
Command:
clear
System Management
sudo command
The sudo command executes a single command with superuser privileges.
Superuser privileges are required for various system operations - like installing software - and the sudo command has various uses and hazards.
Command form:
sudo {{command line}}
Example Command:
sudo apt update
apt command
The apt command from the Debian Linux distribution is the package management utility for Debian-based distributions.
Command form:
sudo apt {{arguments}}
Example Command:
sudo apt upgrade
neofetch command
Not installed by default on most systems.
The neofetch command from Dylan Araps is a “CLI tool to display information about your operating system, software and hardware.” according to tldr pages.
NOTE: More information is available in our post about the neofetch command.
Install neofetch
On Ubuntu, neofetch can be installed through the package manager.
Command:
sudo apt install neofetch
View system information
Command:
neofetch
Assorted Commands
date command
The date command from the GNU project displays the system date and time.
See system date and time
Command:
date
See system date and time in a specific format
You can also pass arguments to the date command to change it’s format.
Example Task:
Output the date and time in the YYYY-MM-DD HH:MM:SS format.
Example Command:
date '+%Y-%m-%d %T'
Example Output:
2023-09-01 14:04:34
history command
To see a history of the commands used in the terminal, use the history command from the GNU project.
See the history of commands entered in the terminal
Command:
history
See the history of a specific number of commands entered in the terminal
Example Task:
Output the history of only the last 5 commands.
Example Command:
history 5
Pattern Matching (i.e. Wildcards)
The Linux shell allows several types of characters to stand-in, or represent, other characters. Officially, Bash refers to this as pattern matching. More popularly, these are referred to as wildcards. Wildcards allow greater ease and flexibility in finding or specifying files.
? (question mark) wildcard
In patterns, the ? wildcard will match to any one character.
For instance, file?.txt
will match:
- file1.txt
- file9.txt
- fileA.txt
- fileb.txt
- file_.txt
- file .txt
- and any other file that starts with file, ends with .txt, and includes any one character between those strings.
* (asterisk) wildcard
In patterns, the * wildcard will match to any characters, including no character.
For instance, file*.txt
will match:
- file.txt
- file1.txt
- file11.txt
- file141.txt
- file9.txt
- fileAa.txt
- filebin.txt
- file_today.txt
- file of the month.txt
- and any other file that starts with file, ends with .txt, and includes any character, characters, and no character, between those strings.
Special Characters
Tilde expansion (~)
Tilde expansion is a shell expansion that replaces an unquoted tilde, ~
, with the path for the user’s home directory.
Example Task:
Navigate to the /Pictures/Wallpapers subdirectory of the users home folder.
Example Command:
cd /home/Emerson/Pictures/Wallpapers
Or, with tilde expansion we could shorten it to the following:
Example Command:
cd ~/Pictures/Wallpapers
Shell Variables
PATH variable
PATH is a Bourne Shell variable that contains a colon-separated list of directories in which the shell looks for commands.
To output the PATH variable:
Example Command:
echo $PATH
Example Output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/snap/bin:/
SHELL variable
SHELL is a Bash variable that expands to the full pathname to the shell.
To output the SHELL variable:
Example Command:
echo $SHELL
Example Output:
/bin/bash
RANDOM variable
RANDOM is a Bash variable that returns a pseudo-random number between 0 and 32767.
NOTE: More information is available in our post about the RANDOM variable.
Example Task:
Output a pseudo-random number.
Example Command:
echo $RANDOM
Example Output:
19853
Further Resources
To find a curated list of many more resources for learning about Bash and the Linux Shell you can review or central topic post.