Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

1 CheatSheet: Linux Files


PRs Welcome

File me Issues or star this repo.

1.1 Check file

Show file contentcat /etc/hosts
Show file content with line numberscat -n /etc/hosts
Show with line numbers, excluding blank linescat -b /etc/hosts
Show the first 3 lineshead -n3 /etc/hosts
Show the first 20 byteshead -c20 /etc/hosts
Show the last 3 linestail -n3 /etc/hosts
Show the last 20 bytestail -c20 /etc/hosts
Keep tailing log filetail -f /var/log/system.log, taif /var/log/system.log
Show file starting from 4th linemore +4 /etc/hosts
Show the 4th linesed -n '4p' /etc/hosts
Show 4th, 5th, 7th and 8th linessed -n '4,5p;7,8p' /etc/hosts
Show matched string with 3 lines before and aftergrep -C 3 "" /etc/hosts
For table-like file, show 2nd columnawk -F'\t' '{print $2}' /etc/hosts
For table-like file, swap 1st and 2nd columnsawk -F'\t' '{print $2,$1}' /etc/hosts
Find file encodingfile -i /var/log/corecaptured.log

1.2 Find file

Basic findfind /home/mac/<myfolder> -name “my*.log”
Find files with two patternsfind . -iname “my*.log” -o -iname “my*.txt”
Find folder old than 3 daysfind . -maxdepth 1 -type d -ctime +3
Find files changed within 60 minutesfind /var/log -mmin 60 -type f
Find with ls detailsfind . \( -iname \) -ls
Find files filtered by sizefind /var/log -type f -size +50k -size -100k
Find files older than another filefind . -newer /tmp/file
Find files while excluding patternsfind . -name "*.log" -prune -o -name ".git" -prune -o -type f -print0

1.3 Find and delete

Find and delete with given namesfind . -name Thumbs.db -delete
Recursively delete empty foldersfind . -type d -empty -delete
Delete files haven’t been updated in 5 daysfind . -mtime +5 -exec rm {} \;
Delete folders created older than 5 daysfind . -name "npm-*" -type d -ctime +2 -exec rm -rf {} +

1.4 Watch file

Show file changeswatch -d -n 1 stat /var/log/message
Keep tailing log filestail -f /var/log/system.log, taif /var/log/system.log

1.5 Copy file

Copy one filecp /etc/hosts /tmp/hosts
Copy one foldercp -r /usr/local/bin/ /tmp/bin/
Copy for backupcp /tmp/hosts{,.bak}, ls -lth /tmp/hosts*
Create a copy but ask confirmation for overwritecp -i ~/foo.txt /tmp/foo.txt
Create a copy for backup with timstamp as suffixcp myfile.txt{,."$(date +%Y%m%d-%H%M%S)"}
Copy files by checking timestamprsync -av $src_dir $dest_dir
Copy files by comparing checksumrsync -avc $src_dir $dest_dir

1.6 Watch file

Remove a file or folderrm -rf <path>
Remove a file or an empty folderrm -f <path>
Remove a file by inodefind <path> -inum 5555 -exec rm -i '{}' \;

1.7 Diff File

Diff two filesdiff <file1> <file2>
Ignore uppercase and lowercasediff -i <file1> <file2>
Diff output of two commandsdiff <(date) <(somecommand)
Generate patch from two filesdiff -Naur <file1> <file2> > diff.patch
Diff two directoriesdiff -r <dir1> <dir2>
Show only brief summarydiff -r --brief <dir1> <dir2>

1.8 Make directory

Make directory. Report error, if existingmkdir foo
Make directory. Avoid reporting error, if existingmkdir -p foo
Make directory and its parentsmkdir -p foo/bar/dir1
Make directories with the hierachymkdir -p foo/{bar,bad/{dir1,dir2}}, tree foo

1.9 More Resources

License: Code is licendiff under MIT License.

You can’t perform that action at this time.