Bash tips This note was created on 2023-03-20 This note was last edited on 2024-03-21 === Jobs === Stop current job: ^Z Move job #1 to the background: $ bg %1 List jobs: $ jobs Start job in the background: $ sleep 500 & Move job #2 to the foreground: $ fg %2 "jobs" options: ~~~ -l - list PIDs in addition to default info -n - list only processes that have changed since the last notification -p - list PIDs only -r - show only running jobs -s - show only stopped jobs ~~~ "bg" options: ~~~ %n - where n is the job number %abc - refers to a job started by a command beginning with abc %?abc - refers to a job started by a command containing abc %- - specifies the previous job ~~~ === Backups === Create encrypted backup of few directories: $ tar -cz ./Documents ./Keys | gpg -c -o backup.tar.gz.gpg Decrypt and unpack backup: $ gpg -d backup.tar.gz.gpg | tar xz -C ./Temp === Other === Create HTML file with clickable listing of selected directory: $ tree -H /path/to/dir -o index.html Split lines with a maximum width of 50 characters at spaces: $ fold -w 50 -s bash.txt Delete big file, when rm fails (does not change owner, permissions or descriptors): $ /path/to/file.log View list of open files: $ lsof Find what take a lot of storage: $ ncdu Measure execution time for application: $ time command Monitor command output (execute command every second): $ watch command Watch changes in file in real time: $ tail -f file.log Install or change locale/language: $ sudo dpkg-reconfigure locales Change "string1" to "string2": $ sed 's/string1/string2/g' Check version of the package in the repository: $ apt-cache policy package-name Edit crontab for other user: $ sudo crontab -e -u www-data Search for a string in files: $ grep -rn '/path/to/somewhere/' -e "pattern" Run command from other user: $ sudo -u www-data ps Find *.log files older then 7 days and delete them: $ find /tmp/*.log -mtime +7 -type f -delete Add prefix for all lines in file: $ awk '$0="prefix"$0' file > newfile List mounted file system as a table: $ mount | column -t $ column -t /proc/mounts Set audible alarm when an IP address comes online: $ ping -i 60 -a 1.1.1.1 Display the top ten running processes, sorted by memory usage: $ ps aux --sort -rss | head Watch network activity in real-time: $ lsof -i diff two unsorted files without creating temporary files: $ diff <(sort file1) <(sort file2) Show file system hierarchy: $ man hier Kill a process, that is locking a file: $ fuser -k filename Replace spaces in filenames with underscores: $ rename 'y/ /_/' * Invoke an editor for long and complex command: $ fc Save manpage as pdf: $ man -t diff | ps2pdf - diff.pdf Create a tree of dirs: $ mkdir -p root/child0/child1/{ab,cd,de,ef} Remove all but one specific file: $ rm -f !(survivor.txt) Generate a random password 30 characters long: $ strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo Graph # of connections for each hosts: $ netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | awk '{ printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++) {printf("*")}; print "" }' Show apps that use internet connection: $ ss -p Recursively remove all empty directories: $ find . -type d -empty -delete Convert seconds to human-readable format: $ date -d@1234567890 Create a quick backup copy of a file: $ cp file.txt{,.bak} Make the permissions of file2 the same as file1: $ chmod --reference file1 file2 Send pop-up notifications on Gnome: $ notify-send ["*title*"] "*body*" Run a long running script and notify when it's finished: $ ./script.sh && notify-send "Script finished" Find what package a file belongs to (on Debian/Ubuntu): $ dpkg -S /usr/bin/zsh Mount .iso file in GNU/Linux: $ mount /path/to/file.iso /mnt/cdrom -o loop "-o loop" lets you use a file as a block device Print specific line from file: $ sed -n 5p Remove all files previously extracted from a tar.gz file: $ tar -tf | xargs rm -r Which program use port 80? $ lsof -i tcp:80 Run a file system check on next boot: $ sudo touch /forcefsck Find out how much data is waiting to be written to disk: $ grep ^Dirty /proc/meminfo Generete random number between 1 and X: $ echo $[RANDOM%X+1] Alternative to if/then/else/fi construct: $ [[ test_condition ]] && if_true_do_this || otherwise_do_that List installed deb packages by size: $ dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n Switch 2 characters on a command line: ("sl" -> "ls") Insert the last argument of the previous command: . Execute a command with a timeout: $ timeout 10 sleep 11 Fast, built-in pipe-based data sink (faster then "/dev/null"): |: Shorter "ls": ls -hog Start a command on only one CPU core: $ taskset -c 0 "0" is a number of CPU core. Check if your ISP is intercepting DNS queries: $ dig +short which.opendns.com txt @208.67.220.220 Run entire shell script as root: ~~~ #!/usr/bin/sudo /bin/bash ~~~ Start HTTP server in current dir: $ python3 -m http.server Find USB device when connecting: $ diff <(lsusb) <(sleep 5s && lsusb) Cleanup Firefox database: $ find ~/.mozilla/firefox/ -type f -name "*.sqlite" -exec sqlite3 {} VACUUM \;