Linux Terminal Tricks Every Linux Enthusiast Must Know!

Filed Under: UNIX/Linux
Linux Terminal Tricks

Time to learn some cool Linux terminal tricks. As a Linux user, the terminal is going to be your best friend and it is very important that you know your way around it and learn to do some of the very cool tasks from the terminal.

Recommended read – Top 50+ Linux commands

Linux Terminal Tricks for Enthusiasts

Let’s go over some the tricks that you must know as a complete enthusiast.

1. Hide Commands From Terminal History

Sometimes you might not want to include some lines into your command history. This may be while echoing sensitive information or when you are trying to be sneaky. To do so, simply add an extra white space before whatever command you are typing.

For Example, “echo Message” becomes ” echo Message” . Notice the extra white space before echo ? That hides our command from being logged into history.

$ history
history
shutdown now
cargo build
vim src/main.rs
vim Cargo.toml
cargo run
vim main.rs
cd program-4/src/
cd Program-4/
cd Code/Learning-Rust/

$  echo "No History For This"
No History For This

$ history
history
history
shutdown now
cargo build
vim src/main.rs
vim Cargo.toml
cargo run
vim main.rs
cd program-4/src/
cd Program-4/
cd Code/Learning-Rust/

2 .Reuse Arguments From Previous Terminal Command

This is another handy shortcut in Linux which involves reusing the arguments from the last supplied commands using !$

This nifty little trick can come in handy in situations like when you create a directory and cd into it or while reusing similar compiler flags. Jut instead of specifying the flags, replace it with “!$” as shown :

$ pwd
/tmp

$ mkdir Test/

$ cd !$
cd Test/

$ pwd
/tmp/Test

3. Re-run The Last Terminal Command

How often has it happened that you typed in a long command in your terminal and it failed because you forgot to add that sudo infront of it? Well to help you with that annoying thing, Linux has a trick up its sleeves.

Whenever you need to run the last command with sudo just type in:

$ sudo !!

For example :

$ apt update
Reading package lists... Done
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
$ sudo !!
sudo apt update
Get:1 http://deb.debian.org/debian bullseye InRelease [146 kB]
Get:2 http://deb.debian.org/debian bullseye/main amd64 Packages.diff/Index [63.6 kB]                                  
Get:3 http://deb.debian.org/debian bullseye/main Translation-en.diff/Index [63.6 kB]                                  
Get:4 http://deb.debian.org/debian bullseye/main amd64 Packages T-2021-04-17-1400.52-F-2021-04-07-0202.42.pdiff [223 kB]
Get:4 http://deb.debian.org/debian bullseye/main amd64 Packages T-2021-04-17-1400.52-F-2021-04-07-0202.42.pdiff [223 kB]
Get:5 http://deb.debian.org/debian bullseye/main Translation-en T-2021-04-17-0200.56-F-2021-04-08-0201.52.pdiff [3,706 B]
Get:5 http://deb.debian.org/debian bullseye/main Translation-en T-2021-04-17-0200.56-F-2021-04-08-0201.52.pdiff [3,706 B]
Fetched 500 kB in 12s (41.6 kB/s)                                                                                     
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
84 packages can be upgraded. Run 'apt list --upgradable' to see them.

Infact you can use this trick to run the last command with anything, not just sudo!

4. Shortcuts To Navigate The Filesystem

Following are some of the little tricks which can aid you navigate the file system in Linux :

cd ../Move up a directory
cd ~Move to the User’s home directory
cd /Move to the root folder
cd –Move to the User’s home directory

You can also use these tricks to navigate to certain directories quicker. Like instead of doing:

$ cd /home/user/Pictures

You can simply write:

$ cd ~/Pictures

5. Record Your Session As A Typescript

Sometimes you might want to store all your terminal output in a file which you can refer to later and the “script” command lets you do just that. It creates a typescript of everything on your terminal session. The terminal data are stored in raw form to the log file and information about timing to another (optional) structured log file.

To start recording, you simple need to time the following in a terminal :

$ script

To stop the same, all you need to do is type :

$ exit

Upon exiting, you should find a file called “typescript” in the directory where script was launched and upto printing it out to the terminal, you can see everything that you typed out on the terminal including their output !

Recording Terminal Session Using Script
Recording Terminal Session Using Script

6. Renaming/Moving Files With Suffixes

If you want to quickly rename some files by adding a suffix to them , you can use the following syntax:

$ ls
result.txt
$ cp ./result.txt{,-old}
$ ls
result.txt result.txt-old

Combined with a bit of regex, this can be quite handy especially while renaming large group of files.

7. Convert Text Documents To PDFs using the Linux terminal

Another very handy Linux trick which can come in really handy is the ability to convert text files into pdfs. This can be done very simply using libreoffice!

Assuming that you already have libreoffice installed, you can simply convert your text documents into PDFs using:

$ libreoffice --convert-to "pdf" [TEXT FILE]
Text Converted Into PDF
Text Converted Into PDF

Conclusion

Hence in this module, we covered some cool Terminal Kung-Fu and tricks to have up your sleeve, especially if you are going to work with the terminal. Even though some of these seem trivial, gradually you will understand how instrumental these are towards bettering your workflow.

Comments

  1. Ashok Arora says:

    `$ sudo !!` command works only on bash and zsh. fish shell doesn’t have a concept of the last executed command.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content