linux command line tricks
Linux

Forgotten Linux Command Line 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 tricks that you might forget or maybe new to you, so let’s get started.

 Display Output as a Table

Sometimes when you see the output of a command, it might be painful to read the output well due to the overcrowded strings, for example, the output of the mount command, what about viewing the output like a table?  It is an easy job.

mount | column t

mount table view

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 :

users tabular view

 Repeat a Command Until It Runs Successfully

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 at specific directory or maybe check if a specific URL becomes available or maybe any geeky thing, the list is very long.

You can use the while true loop to achieve that:

repeat till success

Where  >/dev/null 2>&1 redirects the output of your program to /dev/null, including both the Standard Error and Standard Out.

Actually, this is one of coolest Linux Command Line Tricks for me.

Sort Processes by (Memory – CPU) Usage

ps aux | sort -rnk 4

sort by memory usage

Sort processes by CPU usage.

sort by cpu usage

ps aux | sort -nk 3

Check Your Architecture

getconf  LONG_BIT

Watch Multiple Log Files at the Same Time

You can use the tail command to watch your logs and that’s fine, but sometimes you may need to watch multiple logfiles simultaneously to take some action and this is very important in many cases.

Using multitail command which supports text highlighting, filtering, and many other features that you may need.

multitail command

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 back to the previous directory.

Make Non-Interactive as Interactive Shell Session

To do this put our settings in ~/.bashrc  from ~/.bash_profile, so you will have the same environment for interactive and non-interactive.

Monitor Command Output at Regular Intervals

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 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 hang up.

nohup wget site.com/file.zip

This command is really one of the most useful Linux command line tricks for most webmasters.

nohup command

A file will be generated in the same directory with the name nohup.out contains the output of the running program.

nohup output

Cool command right?

Automatically Answer Yes to Any Command

It’s like an answer bot for those commands whose require the user to say yes.

That can be done using the yes command:

yes | apt-get update

Or maybe you want to automate saying no instead, this can be done using the following command:

yes no | command

yes command

Create a File With a Specific Size

You can create a file with a specific size using dd command:

dd if=/dev/zero of=out.txt bs=1M count=10

This will create a file with 10-megabyte size filled with zeros.

dd command

Run Your 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 !!

sudo command

Record you 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 typings 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 tr command which is very handy.

cat geeks.txt | tr ':[space]:' '\t' > out.txt

This command will replace the spaces with tabs.

tr command

Convert a File to Upper or Lower Case

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

Powerful xargs command

We can say that xargs command is one of the most important Linux command line tricks, you can use this command to pass the output of a command to another command as an argument, 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 images.tar.gz

Or maybe you have a list of URLs in a file and you want to download them or process them in a different way:

cat urls.txt | xargs wget

xargs command

Keep in mind that the output of the first command passed at the end of xargs command.

What if your command needs the output in the middle?

Easy

Just use {} combined with –i parameter to replace the arguments in the place where the output of the first command should go like this:

ls /etc/*.conf | xargs -i cp {} /home/likegeeks/Desktop/out

This is 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 what we’ve forgotten.

Thank you.