16 Useful Linux Command Line Tips and Tricks
We use the Linux command line every day, and due to the little practicing, we may forget some of the Linux command line tricks. In this post, I’m going to show you some of these Linux command line tips and tricks that you might forget or maybe new to you, so let’s get started.
Table of Contents
- 1 Display output as a table
- 2 Run until success
- 3 Sort processes by (Memory – CPU) usage
- 4 Check your architecture
- 5 Monitor multiple log files concurrently
- 6 Return to your previous directory
- 7 Make non-interactive as interactive shell session
- 8 Watch command output
- 9 Run your program after session killing
- 10 Answer bot using yes & no commands
- 11 Create a file with a specific size
- 12 Run the last command as root
- 13 Record your command-line session
- 14 Replacing spaces with tabs
- 15 Convert character case
- 16 Powerful xargs command
Display output as a table
Sometimes it’s painful to read the output well due to the overcrowded strings, for example, the result of the mount command, what about viewing the output like a table? It is an easy job.
mount | column –t
OK, in this example, we see the output is well-formatted because the separator between them is spaces.
What if the separators are something else, like colons :
The /etc/passwd file is a good example.
Just specify the separator with -s parameter like this:
cat /etc/passwd | column -t -s :
Run until success
If you search Google about that trick, you will find a lot of questions about people asking how to repeat the command till it returns success and runs properly, like ping the server till it becomes alive or check if a file with a specific extension is uploaded to a particular directory or maybe check if a particular URL becomes available or maybe any geeky thing, the list is very long.
You can use the while true loop to achieve that:
We use > /dev/null 2>&1 to redirect normal output and errors to /dev/null.
This is one of the coolest Linux Command Line Tricks for me.
Sort processes by (Memory – CPU) usage
To sort by memory usage:
To sort by CPU usage:
Check your architecture
Using the following command, you can get your PC architecture.
getconf LONG_BIT
Monitor multiple log files concurrently
You can use the tail command to watch your logs, and that’s fine, but sometimes you may need to monitor multiple log files simultaneously to take some actions.
Using multitail command which supports text highlighting, filtering, and many other features that you may need.
You can install it if it is not found on your system like this:
apt-get install multitail
Return to your previous directory
It’s not a trick, but some people forget it, others use it every minute.
Just type cd – and you will return to the previous directory.
Make non-interactive as interactive shell session
To do this, put our settings in ~/.bashrc from ~/.bash_profile.
Watch command output
By using watch command, you can watch any output of any command, for example, you can watch the free space and how it is growing:
watch df –h
You can imagine what you can do with any variant data that you can watch using the watch command.
Run your program after session killing
When you run any program in the background and close your shell, definitely it will be killed, what about if it continues running after closing the shell.
This can be done using the nohup command, which stands for no hangup.
nohup wget site.com/file.zip
This command is one of the most useful Linux command line tricks for most webmasters.
A file will be generated in the same directory with the name nohup.out contains the output of the running program.
Cool command, right?
Answer bot using yes & no commands
It’s like an answer bot for those commands which require the user to say yes.
Y using the yes command:
yes | apt-get update
Or maybe you want to automate saying no; this can be done using the following command:
yes no | command
Create a file with a specific size
Use the dd command to create a file with a specific size:
dd if=/dev/zero of=out.txt bs=1M count=10
This will create a file with a 10-megabyte size filled with zeros.
Run the last command as root
Sometimes you forget to type sudo before your command that requires root privileges to run, you don’t have to rewrite it, just type:
sudo !!
Record 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 file named typescript.
script
Once you type exit, all of your commands will be written to that file so you can review them later.
Replacing spaces with tabs
You can replace any character with any other character using the tr command, which is very handy.
cat geeks.txt | tr ':[space]:' '\t' > out.txt
This command will replace the spaces with tabs.
Convert character case
cat my_file | tr a-z A-Z > output.txt
This command converts the content of the file to upper case using the tr command.
Powerful xargs command
We can say that 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 may search for png files and compress them or do anything with them.
find . -name "*.png" -type f -print | xargs tar -cvzf pics.tar.gz
Or maybe you have a list of URLs in a file, and you want to download them or process them differently:
cat links.txt | xargs wget
The cat command result is passed to the end of the xargs command.
What if your command needs the output in the middle?
Just use {} combined with –i parameter to replace the arguments in the place where the result should go like this:
ls /etc/*.conf | xargs -i cp {} /home/likegeeks/Desktop/out
These are only some of the Linux command line tricks, but there are some more geeky things that you can do using other commands like awk command and sed command.
If you know any geeky command I didn’t mention, you can type it in a comment and share it with others.
I’m going to make another post about those Linux command line tricks so we can remember all that we’ve forgotten.
Thank you.
“Repeat a Command until It runs successfully” could be done more succintly with `while ! ping -c 1 http://www.google.com > /dev/null 2>&1;do : ;done`
You can use it in many ways.
And this is the beauty of Linux commands.
Even: `while ! ping -c 1 http://www.google.com &> /dev/null ; do : ; done`
Cheers!
Thanks man! I have never known what the yes command is good for. 😀
You are welcome.
I’m preparing a new post for Linux command line tricks soon.
Stay tuned. Good Luck.
Quick correction: you need to quote “.png” in the find command to suppress glod expansion
Oh Sorry for the typo.
Now fixed.
Thank you.
I read §while :§ is faster than §while true§, so I use that instead. It’s harder to remember though.
I didn’t check that difference before. Did you benchmark it?
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.
The current version of bash has a different performance than the older versions of bash.
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…
Great to know that a guru like you reading our articles.
Never too old to learn new stuff! I only learned about ~.ssh/config file last year!
Good rule. Good man.
Thank you.
Very nice collection of Linux command trick!
Thanks!