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.

Move file and cd with one command

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

April 16, 2022Bryan-netizen

Recursively remove all "node_modules" folders

find . -name node_modules -exec rm -rf {} +

March 29, 2020tg-z

Find directories modified between two dates

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

May 20, 2019fenchu

Get the absolute path of the current script's working directory

cwd=$(cd "$(dirname "$0")" && pwd)

January 22, 2018dhsrocha

Make a new folder and cd into it.

mkcd(){ NAME=$1; mkdir -p "$NAME"; cd "$NAME"; }

August 3, 2017PrasannaNatarajan

Test git archive before actually creating an archive // fake dry run

git archive master some/project/subdir | tar t

December 22, 2015openiduser146

Delete orphan vi and vim undo files

find . -type f -iname '*.un~' | while read UNDOFILE ; do FILE=$( echo "$UNDOFILE" | sed -r -e 's/.un~$//' -e 's&/\.([^/]*)&/\1&' ) ; [[ -e "$FILE" ]] || rm "$UNDOFILE" ; done

September 2, 2014rafaeln

List all non Git comited files and make a gzip archive with them

GITFOLDER="/srv/some/folder"   ls-files --others --exclude-standard | tar czf ${GITFOLDER}-archives/uploads-$(date '+%Y%m%d%H%M').tar.gz -T -

April 2, 2014renoirb

Show the 10 largest open files

lsof / | awk '$7 > 1048576 { print $7 / 1048576 "MB", $9, $1 }' | sort -nu | tail

February 28, 2014cellojoe

Check if a file exists and has a size greater than X

[[ $(find /path/to/file -type f -size +51200c 2>/dev/null) ]] && echo true || echo false

January 9, 2014bashoneliners

Add all unknown files in a Subversion checkout

svn add . --force

September 24, 2013bashoneliners

Find files that are not executable

find /some/path -type f ! -perm -111 -ls

September 18, 2013bashoneliners

Md5sum the last 5 files in a folder

find /directory1/directory2/ -maxdepth 1 -type f | sort | tail -n 5 | xargs md5sum

August 21, 2013openiduser113

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

Rename all items in a directory to lowercase names

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

April 20, 2013EvaggelosBalaskas

Print file owners and permissions of a directory tree

find /path/to/dir1 -printf "%U %G %m %p\n" > /tmp/dir1.txt

March 19, 2013bashoneliners

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

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

March 7, 2013Anntoin

Recreate or update an existing zip file and remove files that do not exist anymore

zip --filesync -r /path/to/out.zip /path/to/dir

January 26, 2013bashoneliners

Copy or create files with specific permissions and ownership

install -b -m 600 /dev/null NEWFILE

September 25, 2012bashoneliners

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

Execute different commands with find depending on file type

find /path/to/dir -type d -exec chmod 0755 '{}' \; -o -type f -exec chmod 0644 '{}' \;

June 17, 2012bashoneliners

Sort du output in Human-readable format

du -hsx * | sort -rh

April 26, 2012Vaevictus