Linux Tips and Tricks

Linux terminal can seem quite overwhelming for new users and even for experienced users without the knowledge of Linux tips & tricks. Linux is a very flexible operating system.
However, remembering all of the instructions and how to use them is challenging. With these excellent tips, you’ll be able to utilize Linux like a pro in no time!

This list of tips includes useful commands and clever hacks that are easy to forget if not committed to memory.

We’ll teach you a few of these Linux tips and tricks in this article, and you’ll notice a significant improvement in your productivity while working in your Linux terminal. So, let’s get started.

1. Removing Larger Files

Files can become “heavy” as a result of poor administrative skills, and can be as huge as 250 GB! Because of the large amount of data involved, the rm function isn’t very useful in this case.

Therefore, removing a single log file of that size using rm utility should be avoided. You should rather opt for an easier solution:

 > /path-to-file/huge_file.log

You’d have to adjust the file names and the path to meet your circumstance. As a result, for that particular file, the empty output will be generated.

2. Copying in Multiple Directories

In general, when you want to copy a file, you use cp command which looks like this:

cp /path-to-file/my_file.txt /path-to-new-dir

Right? What if you want to copy that into multiple directories? You usually go for something like this:

cp /home/user/my_file.txt /home/user/1
cp /home/user/my_file.txt /home/user/2
cp /home/user/my_file.txt /home/user/3

It is time consuming and frustrating to write these commands over and over again. What if we told you that you could still complete this task with a single command? You still don’t believe us? Consider the following:

echo /home/user/1/ /home/user/2/ /home/user/3/ | xargs -n 1 cp -v /home/user/my_file.txt

Voila! Your job is done.

3. Minimizing Keystrokes

The more keys you press on your keyboard, the longer it will take you to complete tasks. You can greatly boost your work efficiency if you are aware of some time-saving commands.

In order to execute your last command, you should make use of the UNIX bash shell.

Rather than typing the entire command over, press Ctrl+R and edit a few lines if necessary. This will save you a lot of time and help you complete your tasks faster.

4. Going Back

Everyone knows that you can go up a directory using cd .. But almost no one knows that with cd - you can go back to the previous directory:

5. Going Home

That is, most likely, the shortcut to your home folder. But there’s a trick that just a few people are aware of: It will still send you to your home directory if you type cd without any other characters following it:

6. Searching Files Made Easy

This may seem easier than you think. Following is an example of the command used for searching files:

find /home/user -type f

This command will locate all files in the /home/user directory. This is a powerful order, however you could want to narrow it down and make it more targeted.

For instance, let’s suppose you want to include an option to search files greater than 10 MB. You can do this by:

find . -type f -size 10M

Be careful not to use the root directory, failing which can cause high I/O on the system.

7. Turning Off Your System

Did you know that you can set your system to switch off at a specified time using certain commands? You may or may not be physically present when your computer is turned off, but you have control over when it is done. Use this command to set the shutdown time:

sudo shutdown 21:00

Your system will shut down exactly at 21:00! Instead of hours, you can also choose minutes.
For example:

sudo shutdown +15

Your computer will automatically turn off after 15 minutes.

8. Authenticate to SSH Without a Password

If you often log in to a certain SSH server, it can be annoying having to enter a password every time. You can skip it if your host and the server exchange certificates.

First, you have to generate one. Run the command ssh-keygen. This creates a private/public key pair and saves it to ~/.ssh/id_rsa. Now you need to copy the public key to server with this command: ssh-copy-id [email protected]_host.The password for the server will be requested, and the public key will be copied. This system now allows you to log in to this server without a password.

Note: This method is not in any way less secure than traditional authentication. If your local system is secure, it may even be safer. There will be no way to log into SSH unless the private key is compromised.

9. Keep Your Program Running in the Background

If you execute a program in the terminal, it will be terminated as soon as the terminal session is ended. Use the nohup command, which stands for “no hang up,” to avoid this and keep the program running.

For example, to transfer files to and from the server with scp, while being sure that the transfer will continue even if you accidentally close the terminal window, use this command:

nohup scp very-big-file.mkv :~/very-big-file.mkv

nohup also creates a file called nohup.out to save the output of the command.

10. Answer Yes

You might get upset if you develop bash scripts to automate specific chores because you have to say yes to every command you run. Prefix any command with yes | to bypass it and answer yes to any command:

yes | apt-get update

If you want to answer no instead, prepend it with yes no |.

11. Login as Root

This isn’t perfect, but there are occasions when you don’t have a choice. However, sudo su is the next best alternative. Su logs you in as root, and sudo executes commands as root. As a result, you won’t require the root password. Furthermore, some versions disallow root passwords, leaving you with only one option:

12. Show System Info

To show your system info in a beautiful way install and use the command screenfetch:

13. Using the Right Command

When you have a lot of command lines at your disposal, it’s difficult to remember them all. You must not only have the correct command in mind, but also be able to efficiently execute it.

What you are looking for is this:

#apropos

Just replace “description” with the actual command description that you are searching for.

Take a look at this example:

  • dir (1) – list directory contents
  • ls (1) – list directory contents

With this trick, you don’t need to recall the required command at all. You just need to search for one!

14. Executing Multiple Commands

Frequently, you must wait for the previous command to complete before proceeding to the next. This takes up a lot of your time once more.

There’s an efficient way to do it. You can use a single command to execute multiple ones and your waiting time is over!

The command looks like this:

command_1; command_2; command_3

This separator is a lifesaver when it comes to finishing your job within a stipulated time.

15. Multiple Commands: When First One Fails

We discussed how to perform many commands with a single command in the previous section. What should you do, though, if the first command does not work? If the previous command was successful, you just want to run the next one.

For this use “||” separator like shown below:

command_1 || command_2

After this, command 2 will run only after the command 1 when you use the above single-line command.

16. Creating Directory Trees

You generally use the mkdir command to create new directories in Linux.

The usual command for creating directories goes like this:

mkdir new_folder 

How about creating 7 sub folders within the new folder? Repeating the above command 7 times is not an ideal solution. You can instead use this command:

mkdir -p new_folder/{folder_1, folder_2, folder_3, folder_4, folder_5, folder_6, folder_7} 

With the help of the above command, you can easily create 7 subfolders without having to run mkdir multiple times.

17. Moving to End or Starting of Line

You’ve typed a long command, but you realize you need to go back to the beginning to make some modifications.

What do you do? Strike that left arrow key several times until you reach the starting of command?
There is a better way.

Apart from using End and Home keys, you can opt for Ctrl+E to reach the end and Ctrl+A to reach the beginning.

Conclusion

Mastering these tips and tricks will make your transition to Linux hassle-free.

Even if you are not a Linux specialist, you should be able to understand the aforementioned commands. That’s what makes it so appealing!

These useful tricks can work wonders for your efficiency. Wish you all the best for your Linux journey!

Also Check:

Leave a Comment