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.

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

Find directories modified between two dates

find . -type d -newermt "2019-01-01" ! -newermt "2019-02-01" -exec ls -ld {} \;

May 20, 2019fenchu

Up all docker services as detached mode over all immediate subdirectories

for dir in $(ls -d */); do eval $(cd $PWD/$dir && docker-compose up -d && cd ..); done;

August 17, 2018gatero

List the content of a GitHub repository without cloning it

svn ls https://github.com/user/repo/trunk/some/path

May 21, 2017bashoneliners

Remove files and directories whose name is a timestamp older than a certain time

ls | grep '....-..-..-......' | xargs -I {} bash -c "[[ x{} < x$(date -d '3 days ago' +%Y-%m-%d-%H%M%S) ]] && rm -rfv {}"

May 7, 2013openiduser95

Tree-like output in ls

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

April 26, 2013clitips

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

find . -name 'filename' | xargs -r ls -tc | head -n1

March 7, 2013Anntoin

Log and verify files received via FTP

for i in $(cat /var/log/vsftpd.log | grep $DATE_TIME | grep UPLOAD | grep OK); do ls /FTP/HOME/$i >> /dev/null 2> \&1; if \[ $? = 0 \]; then echo "$i" >> $FILES_OK_UPLOADS.log; else  echo "$DATE ERROR: File $i not found" >> $FTP_FILES_NOTOK_$DATE_TIME.log; fi; done

July 10, 2012dark_axl

Find the most recently modified files in a directory and all subdirectories

find /path/to/dir -type f -mtime -7 -print0 | xargs -0 ls -lt | head

March 8, 2012bashoneliners

Remove all the versioned-but-empty directories from a Subversion checkout

find . -name .svn -type d | while read ss; do dir=$(dirname "$ss"); test $(ls -a "$dir" | wc -l) == 3 && echo "svn rm \"$dir\""; done

November 27, 2011bashoneliners

Rename all files in the current directory by capitalizing the first letter of every word in the filenames

ls | perl -ne 'chomp; $f=$_; tr/A-Z/a-z/; s/(?<![.'"'"'])\b\w/\u$&/g; print qq{mv "$f" "$_"\n}'

November 1, 2011bashoneliners

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

Aliases the ls command to display the way I like it

alias ls='ls -lhGpt --color=always'

September 22, 2011versorge

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