linux command line tricks
Linux

Forgotten Linux Command Line Tricks

We use Linux command line every day and we use many commands and we discussed some of them Linux commands and sometimes we know some tricks from the web but due to the little practicing maybe !! we forget some of those Linux Command Line Tricks that could make life easier in this article I’m going to list some of those tricks that you might forget or even new to you so let’s get started

Our points are:

Display output as a table

Repeat a Command until It runs successfully

Check your architecture

Watch Multiple Log Files at the Same Time

Return to your previous directory

make Non-Interactive as Interactive shell session

Monitor command output at regular intervals

Run you program after session killing

Automatically Answer Yes to Any Command

Create a file with specific size

Run your last command as root

Record you command line session

Replacing spaces with tabs

Convert a file to upper or lower case

Powerful xargs command

 Display output as a table

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

mount | column t

mount table view

Ok is this example we see the output well formatted because the separator between them are spaces

What are the separators are something else like colons : like the output of cat /etc/passwd

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 feature you will find a lot of questions that people ask how do repeat the command till it return success and runs correctly like ping the server till it becomes a live or check if a file with a specific extension is uploaded at specific directory or maybe check if a specific URL become existed or may any geeky thing, the list is very long

You can use while true loop to achieve that

repeat till success

Where  >/dev/null 2>&1 is redirected the output of your program to /dev/null. Include both the Standard Error and Standard Out

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

Sort processes by memory 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 may use tail command to watch your logs and it is fine but sometimes you may want watching multiple log files to take some action and this is very important in many cases

Using  multitail command which supports text highlighting and filtering and many more features that you may need

multitail command

You can install it if it is not found on your system

apt-get install multitail

Return to your previous directory

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 we have the same environment for interactive and non-interactive

Monitor command output at regular intervals

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 you program after session killing

When you run any program in the background and close you shell surely it will be killed by your shell, what about continue running the program after closing the shell

This can be done using nohup command which stands for no hang up

nohup wget site.com/file.zip

this command is really one of the forgotten Linux Command Line Tricks for most of webmasters because they use another command like screen

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

If you want to automate the process that requires user to say yes

That can be done using yes command

yes | apt-get update

or maybe you want to automate saying no instead, this can be done using

yes no | command

yes command

Create a file with 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 10 megabytes size file filled with zeros

dd command

Run your last command as root

Sometimes you forget to type sudo before your command that requires root privileges, 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 type don your shell screen you can use 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 it later

Replacing spaces with tabs

You can replace any character with any other using tr command which is very handy

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

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 like the following to replace arguments in the place where the output of the first command should go

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 did not 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 all remember what we forget