Essential Linux commands

Essential Linux commands

Here is a list of some must-know commands in linux with examples:

Useful grep options:

  • Option -v, will display all the lines except the match. In the example below,
    it displays all the records from /etc/password that doesn’t match John.
    Note: There are several lines in the /etc/password that doesn’t contain the
    word John. Only the first line of the output is shown below.
# grep -v John /etc/passwd
jbourne:x:1084:1084:Jason Bourne:/home/jbourne:/bin/bash
  • Option -c shows the number of lines that the pattern matches.
# grep -c John /etc/passwd
2

You can also get the total number of lines that did not match the specific
pattern by passing option -cv.

# grep -cv John /etc/passwd
39


  • Use option -r (recursive) to search all subdirectories for a text matching a specific pattern. In the example below, it will search for the text “John” by ignoring the case inside all the subdirectories under /home/users.
    This will display the output in the format of “filename: line that matching the
    pattern”. You can also pass the option -l, which will display only the name of
    the file that matches the pattern.
# grep -ri john /home/users
/home/users/subdir1/letter.txt:John, Thanks for your
contribution.
/home/users/name_list.txt:John Smith
www.thegeekstuff.com
/home/users/name_list.txt:John Doe

# grep -ril john /root
/home/users/subdir1/letter.txt
/home/users/name_list.txt

Find Command

The following command looks for all the files under /etc directory with mail
in the filename.

# find /etc -name "*mail*"

The following command will list all the files in the system greater than
100MB.

# find / -type f -size +100M

The following command will list all the files that were modified more than 60
days ago under the current directory.

# find . -mtime +60

The following command will list all the files that were modified in the last
two days under the current directory.

# find . –mtime -2

How to delete all the archive files with extension *.tar.gz and
greater than 100MB?
Please be careful while executing the following command as you don’t want
to delete the files by mistake. The best practice is to execute the same
command with ls –l to make sure you know which files will get deleted when
you execute the command with rm.

# find / -type f -name *.tar.gz -size +100M -exec ls -l {} ;
# find / -type f -name *.tar.gz -size +100M -exec rm -f {} ;

The following command finds all the files not modified in the last 60 days
under /home/jsmith directory and creates an archive files under /tmp in the
format of ddmmyyyy_archive.tar.

# find /home/jsmith -type f -mtime +60 | xargs tar -cvf
/tmp/`date '+%d%m%Y'_archive.tar`

Xargs Command

xargs is a very powerful command that takes output of a command and pass it
as argument of another command. Following are some practical examples on
how to use xargs effectively.
1. When you are trying to delete too many files using rm, you may get error
message: /bin/rm Argument list too long – Linux. Use xargs to avoid this
problem.

# find ~ -name ‘*.log’ -print0 | xargs -0 rm -f

2. Get a list of all the *.conf file under /etc/. There are different ways to get
the same result. Following example is only to demonstrate the use of xargs.
The output of the find command in this example is passed to the ls –l one by
one using xargs.

# find /etc -name "*.conf" | xargs ls –l

3. If you have a file with list of URLs that you would like to download, you can
use xargs as shown below.

# cat url-list.txt | xargs wget –c

4. Find out all the jpg images and archive it.

# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

5. Copy all the images to an external hard-drive.

# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

You may also like