# grepSearch for text
Syntax grep [options] pattern [file ...]
$ grep "error" app.logCopy
Lines that contain error
$ grep -i "timeout" app.logCopy
Ignore capital letters
$ grep -rn "TODO" src/Copy
Search every file in a folder, show line numbers
$ grep -v "^#" config.confCopy
Show lines that do NOT start with # (hide comments)
$ grep -c "404" access.logCopy
Just count the matching lines
$ ps aux | grep nginxCopy
Filter another command's output
# findFind files by name, size or age
Syntax find path [tests] [action]
$ find /var/www -name "*.php"Copy
Every .php file below /var/www
$ find . -type d -name "backup*"Copy
Folders whose name starts with backup
$ sudo find /var/log -size +100MCopy
Files bigger than 100 MB (sudo lets it look into every folder)
$ find . -mtime -1Copy
Files changed in the last day
$ find /tmp -name "*.tmp" -deleteCopy
Find and delete (test without -delete first)
$ find . -type f -exec chmod 644 {} \;Copy
Run a command on every file found
# locateFind files fast using an index
Syntax locate name
# whichShow where a command lives
Syntax which command
# whereisLocate a program, its manual and source
Syntax whereis command
# awkWork with columns of text
Syntax awk 'pattern { action }' file
$ awk '{print $1}' access.logCopy
Print the first column (the IP address in many web logs)
$ awk -F: '{print $1}' /etc/passwdCopy
Use a colon as the separator and list user names
$ df -h | awk '$5+0 > 80'Copy
Show only disks that are more than 80% full
# sedFind and replace in text
Syntax sed 's/old/new/g' file
$ sed 's/foo/bar/g' file.txtCopy
Print the file with every foo changed to bar
$ sed -i 's/8080/9090/' app.confCopy
Change the file itself (in place)
$ sed -n '10,20p' file.txtCopy
Print only lines 10 to 20
Tip On macOS the in-place flag needs an empty argument: sed -i '' 's/a/b/' file.
# sortSort lines
Syntax sort [options] file
# uniqRemove repeated lines
Syntax sort file | uniq [-c]
$ sort ips.txt | uniq -cCopy
Count how many times each line appears
$ sort ips.txt | uniq -c | sort -rn | headCopy
The ten most common lines
# cutCut out columns
Syntax cut -d delimiter -f fields file
$ cut -d, -f1,3 data.csvCopy
Columns 1 and 3 of a CSV
$ cut -c1-10 file.txtCopy
First ten characters of each line
Change or delete characters
Syntax tr set1 set2
$ echo "hello" | tr a-z A-ZCopy
Prints HELLO
$ tr -d '\r' < dos.txt > unix.txtCopy
Remove Windows line endings
# xargsBuild commands from a list
Syntax command | xargs command
$ find . -name "*.log" | xargs ls -lhCopy
Run ls on every file found
$ cat urls.txt | xargs -n1 curl -OCopy
Download every URL in a list
# diffCompare two files
Syntax diff [options] file1 file2
$ diff old.conf new.confCopy
Show the differences
$ diff -u old.conf new.confCopy
Show them in the common unified (patch) style