Skip to content

Linux Command Quick Reference

Find Files

Search Specific Types of Files that Contains a Keyword

# Case sensitive
grep -r -e <RegExpPattern> --include=*.{cpp,h}
grep -r -e <RegExpPattern> --include="*.cpp" --include="*.h"

# Case insensitive
grep -r -i -e <RegExpPattern> --include=*.{cpp,h}

Note: The following syntax does not work

grep -r -e <RegExpPattern> --include="*.{cpp,h}"

Find all the files under path/to/dir that are modified within N days

find path/to/dir -mtime -N -ls

Find all the files under path/to/dir that are modified older than (N+1) days

find path/to/dir -mtime +N -ls

Find all .cpp and .h files whose permission is rw for user

find . -type f -iregex ".\*\\.\\(cpp\\|h\\)" -perm -u=rw

Search a Binary File or UTF-16 File

Convert utf-16 file to utf-8 before using grep

iconv -f utf-16 -t utf-8 file.txt | grep -e <RegExpPattern>

Search a binary file as text

grep --text -e <RegExpPattern> file.bin

Split a String (e.g. $PATH) into Multiple Lines

echo $PATH | tr ':' '\n'

Count the lines of CPP codes in a folder

find . -name '*.cpp' -o -name '*.h' | xargs wc -l

Use of xargs

Delete all the .c files

find . -name "*.c" -print0 | xargs -0 rm -rf

Append .bak to all the .c files

find . -name "*.c" -print0 | xargs -0 -I '{}' mv '{}' '{}'.bak

Find all .c files that contain string 'stdlib.h'

find . -name '*.c' | xargs grep 'stdlib.h'

Use of sed

(To be edited)

Reading Binary file

Print the first 44 bytes in hex, starting from 20th byte, 16 bytes each line

xxd -s 20 -l 44 -c 16 MyFile.bin

Reading text file/pipe

Print the first 3 lines or the last 3 lines of the file

head -n 3 MyFile.txt
tail -n 3 MyFile.txt

Print the first 100 bytes or the last 100 bytes of the file

head -c 100 MyFile.txt
tail -c 100 MyFile.txt

Print only the first 3 ls -l result

ls -l *.txt | head -n 3

Diff the STDOUT of two commands

diff <(command1) <(command2)
diff <(ll *.wav) <(ll *.txt)