Here, I gather useful Linux Shell commands that you need know to work with a Bash terminal.
Change current directory with
I highly recommend installing
zsh instead of Bash. In
zsh, you don’t need to write
cd, only the name of the directory takes you there. Here, I assume you don’t use
Go one folder up, or to parent directory with
cd /home/sorush # we are at /home/sorush cd .. # we are at /home/
. means the current directory, it doesn’t do anything with
we can mix
.. with the absolute path:
cd /home/sorush/../../home/sorush/../. # we are at /home
Two useful shortcuts are
~: home folder, where current user’s files reside
-: previous folder
cd / cd ~ # we are at /home/sorush cd - # we are back to /
Get current directory address using the print-working-directory command :
Create an empty file via
If you are a
vim. When you save your changes, the file is created.
See file content
To print all the content of a file on screen
To browse up and down in an interactive way, use
less myFile.txt # or more myFile.txt
nano are two popular text editors in Bash which can also be used for viewing the content of files.
Read live logs
To see the live updates to a log file that is being written by a program, run
tail -f logFile
-f: follow updates
-f is dropped, you see only the last 10 lines of the file at the moment you press Enter.
Create a directory with
Get content of a directory with the list command
ls -al /home
-a: show everything
-l: show details.
You can drop
-al to see the difference. If you drop the destination folder, it shows the content of the current directory.
To show a tree-like list of content:
tree -d -L 2
-d: show only directories
-L 2: show only 2 layers of subdirectories
On some Linux distros
tree is not available by default, you have to install it.
Get the size of a directory (I read it “dush”):
du -sh ~/myFolder
-h: human-readable like MegaByte (M), GigaByte (G)
-s: to hide subdirectories.
Print to file
To write the output of a command into a file, use
> followed by the name of the file:
ls > myFile.txt
Nothing will be shown in the terminal.
A file in the current folder can be copied into another folder
cp myfile.txt ~/myfolder/
A folder can be copied into another place:
cp -r ~/myfolder ~/anotherFolder
-r: copy the folder and everything inside (Recursive)
cp command overwrites existing files without any question.
Move command is to copy a file/folder to another place and delete the original file/folder. It is equivalent to cut-paste in Windows and copy-move in Mac.
A file can be moved to a new folder via:
mv myfile.txt ~/myfolder
A folder can be moved into another folder:
mv ~/myfolder ~/anotherFolder
The move command is used for renaming a file
mv myfile.txt info.csv
or renaming folder:
mv ~/myfolder ~/Simulation01
So when using
mv command, if the destination folder exists, cut-paste happens, otherwise, the current folder is renamed to the destination folder.
To remove a file or directory
rm ./myFileOrFolder -rf
-r: recursively delete i.e. removes everything including subfolders and files in them. This is what happens when we delete a folder in Windows or Mac. This is not necessary for files.
-f: Use forced deletion i.e.
rmwill remove all the files without any confirmation, even if the files are write-protected.
Add it before any command to be run as a superuser (admin). You will be requested for your admin password. For example, to install an application on Ubuntu you need to have sudo privilege:
sudo apt install tree
Changing and deleting sensitive system folders needs a superuser.
Search for file
You can find a file in the current directory and its subdirectories via
find . -name "*.txt"
*: wildcard for any
You can also put the exact name there.
To get a list of all the commands you’ve used so far, run
the result is two columns, the first column is the ID of command and the second is the command like
316 cd /home/sorush/../../home/sorush/../. 317 cd / 318 cd ~ 319 pwd
To run command again, use
!+ID of the command:
!319 # will run pwd
To run the last command, use
!!. Here I run the last command with
Grep via pipes
grep, we can filter the outcome of a command. For example, we want to filter the outcome of
history command to only show the ones that contain
history | grep ssh
or list the files of the current directory but only show text files
ls | grep .txt
We can set a variable in Bash by
We store a text in a variable. If there are spaces in the content, use quotations:
nameOfVariable="The Content Of Variable"
The content is usually a path to a file or folder:
The variable is called with
echo to see the content of a variable
We can use the variable in any Bash command:
cd $destination cp myFile.txt $destination
Store the outcome of a command into a variable :
If a variable is needed to be available inside every program, we use
export command to make it an environment variable:
For example, CMake reads
CC environment variable to find C compiler path. In a terminal, we can set it as:
We can see all the environment variables defined, with the command
A good place to define environment variables is
~/.bashrc in Ubuntu or
~/.zshrc for Z-shell.
To have an executable available in every working directory, add its folder to
To have a library accessible by compilers, add its folder to
To create a custom command, we use alias. See some of aliases I use in my terminal:
alias hgrep="history | grep" alias lgrep="ls | grep" alias current='cd "/home/sorush/test program"' alias brc="vim ~/.bashrc" alias zrc="vim ~/.zshrc" alias dirsize="du -sh" alias unicomputer="ssh firstname.lastname@example.org"
So we can do this:
hgrep ssh # equals to history | grep ssh
A good place for aliases is
~/.bashrc in Ubuntu.
We can see all the aliases defined in a terminal by running
Get the list of disks and their free space with disk free command
Get memory usage by
Get the list (ls) of CPUs (cpu) and their info with
Get a list of running processes similar to Windows Task Manager with
I recommend using
htop, it looks much nicer than
htop is not available, and you have to install it by yourself.
Clear & Exit
You can clear the terminal with
You can press
ctrl+l to do the same.
You can exit a terminal by:
Stop a process
ctrl+c to kill a process running in the current terminal:
# takes 5 seconds to finish sleep 5 # press ctrl+c before it finishes
To stop an application (or process) that is running in the background or another terminal:
Another way is to first get the ID of the application:
You can also use
htop. Then stop it using its ID
tar.gz is a very popular format for compressed files in Linux. You can extract them via:
tar -xvf myFile.tar.gz
The same works for
Sometimes you want to copy a text from the terminal into the Linux clipboard, and then paste it somewhere else, for example in a Word document. This is what I do in Ubuntu:
- Copy: select a text with the mouse and press
ctrl+shift+cto copy it.
- Paste: paste the clipboard content into a terminal with
You can also do both of the above with right-click on a terminal.
Write a line in a terminal and move the cursor to the center:
- ctrl+k: cut everything after the cursor (I say Knock)
- ctrl+u: cut everything before the cursor (I say Up)
- ctrl+y: paste what cut before (Yank)
- ctrl+a: to the begining of the line (Arch)
- ctrl+e: to the end of the the line (End)
You can change who is eligible to read/write/execute a file using
chmod command. There are a lot of details there but a few useful tricks are mentioned here.
Make a file executable:
chmod +x nameOfTheFile
Make a file/folder to be read/written/executed by only you (a private file)
chmod 700 nameOfTheFile
Make a file available to every user (a shared file)
chmod 777 nameOfTheFile
Sometimes you want to run a command that takes a lot of time like copying huge files and in the meantime, you want to do other stuff. You can run the time-consuming task in the background of the terminal by adding
& to it:
# A CPU sleeps 20 seconds sleep 20 &
You can see the list of all processes in the background via
jobs #  + running sleep 20
You can kill the process with its job id
# Note the % sign kill %1
If you want to run a program, for example, a GUI program, from a terminal but you want them to be detached. In other words, if you close the terminal, the GUI program is still running, use
nohup paraview &
The window of Paraview will be opened and detached from the terminal. All the messages that Paraview sends to the terminal are redirected to
Sometimes you need to find a process
ps -ef | grep text_1 | grep text_2
ps -ef shows all the processes on the machine in columns of username, process id, date, process command, and so on. We can filter the results with as many
grep pipes as we like. For example, text_1 can be the username, text_2 can be the process command.
A process is killed by its id (we get it from
ps -ef command):
You can download a file from the internet by knowing its download link:
Before jumping to google to understand a Linux command, try the
man command. It may explain them better:
man ls man cp man history
Some executables are in the system path. You run them without you know where they reside. To know where the binary, source and manual of command are, run:
whereis ls whereis history whereis gcc
If you just want to know the address of the binary of a command, use
which cp which ls which gcc
A symbolic link or symlink is a file that refers to another file or folder. It is similar to a shortcut in Windows.
To create a link file from an existing file, run
ln -s ExistingFile LinkFile
See this example:
# create a new file touch myFile.txt # create a symlink to it ln -s myFile.txt linkToMyFile.txt
Now any change to the content of
linkToMyFile.txt is applied to
Note symlinks can be also created for folders.
If you create a symlink to an executable, running the symlink is the same as running the executable.
- Script to copy a directory path in Bash terminal
- What is the difference between .bashrc, .bash_profile, and .profile?
- A C++ MPI code for 2D arbitrary-thickness halo exchange with sparse blocks
- Create clone of a C++ class in polymorphic way
- How to use type erasure pattern to decouple polymorphic classes in C++?