wiki:bash_commands
Table of Contents
Bash commands
Remove first character:
for f in *; do mv "$f" "${f:1}"; done
Add 8 to filename:
for f in *; do mv "$f" "8$f"; done
Sort folders by disk usage, larger first:
du -sh /* | sort -hr
Checksum directory recursively
find /var/lib/mysql -type f -print0 | xargs -0 sha256sum > mysql.sha256sum
Comment and uncomment lines containing specific strings
sed -e '/multiverse/s/^/#/' -i /etc/apt/sources.list sed -e '/multiverse/s/^#//' -i /etc/apt/sources.list
Search and replace strings in multiple html files:
for file in $(grep -cr --include "*.html" "somestring" . | awk -F ":" '{print $1}');do sed -i 's/\bsomestring\b/newstring/g' $file;done
\b is word boundary, meaning it won't select strings like string_somestring_smth_smth Put sed -n… and ..newstring/gp' to just see the changes without actually making them.
Print from certain line to certain line:
zcat server_20170726-060002.log.gz | awk '/2017-07-25\ 20:54:59.381/,/2017-07-25\ 21:16:00.948/' - > incident.txt
Sed add word/string after specific string on same line i.e. add "blablah" after " localhost ":
sed 's/\blocalhost\b/& blablah/' file
Print disk usage, summarize in GB
du -s /var/lib/mysql/mydb/{table1,table2,table3}.* | awk '{ sum+=$1} END {printf sum/1024^2; print " GB"}'
Remove old unused kernels
sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'|grep -E "(image|headers|modules)" | grep -v hwe | xargs sudo apt-get -y purge
Tested on
See also
References
wiki/bash_commands.txt · Last modified: 2024/07/22 12:32 by antisa