Linux Useful Commands

Useful Command Lines

25/02/2021

I. Cat

Read a text-like file
Example:

1$ cat file.txt | grep "pattern" | -A number_of_after_line -B number_of_before_line -C both_before_and_after_lines

II. Find

Find one or more files base on pattern

  • return all the file and directory locate in a dir
1$ find ~/Hd7/
  • return only file (or dir)
1$ find ~/Hd7/ -type f

or

1$ find ~/Hd7/ -type d
  • return a file with specific name
1$ find . -type f -name "test.txt"
  • return a file match the pattern
1$ find . -type f -name "*.py"

case insensitive: -iname

  • return files which are modified in from 2 to 10 minutes ago
1$ find . -type f -mmin -10 -mmin + 2

+10 mean more than 10 minutes ago
-mtime apply for day instead of minute

  • return files over 5M
1$ find . -size + 5M

after that we can list all file in that directory

1$ ls -lah path 
  • return all empty file/dir
1$ find .  -empty
  • return file/dir have permission
1$ find . -perm 777
  • execute another command on result of a find command
1$ find path -name "*.py" -exec rm {} +
  • exclude in find
1$ find ./codeforces -type f ! -name "*.*"
  • find default apply for all subdirectory, to only apply for 1 level directory, use -maxdepth
1$ find . -type f -name "*.jpg" -maxdepth 1

III. du (Disk Usage)

Example

  • show size of current directory in human-readable output
1$ du -shc
2$ du -s .[^.]*

IV. sed (Stream EDitor)

  • Find and replace text in a file
1sed -i 's/original/new/g' file.txt

-i: in-place (ie: save back to original file)
s: substitute command
g: global (ie: replace all and not just the firse occurence)

  • insert text in b.txt into a.txt at line 5 (-i to save back to orginal a.txt)
1sed -i '5r ~/path_to_b.txt' a.txt

V. stat

  • show statistics of a file or folder (size, permission, …)
1$ stat file.txt

VI. awk (Aho, Weinberger, and Kernighan)

1$ cat file.txt | grep "pattern" | awk '{print $1 $2}'

VII. xargs (eXtended ARGuments)

Read streams of data from standard input, then generate and executes command line, meaning it can take output of a command and passes it as argument of another command. If no command is speicified, xargs executes echo by default.

1$ ps -ef | grep "pattern" | awk '{print $2}' | xargs kill -9