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.

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

Move file and cd with one command

mv file dir/ && cd "$_" && pwd

April 16, 2022Bryan-netizen

Organise image by portrait and landscape

mkdir "portraits"; mkdir "landscapes"; for f in ./*.jpg; do WIDTH=$(identify -format "%w" "$f")> /dev/null; HEIGHT=$(identify -format "%h" "$f")> /dev/null; if [[ "$HEIGHT" > "$WIDTH" ]]; then mv "$f" portraits/ ; else mv "$f" landscapes/ ; fi; done

August 23, 2018Jab2870

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

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

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

Rename all items in a directory to lowercase names

for name in *; do mv "$name" "${name,,}"; done

April 20, 2013EvaggelosBalaskas

Test a one-liner with echo commands first, pipe to bash when ready

paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done | sh

October 8, 2011janos

Remove spaces recursively from all subdirectories of a directory

find /path/to/dir -type d | tac | while read LINE; do target=$(dirname "$LINE")/$(basename "$LINE" | tr -d ' '); echo mv "$LINE" "$target"; done

September 20, 2011bashoneliners

Rename all files in a directory to lowercase names

paste <(ls) <(ls | tr A-Z a-z) | while read OLD NEW; do echo mv -v $OLD $NEW; done

August 5, 2011bashoneliners