15 Essential Linux Command Line Tips & Tricks

Linux is the operating system that powers the web. It is essential for a software developer to at least have an idea how Linux works and how to use it. In this post, you will find some insight into the Linux command line.

Before we continue: I will be assuming you are using bash. This is the command interpreter that ships by default with all major distributions. Unless you manually changed it, it should be bash.

1. The !!

How many times has it happened to you: after entering and running a long command you find out you forgot to add sudo at the beginning. Well, you can just enter sudo !! and the command line will replace !! with the last command you tried to run:

2. 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:

3. Going home

You probably know that ~ is the shortcut to your home folder. But there is a trick that hardly anyone knows about: if you enter cd without anything it will still take you to your home directory:

4. Searching

It is known that you can scroll through your history with arrows. Developers are so lazy, we would rather press the up arrow 15 times to find that ls we had somewhere. But this can be achieved a lot easier with the reverse search function. Press Ctrl+R and start typing the command, the reverse search fill find the closest match in your recent history:

5. Reuse an argument

Another handy trick is the !$ shortcut. It will be replaced by the arguments of the previous command. It is useful, for example, when you create a folder and want to cd into it:

6. Copy and paste

You probably noticed that Ctrl+C and Ctrl+V do not work as usual in the Linux terminal. Most often, these are replaced with Ctrl+Shift+C and Ctrl+Shit+V. This is done because Ctrl+C is already reserved for terminating the currently running program.

7. Authenticate to SSH without a password

If you login to a certain SSH server often, 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. It will create a private/public key pair and save it to ~/.ssh/id_rsa. Now you need to copy the public key to server with this command: ssh-copy-id username@remote_host. You will be prompted for the password for the server and the public key will be copied. Now, you will login to this sever without a password from this particular system.

Note: this method is by no means less secure than your regular authentication, maybe even more secure, if your local system is secured. Unless you compromise the private key, there will be no way to login to SSH.

8. Keep your program running in the background

If you run a program in terminal, it will be killed as soon as you end this terminal session. To prevent this and keep the program running, use the nohup command. It stands for no hang up.

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

nohup scp very-big-file.mkv mk@remote-server:~/very-big-file.mkv

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

9. Answer yes

If you write bash scripts to automate certain tasks, you may be frustrated with entering yes to every damn command you run. To skip it and answer yes to any command, prepend it with yes | like this:

yes | apt-get update

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

10. Login as root

This is not a best practice, but sometimes there is no choice. However, the next best option is to use sudo su. The su command logs you in as root, the sudo will execute is as root. Therefore, you do not need the root password for it. Moreover, some distributions disable root password, so this is your only option:

11. Logout

The fastest way to logout from SSH, SFTP, root, or from the terminal session altogether is the Ctrl+D shortcut. It comes in handy when you handle a lot of SSH connections or not able to enter exit.

12. Shred files

If you value privacy, this one is for you. The rm command is widely used to delete files, but it does not delete them completely. Even after deleting, it possible to extract data using special software. To completely delete the file and fill the space it was using with zeros, use the shred command. Use it like this: shred -zvu <filename>.

13. Password protect files in VIM

If you are one of the people using Vim, I admire you. You will be pleased to learn that you can password protect files in Vim using the vim +X filename command, or :X command directly in Vim.

14. List users

If you have privacy concerns, you might want to check who is logged in on a system at any point in time. You can use the w command to list all users currently in the system. Moreover, you can write a script that will run this command on a schedule and email you if something is out of line.

15. Show system info

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

16. Bonus

If you are feeling lonely, Linux can spawn an awesome cow that can talk to you. To say Hello world, for example, use echo "Hello world" | cowsay:

Closing notes

Thank you for reading, I hope you liked this article. Let me know about your favorite Linux hacks in the comments!


Get new content delivered to your mailbox:

leave a comment