Welcome to bashoneliners.com, a curated collection of practical and well-explained Bash one-liners, snippets, tips and tricks. We aim to make each published one-liner to be of high quality: useful, easy to read, follows best practices, with clear, detailed, accurate explanation. These one-liners should help you automate tasks, troubleshoot problems, whether it be in system administration, file management, networking or programming.
> /path/to/file
f=/path/to/file; sed -e "s/pattern/replacement/" "$f" > "$f".bak && mv "$f".bak "$f"
head -c 100 /path/to/file
echo /etc/*_ver* /etc/*-rel*; cat /etc/*_ver* /etc/*-rel*
(cd /var/log && ls syslog* | sort -n -t. -k2 | while read file; do echo "===== $file ====="; zcat "$file" || cat "$file"; done)
mapfile -d $'\0' arr < <(find /path/to -print0)
find . -type f -mtime +1 -name "*.log" -exec zip -m {}.zip {} \; >/dev/null
for FILE in *.full ; do split -l 100000 $FILE; mv -f xaa `echo "$FILE" | cut -d'.' -f1`.txt; rm -f x*; done
grep -ril '$SEARCH_PATTERN' src | sed -i 's/$FIND_PATTERN/$REPLACE_PATTERN/g'
jar tf "$1" | grep '/.*\.class$' | xargs dirname | sort -u | tr / .
rename 's/[\r\n]//g' *
for FILE in *.flac; do ffmpeg -i "$FILE" -b:a 320k "${FILE[@]/%flac/mp3}"; done;
find . -type f -name '*.java' -exec sh -c 'iconv -f cp1252 -t utf-8 "$1" > converted && mv converted "$1"' -- {} \;
files=("yourcert.crt" "provider.ca.pem") && for i in ${files[@]} ; do $(cat $i >> yourcert.pem && echo "" >> yourcert.pem) ; done
awk 'NR % 10 == 1 {getline f2 < "file1"; print f2} 1' file2 | cat -n
sed -re ':a;Rfile1' -e 'x;s/^/./;/.{10}/!{x;ba};s/.*//;x' file2
find . -type f | perl -ne 'print $1 if /\.([^.\/]+)$/' | sort -u
export psid=$(pgrep -f libflashplayer.so); cp /proc/$psid/fd/$(lsof -p $psid | grep eleted | awk {' print $4 '} | sed -e "s/[a-z]//g") saved.flv
find . -maxdepth 1 -type f -name '\.*' | sed -e 's,^\./\.,,' | sort | xargs -iname mv .name name
for i in *; do mv "$i" "${i^^}"; done
find . -name custlist\* | perl -ne '$path = $_; s?.*/??; $name = $_; $map{$name} = $path; ++$c; END { print $map{(sort(keys(%map)))[$c-1]} }'
sed -i 18d .ssh/known_hosts
vi +18d +wq ~/.ssh/known_hosts
find . -type f -name '*.html' -exec sed -i -e '1r common_header' -e '1,/STRING/d' {} \;