bashoneliners.com

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.

Truncate a file or create an empty file

> /path/to/file

November 29, 2023bashoneliners

Replace a pattern in a file in a portable way

f=/path/to/file; sed -e "s/pattern/replacement/" "$f" > "$f".bak && mv "$f".bak "$f"

November 22, 2023bashoneliners

Print the first 100 characters of a file

head -c 100 /path/to/file

November 20, 2023bashoneliners

Get detailed info about distro

echo /etc/*_ver* /etc/*-rel*; cat /etc/*_ver* /etc/*-rel*

July 18, 2023harsszeg

Unzip logrotated files in order and print concatenated content

(cd /var/log && ls syslog* | sort -n -t. -k2 | while read file; do echo "===== $file ====="; zcat "$file" || cat "$file"; done)

June 16, 2023harisokanovic

Store the output of find in an array

mapfile -d $'\0' arr < <(find /path/to -print0)

February 18, 2019bashoneliners

Find all log files modified 24 hours ago, and zip them

find . -type f -mtime +1 -name "*.log" -exec zip -m {}.zip {} \; >/dev/null

November 9, 2018TrongTan124

Create a txt files with 10000 rows

for FILE in *.full ; do split -l 100000 $FILE; mv -f xaa `echo "$FILE" | cut -d'.' -f1`.txt; rm -f x*; done

August 22, 2018Kifli88

Find and replace string inside specific files

grep -ril '$SEARCH_PATTERN' src | sed -i 's/$FIND_PATTERN/$REPLACE_PATTERN/g'

August 17, 2018gatero

List all packages with at least a class defined in a JAR file

jar tf "$1" | grep '/.*\.class$' | xargs dirname | sort -u | tr / .

February 19, 2018stefanobaghino

Remove new lines from files and folders

rename 's/[\r\n]//g' *

September 30, 2017moverperfect

Convert all flac files in dir to mp3 320kbps using ffmpeg

for FILE in *.flac; do ffmpeg -i "$FILE" -b:a 320k "${FILE[@]/%flac/mp3}"; done;

September 20, 2015Orkan

Change the encoding of all files in a directory and subdirectories

find . -type f  -name '*.java' -exec sh -c 'iconv -f cp1252 -t utf-8 "$1" > converted && mv converted "$1"' -- {} \;

November 20, 2014bashoneliners

Concatenate multiple SSL certificate files to make one PEM file

files=("yourcert.crt" "provider.ca.pem") && for i in ${files[@]} ; do $(cat $i >> yourcert.pem && echo "" >> yourcert.pem) ; done

April 2, 2014renoirb

Insert lines from one text file to another one

awk 'NR % 10 == 1 {getline f2 < "file1"; print f2} 1' file2 | cat -n

June 22, 2013openiduser102

Insert lines from one text file to another one

sed -re ':a;Rfile1' -e 'x;s/^/./;/.{10}/!{x;ba};s/.*//;x' file2

June 22, 2013openiduser102

Find distinct file extensions in a folder

find . -type f | perl -ne 'print $1 if /\.([^.\/]+)$/' | sort -u

May 17, 2013kowalcj0

Get streamed FLV from Chrome with lsof

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

May 11, 2013GNA

Unhide all hidden files in the current directory.

find . -maxdepth 1 -type f -name '\.*' | sed -e 's,^\./\.,,' | sort | xargs -iname mv .name name

April 25, 2013openiduser93

Rename all files in a directory to upper case

for i in *; do mv "$i" "${i^^}"; done

April 20, 2013EvaggelosBalaskas

Get only the latest version of a file from across mutiple directories

find . -name custlist\* | perl -ne '$path = $_; s?.*/??; $name = $_; $map{$name} = $path; ++$c; END { print $map{(sort(keys(%map)))[$c-1]} }'

February 23, 2013bashoneliners

Remove offending key from known_hosts file with one swift move

vi +18d +wq ~/.ssh/known_hosts

October 30, 2012bashoneliners

Replace the header of all files found.

find . -type f -name '*.html' -exec sed -i -e '1r common_header' -e '1,/STRING/d' {} \;

October 25, 2012jam