Useful Linux Commands

Directories & Files

  1. To create multiple directories in one command & give it required permission

mkdir -pm 755 dir1/dir2/dir3

This command will create dir1, then create dir2 inside dir 1, then create dir 3 inside dir2 with 755 permissions on all dirs.

  1. To find the oldest file

ls -lt

ls -ltr → In reverse order

This command will sort the files by their modification time, or the last time it was changed.

  1. To find the newest file

ls -lu

  1. To find when was inode changed i.e. permission modification etc

ls -c

This command will list the files, as per the last changes in inode.

  1. To list all subdirectories

ls -R

  1. To list all directories

ls -l | grep ‘^d’

Alias ld=’ls -l | grep ‘^d’’

  1. To sort by Size

ls -lS

  1. To list the file with file types

ls -lAF

  1. To list the ending characters of the file

ls -l | cat -vte

cat -e option marks the end of lines with a $

  1. To list the file name in qoutes

ls -Q

  1. To list all hidden files

ls -la

  1. To list all hidden files except . & ..

ls -lA

  1. Counting files by type

find ${*-.} -type f -print | xargs file | awk ‘{ $1=NULL; t[$0]++; } END { for (i in t) printf (“%d\t%s\n”, t[i], i); }’ | sort -nr

Find command and it’s usage:

  1. To find all files in the present directory

find . -print

  1. why xargs option is used

When we give command ls -ld `find . -print` , it might give error, when command line is too long

hence we can give xargs

find . -print | xargs ls -ld

  1. How to find a file older than X days

find .  -type f -mtime X  -print  (mtime: modified time)

  1. How to find file not accessed for X days

find . -type f -atime X -print

  1. How to use “OR” operator in find command

find . -atime +5 \ ( -name “*.o” -o -name “*.tmp” \ ) -print

  1. Time operators which can be used with “find” command

atime = access time (tells last access time)

mtime = access time (tells last modification time)

ctime = access time (tells last inode changes time)

  1. How to use find for exact  File time comparisons using find command

create files with exact times you would like to compare
touch -t 201109092100 file1

touch -t 201110102200 file 2

find . -newer file 1 \! -newer file 2 -print
This will list all the files created between these 2 dates.

-exec operator in “find” command
Often, when you find a file, you don’t just want to see its name; you want to do something, like grep for a text string. To do this, use the -exec operator. This allows you to specify a command that is executed upon each file that is found.
The -exec operator allows you to execute any command, including another find command. If you consider that for a moment, you realize that find needs some way to distinguish the command it’s executing from its own arguments. The obvious choice is to use the same end-of-command character as the shell (the semicolon). But since the shell uses the semicolon itself, it is necessary to escape the character with a backslash or quotes.
Therefore, every -exec operator ends with the characters \; There is one more special argument that find treats differently: {}. These two characters are used as the variable whose name is the file find found.
The difference between -exec and xargs is subtle. The first one will execute the program once per file, while xargs can handle several files with each process. However, xargs may have problems with filenames that contain embedded spaces

  1. how to delete strange file using -exec operator in “find”

find the inode number of the file using ls -il
find . -inum 163866 -exec rm {} \;

  1. how to search for all files in a folder with required word using “find” command

find . -name \*.log  -exec grep -n “root.conf” {} \;
grep can also search for the word “root.conf” but it’ll search in all files, not just only in *.log files

  1.  how to find all symbolic links in your home dir & prints the files to which your symbolic links point

find $HOME -type l -ls | awk ‘{print $NF}’

  1. To search file by size

to search file > 500Bytes  c=Size in bytes
find . -size 500c -print
to search files < 500 Bytes
find . -size -500c -print

  1. To search files by permission, user , group

find . -perm 755 -print
find . -user maq -print
find . -group maq  755 -print

  1. duplicating a directory tree using find command

find . -type d exec mkdir \usr\project\{} \;
using sed :
find . -type d -print | sed ‘s@^@/usr/project/@’ | xargs mkdir

find . -type d -print | sed ‘s@^@mkdir @’ | (cd /usr/project; sh)

  1. How to limit find to search only files at first level i.e. stop it from searching recusively

find . -maxdepth 1 -print

  1. How to limit find to search only files in one disk partition only

find /usr -size +10485760c -xdev -print

  1. fastest way to search files on linux

locate filename

  1. To search a particular word or string in all files in a directory & it’s subdirectory

egrep ‘string’ `find . -type f -print`
-bash-3.2$ egrep ‘string’ `find . -type f -print`

find . -type f -print -exec egrep ‘string’ \;
In case there’s only 1 file in the directory, where you are searching, the find will only print the string and not the file name, to resolve this
find . -type f -print -exec egrep ‘string’  /dev/null

  1. How to find the top 5 big files

find . -type f -exec ls -s {} \; | sort -n -r | head -5

  1. how to find all hidden files

find . -type f -name “.*”

  1. Using {} more than 1 time in find command

find . -name “*.txt” -exec cp {} {}.bkup \;

  1. Redirecting errors to /dev/null

find / -name “*.conf” 2>>/dev/null

  1. how to subsitute space with _ in file names

find . -type f -iname “*.txt″ -exec rename “s/ /_/g” {} \;
Linking, Renaming & copying files

To Create a symllink/softlink

ln -s filename linkname

  1. To create a hardlink

ln filename linkname

  1. Word anchors: To search a pattern at the start of line : ^

To search a pattern @ end of line: $

  1. To search line starting with string “abcd”

grep ‘\<abcd\>’ server.txt

  1. To find a string that is a separate word (enclosed by spaces), it is better use the −w

for example to search “/” fs in /etc/fstab

if you give grep / /etc/fstab .. it’ll show all file systems having /

if you give grep -w /  /etc/fstab.. it’ll show only /root file system

  1. To search for . or * in a file

grep -F ‘*’ abc.sh

  1. To search all directory starting with a,b,c or x,y,z

ls -ld [a-cx-z]*

  1. To search all directory except the directories starting with a,b,c or x,y,z

ls -ld [^a-cx-z]*

  1. To print lines containing a pattern

sed ‘/string/p’ textfile

  1. To print only the lines matching the pattern

sed -n ‘/string/p’ textfile

  1. To delete the line containing the pattern

sed -n ‘/string/d’ textfile

  1. Find and replace with Sed

sed ‘s/errors/awesome’ abc.log

Note: This will change only 1st occurence of “errors” in the line

  1. Find and replace with Sed globally

sed ‘s/errors/awesome/g’ abc.log

  1. To insert a string “>” at the beginning of each line of a file

sed ‘s/^/> /’ abc.log

  1. To insert a string “EOL” at the end of each line of a file

sed ‘s/$/EOL/g’ abc.log

  1. To run multiple replace commands

sed ‘s/^/>/g’ -e ‘s/$/EOL/g’ abc.log
Keep in mind that by default sed prints its results to the standard output, most likely your terminal window. If you want to save the output to a file, redirect it

sed option ‘some/expression’ file_to_process > sed_output_in_a_file
AWK

How to print/display the first line of a file?

head -1 file.txt

sed ‘2,$ d’ file.txt

How to print/display the last line of a file?

tail -1 file.txt

sed -n ‘$ p’ test

How to display n-th line of a file?

sed –n ‘<n> p’ file.txt

Disclaimer:

All the trademarked and copyrighted content in my blog belong to their original owners and are included for representation purposes only.

Advertisements