19 Useful Linux Command Line Tips and Tricks

As you might have noticed, Linux is a very complex operating system. Because of this, it can get a little tedious to work with when there’s so much going on.

But that’s where the natural beauty about Linux shines; there exist many tricks and shortcuts to utilize in your command line for extra convenience.

This range of tricks encompasses handy commands and nifty hacks that one can easily forget if not fully committed to memory.

In this tutorial, we’ll show you a few of these Linux command line tips and tricks that you’ll definitely notice improving your efficiency while working in your Linux terminal. So, let’s begin.

 

 

Displaying an Output As a Table

When running commands in the Linux terminal, sometimes their outputs can be quite hard to follow. Overcrowded strings and cluttered information are the reasons for this.

Working through this can prove to be quite challenging, especially when you’re pressed for time.

These messy outputs particularly affect system administrators, as they need to efficiently execute operations in the Linux terminal and extract the information swiftly.

We can facilitate this best by utilizing an alternate separator such as colons with the -s option.

$ cat /etc/passwd | column -t -s :

Displaying an Output As a Table

 

How to Use the Bash While Loop Command

A question newer Linux users tend to ask quite frequently is how to repeat a command until it runs successfully without error in the Linux terminal.

These questions may concern operations such as pinging a server until it’s back up and running, checking if a file with specific extensions is uploaded to a particular directory, or even checking if a URL has become available.

To put it more simply: there are many system management scenarios where the while command can come in handy.

In the example below, we’ll show you how to use the while command to execute the read command and if that operation proves successful, the echo command in our while loop is executed as well, the output of which displays the lines contained in the file03.txt file.

Using the while loop command to execute the read command until the operation is successful.

 

Sorting by Processes by (Memory – CPU) Usage

In Linux, you can list all current processes and sort them by how much memory each process consumes using the ps aux command.

This command can be helpful for situations in which your system seems to be running on more memory than usual. Using the ps aux command by itself without any additional options will only list the current processes running on your system.

To sort this listing of processes by memory usage, we can use the sort command along with the -nk option by piping both commands together.

$ ps aux | sort -nk 4

As the title of this section of the article suggests, we can also use the ps aux command piped with the sort command and the -nk option to sort listed processes by CPU usage.

$ ps aux | sort -nk 3

There is also a pretty nifty little command that lets you check your system architecture; most systems these days are 64-bit, as it is objectively superior to 32-bit.

That being said, if you are wondering for any reason or simply confirming if you have a 64-bit or 32, you can utilize the command shown in the example below.

$ getconf   LONG-BIT

Using the getconf command to check your system architecture from your Linux terminal

 

Monitoring Multiple Log Files

Typically, when you think about using a command to monitor your logs, you think of the tail command. And that’s for a good reason, as the tail command is very reliable for checking log files.

But things get a little shakier when you try to monitor multiple log files simultaneously.

This is where the multitail command comes in really handy. It also comes fully equipped with text highlighting, filtering, and other features that any system administrator or general Linux user may need.

$ multitail

The multitail command by default is not installed by default on some Linux distros. So, you may get an error when trying to run this command. You can simply install it in the Linux terminal using the syntax below.

$ multitail file01 file02

Using multitail to monitor both file01 and file02

 

Returning to the previous directory

While command may not be a trick necessarily, it is still a massively helpful tool some may forget. But on the other side of the coin, many other users use this command every single minute.

So, it exists in an awkward state of being heavily underutilized and heavily relied on.

To go back to your previous directory, simply type the cd command along with the -- option.

$ cd --

Using the cd command with the -- option to return to the previous directory

There is also an alternative to this command that you can use to return to the previous directory in which you were working.

You can use both these commands interchangeably with exactly zero differences between the both of them. The alternative to the cd command with the -- option is the cd command with the .. option.

$ cd ..

Using the cd command with the .. option instead of the -- option to return to the previous working directory.

 

Monitoring Command Outputs

We can use the watch command to monitor the output of a command, running that command periodically with a certain set time interval. An excellent example of how we can use the watch command is by using it with the date command.

Using the syntax below in your Linux command line will cause the watch command to run the date command every 2 seconds and the output of which will be displayed in your terminal.

This is useful for continuously running the date command without needing to type it over and over.

$ watch date

Using the watch command to run the date command and display its outputs continuously.

 

Running Programs After Killing a Session

Closing your shell after running a program will immediately kill it. This limits the amount of multitasking capable on your system. Luckily, there’s a way around it. We can use the nohup command to achieve this.

This allows for maximum convenience and an optimal environment for all your system administrating and multitasking purposes. The nohup stands for “no hangup”. So, the function of the command is all in the name.

$ nohup wget site.com/file.zip

This command is one of the most useful Linux command line tricks for most web admins.

Using the nohup command to keep programs running after killing a session

This command will generate a file in the same directory with the name nohup.out and contains the output of the running program.

Using the nohup command to create a file that contains the output of a running program to avoid killing

Fantastic command, right?

 

Answer bot using yes & no commands

In Linux, some commands can be run interactively, most commonly by using a -i option in conjunction with the command.

Upon running these commands with the interactive arguments, a confirmation prompt will ensue. This prompt will request you to type “y” or “n.”

Sometimes this can get tiresome and even tedious for commands that request confirmation even without the interactive -i option.

If you ever felt like there was just no way around this, and you’ll have to get over it, you’re in for a real treat, as Linux is great with finding ways to get around little bumps in hurdles preventing you from working efficiently.

That is why in this post, we are listing various tips and tricks like this one so you can fully utilize Linux most optimally; the yes and no command can be used to automate either response to any command that asks for confirmation in order to be run.

$ yes no | command

Using the yes no command to automate responses to commands that prompt confirmation.

 

Autocompleting Using Tab

In Linux, it may sometimes get arduous to continuously be required to type many commands and strings over and over again, slowing your work down and just making things quite tedious.

But there exist some helpful techniques and methods to get around problems like this.

One such technique would be utilizing the tab key to autocomplete possible options based on what you’ve typed so far. Tabbing again further upon autocompletion, it will list all potential strings below in your Linux terminal.

This technique allows for more efficient and speedy work to be done. So, be sure to commit it to memory as it is not as grand or readily noticeable if you’re pretty new to Linux. Thus it may be easy to miss or forget.

Using the tab key to autocomplete strings in your Linux terminal

 

Run multiple commands in one line (concatenate commands)

It is very often in Linux that you have run commands, obviously. And that’s no problem, really, until, of course, this frequency becomes so high that you are required to run multiple commands at the same time.

Constantly having to run a command and type up another over and over again just slows you down.

That’s why in this post about Linux command line tips and tricks, we’ll show you exactly how you can avoid this problem and continue to work as efficiently as possible.

To run multiple commands in one string in your Linux command line terminal, simply use a semicolon to connect all of your commands and run them at the same time.

$ command [OPTIONS] ; command [OPTIONS]

Using a semicolon to connect commands and run them together in the Linux command line

 

Using the Arrow Keys to Retype Commands

Sometimes in Linux, you have to revisit commands you’d previously typed in your current terminal session. Having to do this can be the case for many reasons.

For example, if you are using the tail command on a text file, but you decide to edit it and tail it again, you might want to avoid having to type everything again.

In the example below, we’ll show you how we can utilize the arrow keys in your Linux terminal to retype commands you’ve run previously.

This trick is a very easy to miss but equally essential and valuable command that you should absolutely commit to memory.

Using the arrow keys to retype commands you have run previously

In the screenshot above, we used the up arrow key to navigate to the clear command in our command line history. We were able to use that command to clear the terminal without having to type out the entire command.

You can see how this can be useful for much longer commands than the clear command.

 

Recording Your Command Line Session

If you want to record what you’ve typed in your shell screen, you can use the script command, which will save all of your typing to a specified file or a file named typescript by default.

$ script [FILENAME]

Once you type exit, the script command will write all of your commands to that file so you can review them later.

Using the script command to record your command-line sessions

 

Replacing spaces with tabs

You can use the tr command to replace any character with another character. This command is convenient for automating processes like the one in the example below.

Having to replace characters manually can take up a significant amount of time that one could spend working on other things. Use the syntax below for replacing spaces with tabs with the tr command.

$ cat file.txt | tr ':[space]:' 't' > out.txt

Replacing spaces with tabs with the tr command

 

Convert character case

We can use the tr command to convert the content of a file to uppercase characters. The example below will show you how to use the syntax for this command.

$ cat file.txt | tr a-z A-Z > output.txt

Using the tr command to convert the content of a text file to uppercase characters

 

Utilizing the Powerful xargs Command

We can say that the xargs command is one of the most essential Linux command line tricks. You can use this command to pass outputs between commands as arguments. For example, you can use the xargs command with the touch command to create multiple .txt files.

$ echo file01 file02 | xargs touch

The text filenames typed following the echo command are passed to the end of the xargs command, which tells the touch command to run and create those files.

 

Closing Thoughts

These are only a few of the Linux command line tricks, but there are many more geeky things that you can do using other commands like the awk command and the sed command.

Thank you for reading and keep coming back for more!

17 thoughts on “19 Useful Linux Command Line Tips and Tricks
    1. You are welcome.
      I’m preparing a new post for Linux command line tricks soon.
      Stay tuned. Good Luck.

  1. Quick correction: you need to quote “.png” in the find command to suppress glod expansion

  2. I read §while :§ is faster than §while true§, so I use that instead. It’s harder to remember though.

      1. It’s been yeeeears since I read it, so I don’t remember if there were any benchmarks in the first place, and I haven’t done those benchmarks since I’m not quite in the clear about how.

        I think the person said something like that the colon has a special meaning which makes it faster to resolve than §true§ or even §1§. Not sure, though, but could be you can’t use §while 1§ in Bash.

        1. The current version of bash has a different performance than the older versions of bash.

  3. I’ve been using that “while true” loop syntax for 20 something years to “watch” file transfer progress (e.g. “while true ; do clear ; ls -al bigfatfilecopy ; sleep 1 ; done”)…. and press Ctrl+C to end it… never even knew about “watch” command until recently…

      1. Never too old to learn new stuff! I only learned about ~.ssh/config file last year!

Leave a Reply

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