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.
Table of Contents
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
$ cat /etc/passwd | column -t -s :
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
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
You can read more about process management from here.
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 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
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
$ cd --
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
$ cd ..
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
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
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.
This command will generate a file in the same directory with the name
nohup.out and contains the output of the running program.
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
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
no command can be used to automate either response to any command that asks for confirmation in order to be run.
$ yes no | command
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.
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 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.
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
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.
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
$ cat file.txt | tr ':[space]:' 't' > out.txt
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
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.
Thank you for reading and keep coming back for more!
Mokhtar is the founder of LikeGeeks.com. He works as a Linux system administrator since 2010. He is responsible for maintaining, securing, and troubleshooting Linux servers for multiple clients around the world. He loves writing shell and Python scripts to automate his work.