In this guide you are going to learn the most simple yet powerful Linux commands which every Linux system user should know. These commands are used over and over on your daily work.
Below given the Tips & Tricks you are about to learn.
Linux Commands Tips & Tricks
- Return to previous directory or home directory.
- Shortcut to clear your screen.
- Remember directories you used before pushd popd.
- Send apps to background and get back in frontend.
- When you forget to use sudo with your command.
- Run a specific command from history.
- Chaining commands together.
- Find original file of a symbolic link.
- Create multiple directories using a single command.
- Answer yes or no in the command.
Return to Previous Directory or Home Directory
To return to previous directory you can use the cd
command with hyphen -
. This command will output the directory you are and changes you to that directory.
cd -
Output
/home/cloudbooklet
To return to the home directory you can use the cd
command with the tilde ~
symbol.
cd ~
Shortcut to clear your screen
By default you can use clear
command to clear your screen.
But you can use the shortcut with CTRL + L
to clear your screen. What this command does is just move your shell cursor to the top of the screen to gain the most available space.
clear
CTRL + L
If your shell is behaving strange or having some issues you can try using the reset
command otherwise you can just use the shortcut mentioned above.
Remember Directories you used before Pushd Popd
With pushd
and popd
command you can get in to the directory that is remembered before easily.
Pushd command remembers the directory.
Popd gets in to the remembered directory.
The below example is used while moving from home directory ~
to /etc
directory using pushd
command. The command outputs the target directory and the current directory you are in.
pushd /etc
Output
/etc ~
Now the commands remembers the ~
directory.
You can now navigate to some other directories using cd
command and when you wish to move in to the directory which is remembered before using the pushd
command you can just use the popd
command.
popd
Output
~
Send apps to Background and get back in Frontend
For example in case when you are editing a file using nano
or vim
editor and you haven’t completed your changes, but you need to return to the terminal and execute some commands and get back to the editor without losing the changes you made already. You can use this with any commands like top
, htop
, etc.
This is where the following commands comes in. While you are editing you can use a shortcut CTRL + Z
to exit the file by moving the editor to background.
CTRL + Z
To move back to the editor without losing changes you can use the fg
command which moved the editor to the foreground.
fg
When you forget to use sudo with your command
When you forget to use sudo
with your command you don’t have to repeat the exact full command again with sudo
. Instead you can just use double exclamation mark !!
with sudo.
apt update
If you run the above above without sudo
you will get an error with permission denied.
To run the above command again with sudo
you can use the following shortcut.
sudo !!
The double explanation mark tells the shell to run the most recent command with sudo.
Run a Specific Command from History
The history command outputs the commands that are executed earlier. The output looks something similar to the one below.
85 sudo apt clean 86 df -h
To run the sudo apt clean
command again you can use the below shortcut with exclamation mark and the ID of the command.
!85
The above command will outputs the command and execute the command associated with the ID.
Chaining Commands Together
There are two methods to chain commands together one is using two ampersands &&
and the other one is using semi-colon ;
The both command outputs the same if both commands are executed without any failure.
The commands chained using &&
will break execution if a previous command get failed.
The commands chained using ;
won’t break the full execution of the commands if any of the command breaks.
ls -l /non-exising-directory && echo "Hello World"
Output
ls: cannot access '/non-exising-directory': No such file or directory
The above command with &&
will stop executing by outputting the failed message.
ls -l /non-exising-directory; echo "Hello World"
Output
ls: cannot access '/non-exising-directory': No such file or directory
Hello World
The above command with ;
will continue execution if any of the command fails.
Find original file of a symbolic link
Symbolic link is a type of a file which points to a different file or a different folder. You can check the original file or folder of a symbolic link or symlink.
Below given the examples using ls -l
command and readlink
commands.
ls -l /etc/localtime
Output
lrwxrwxrwx 1 root root 34 Aug 3 16:48 /etc/localtime -> ../usr/share/zoneinfo/Asia/Bangkok
readlink /etc/localtime
Output
../usr/share/zoneinfo/Asia/Bangkok
Create Multiple Directories using a Single Command
You can create a new directory using mkdir
command. We can also use the single command to create multiple directories by enclosing them by {}
.
sudo mkdir folder-name
The above command creates new directory with the specified name.
sudo mkdir -p folder-name/{folder-1, folder-2, folder-3}
The above command creates all the specified directories easily in a single command.
Answer Yes or No in the Command
While executing some commands you will be prompted to enter yes or no to proceed. In such cases you can pass the answer with your command as shown below.
yes | sudo apt dist-upgrade
The yes
command echo’s y
, if you need to pass capital Y
you can use the below one.
yes Y | command
For passing N
you can still use the yes
command.
yes N | command
Prepare yourself for a role working as an Information Technology Professional with Linux operating system
Supercharge your Linux Administration Career with completed training course and get your dream job.
Conclusion
Now you have learned some most useful commands that will save your time in Linux command line usage.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.