Skip to content

Instantly share code, notes, and snippets.

@somarlyonks
Last active July 7, 2023 09:54
Show Gist options
  • Select an option

  • Save somarlyonks/a13ef301010cf2cd3aae029c53938bda to your computer and use it in GitHub Desktop.

Select an option

Save somarlyonks/a13ef301010cf2cd3aae029c53938bda to your computer and use it in GitHub Desktop.
# Show 20 biggest directories with 'K M G'
du | \
sort -r -n | \
awk '{split("K M G",v); s=1; while($1>1024){$1/=1024; s++} print int($1)" "v[s]"\t"$2}' | \
head -n 20
# Find files that have been modified on your system in the past 60 minutes
find / -mmin 60 -type f
# Find all files larger than 20M
find / -type f -size +20M
# Find duplicate files (based on MD5 hash)
find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33
# Change permission only for files
cd /var/www/site && find . -type f -exec chmod 766 {} \;
cd /var/www/site && find . -type f -exec chmod 664 {} +
# Change permission only for directories
cd /var/www/site && find . -type d -exec chmod g+x {} \;
cd /var/www/site && find . -type d -exec chmod g+rwx {} +
# Find files and directories for specific user
find . -user <username> -print
# Find files and directories for all without specific user
find . \!-user <username> -print
# Delete older files than 60 days
find . -type f -mtime +60 -delete
# Recursively remove all empty sub-directories from a directory
find . -depth -type d -empty -exec rmdir {} \;
# How to find all hard links to a file
find </path/to/dir> -xdev -samefile filename
# Recursively find the latest modified files
find . -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
# Recursively find/replace of a string with sed
find . -not -path '*/\.git*' -type f -print0 | xargs -0 sed -i 's/foo/bar/g'
# Recursively find/replace of a string in directories and file names
find . -depth -name '*test*' -execdir bash -c 'mv -v "$1" "${1//foo/bar}"' _ {} \;
# Recursively find suid executables
find / \( -perm -4000 -o -perm -2000 \) -type f -exec ls -la {} \;
# Search for a "pattern" inside all files in the current directory
grep -rn "pattern"
grep -RnisI "pattern" *
fgrep "pattern" * -R
# Remove blank lines from a file and save output to new file
grep . filename > newfilename
# Show only for multiple patterns
grep 'INFO*'\''WARN' filename
grep 'INFO\|WARN' filename
grep -e INFO -e WARN filename
grep -E '(INFO|WARN)' filename
egrep "INFO|WARN" filename
# Except multiple patterns
grep -vE '(error|critical|warning)' filename
# Show data from file without comments
grep -v ^[[:space:]]*# filename
# Show data from file without comments and new lines
egrep -v '#|^$' filename
# Show strings with a dash/hyphen
grep -e -- filename
grep -- -- filename
grep "\-\-" filename
# Show process that use internet connection at the moment
lsof -P -i -n
# Show process that use specific port number
lsof -i tcp:443
# Lists all listening ports together with the PID of the associated process
lsof -Pan -i tcp -i udp
# List all open ports and their owning executables
lsof -i -P | grep -i "listen"
# Show all open ports
lsof -Pnl -i
# Show open ports (LISTEN)
lsof -Pni4 | grep LISTEN | column -t
# List all files opened by a particular command
lsof -c "process"
# View user activity per directory
lsof -u username -a +D /etc
# Show 10 largest open files
lsof / | \
awk '{ if($7 > 1048576) print $7/1048576 "MB" " " $9 " " $1 }' | \
sort -n -u | tail | column -t
#Show current working directory of a process
lsof -p <PID> | grep cwd
ffmpeg -i input.mp4 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis output.webm
netstat -tn 2>/dev/null | grep :22
# Show a 4-way scrollable process tree with full details
ps awwfux | less -S
# Processes per user counter
ps hax -o user | sort | uniq -c | sort -r
# Show all processes by name with main header
ps -lfC nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment