Bash Utility Usages
Permissions
Section titled “Permissions”1, 3, 5, 7 xyz x = file owner y = group owner z = everyone else 7 = r + w + x 5 = r + x
755 = read/write/execute to owner, read/execute to group owner 775 = owner read/write/execute
Time utility
Section titled “Time utility”real - wall clock time user - amount of cpu time used while executing sys - amount of cpu time in hte kernel in the process
Compression and Decompression
Section titled “Compression and Decompression”tar examples
Section titled “tar examples”Compress
Section titled “Compress”tar --create --file /tmp/archive.tar --verbose /tmp/archive/ # Create archiveList files
Section titled “List files”tar -tzf archive.tar.gzDecompress
Section titled “Decompress”tar xf <archive name>.tar # Unpack archivetar -xzf archive.tar.gzpigz examples
Section titled “pigz examples”Compress
Section titled “Compress”tar --use-compress-program=pigz -cf bldC.tar.gz bin/*Decompress
Section titled “Decompress”# 3m real time decompressionpigz -dc bldC.tar.gz | tar --directory=/tmp/cpp -x -f -pigz -dc bldC.tar.gz | tar -xf - -C cpp/zstd examples
Section titled “zstd examples”Compress
Section titled “Compress”# 2m 5star -cf - cpp/ | zstd --long -T0 -3 > bldC.tar.zst# 4m 30star -cf - cpp/ | zstd > bldC.tar.zstDecompress
Section titled “Decompress”# 1m 20s - so 3x as fastzstd -d -c bldC.tar.zst | tar -xf - -C cpp/# no speed up using long -T0 options from aboveSymlinking
Section titled “Symlinking”Relative symlinks can have unintended consequences - I try to avoid
readlink /tmp/mysymlinkln -s /tmp/target.a /tmp/symlink_to_target.aRemove files - rm too long error
Section titled “Remove files - rm too long error”Can encounter errors with fuzzy matching if too many files return
#rm -rf ./*.dat # old stylefind . -name "*.rst" -print0 -exec echo {} +find . -name "*.rst" -deletefind . -mtime +N # modified more than N days agoSed commands
Section titled “Sed commands”find /path/dir -type f -name "*.txt" -exec sed -i 's/oldpattern/newpattern/g' {} +# on mac, after brew install gsedfind /path/dir -type f -name "*.txt" -exec gsed -i 's/oldpattern/newpattern/g' {} +Kill Procs
Section titled “Kill Procs”ps aux | grep python| awk '{ print $2 }' # List out processes to killKill processes
kill $(ps aux | grep python | awk '{ print $2 }')# get hanging proceskill -9 $(ps aux | grep python | awk '{ print $2 }')# Or, with pkillpkill -f python# Or, with killallkillall -r pythonPostgres Commands
Section titled “Postgres Commands”View and reload HBA File
SHOW hba_file;SHOW data_directory;SELECT pg_reload_conf();pg_dump and pg_restore examples
pg_dump -Z 6 -F c db_name > /tmp/db_dump.dumppg_restore -F c ./db_dump.dump -d db_name -vQuick and dirty backups
Section titled “Quick and dirty backups”Improved version
hosts=("example" "example2")for h in "${hosts[@]}"; do echo "Runnning $h" ssh "$h" 'rsync -avP ~/ archivehost:/tmp/archive/$HOST/home/'doneSimple version using excel to generate lists
HOST=example; ssh "$HOST" 'rsync -avP ~/ "archivehost:/tmp/archive/$HOST/home/' &HOST=example2; ssh "$HOST" 'rsync -avP ~/ "archivehost:/tmp/archive/$HOST/home/' &Iperf3 usage
Section titled “Iperf3 usage”iperf3 -s -p 5201 # On serveriperf3 -c <server-ip> -p 5201 # On Clientiperf3 -c <server-ip> -p 5201 -u # udpfor port in {5201..5210}; do iperf3 -c <server-ip> -p $port; done # multiple portsRPM Commands
Section titled “RPM Commands”rpm -qa package # Find out where in file system package is installedrpm -qa | grep package # Lookup where packages are installedLsof commands
Section titled “Lsof commands”lsof /path/to/file # See What’s Using a File or Directorylsof -u usernamekill -9 $(lsof -t -i :5000)lsof -p <PID> # List open files for process IDCleanup remote git branches
Section titled “Cleanup remote git branches”git fetchfor branch in $(git branch -r --merged | grep -v HEAD | grep -v develop | grep -v master | grep -v master | sed /\*/d); do if [ -z "$(git log -1 --since='Jun 15, 2020' -s ${branch})" ]; then echo -e `git show --format="%ci %cr %an" ${branch} | head -n 1` \\t$branch remote_branch=$(echo ${branch} | sed 's#origin/##' ) fidoneFormat xml file in place
Section titled “Format xml file in place”xmllint -format -recover a.xml > b.xml