TIL: Very useful Linux/Unix commands

Here is a list of useful unix commands or code parts. Who does not know it? You have a problem and looking for a solution where you find at stack overflow or similar pages? Here I collect all the commands that I have encountered over time or whose switch I simply can not remember (or want).

  • How do I find all files containing specific text?
grep -rnw '/path/to/somewhere/' -e 'pattern'
  • How i change the default file permissions (mask that controls file permissions)
umask
  • Untar (unzip) file/folder
tar -zxvf archive.tar.gz
  • Tar (zip) file/folders
tar -cvzf archive.tar.gz file1 file2
  • Copy files via rsync from one host to another
rsync -avz [USER@]HOST:SOURCE [USER@]HOST:DEST
rsync -avz [USER@]HOST:SOURCE rsync://[USER@]HOST[:PORT]/DEST
rsync -avz -e "ssh -p 12345" LOCAL/SOURCE [USER@]HOST:DEST
  • nslookup missing? Install dig
sudo apt-get install dnsutils
  • find without “Permission denied”
find / -name 'filename.ext' 2>&1 | grep -v "Permission denied"
  • flush dns cache
sudo systemd-resolve --flush-caches
  • show open ports
netstat -tulpn
  • Directory size
du -sh /var
du -shc /var/*
du -h --max-depth=1 /var
du -sh /var/lib/docker/containers/*/*.log
  • Search multiple PDF files for a “needle”
pdfgrep -i needle haystack*.pdf
  • Show hidden files with ls
ls -lar
  • Redirect STDOUT and STDERR to a file
nice-command > out.txt 2>&1
  • Installs your SSH public key to a remote host
sh-copy-id 'user@remotehost'
  • A command-line system information tool
neofetch
  • Show disk usage, folder size, items per folder, find big directorys, … with ncdu
ncdu