15 Essential Linux Command Line Tips and Tricks

Linux is the operating system that powers the web. It’s essential for a software developer to at least have an idea of how Linux works and how to use it. In this article, you’ll find some insights into the Linux command line.

Before we continue, I’ll be assuming you’re using bash. This is the command interpreter that ships by default with all major distributions. Unless you’ve 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’s a trick that hardly anyone knows about: If you enter cd without anything after, it will still take you to your home directory:

4. Searching

It’s widely known that you can scroll through your history with arrows. Developers are so lazy, we’d 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’s 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 don’t work as usual in the Linux terminal. Most often, these are replaced with Ctrl+Shift+C and Ctrl+Shit+V. This is because Ctrl+C is already reserved for terminating the currently running program.

7. 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. You’ll be prompted for the password for the server and the public key will be copied. Now, you can log in to this server without a password from this particular system.

Note: This method is by no means less secure than your regular authentication. It may even be 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 the 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 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 [email protected]:~/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 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 isn’t best practice but sometimes there’s 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 are unable to enter exit.

12. Shred Files

If you value privacy, this one’s 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’re one of the people using Vim, I admire you. You’ll 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’re 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

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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s