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

Move file and cd with one command

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

April 16, 2022Bryan-netizen

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

Source without circular reference

[ ! "${LIB}" ] && ( readonly LIB; . "${ $( cd $( dirname $0 ) && pwd ) }/<path_to>/LIB.sh" )

January 24, 2018dhsrocha

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

Go up to a particular folder

alias ph='cd ${PWD%/public_html*}/public_html'

July 18, 2017Jab2870

Preserve your fingers from cd ..; cd ..; cd..; cd..;

up(){ DEEP=$1; for i in $(seq 1 ${DEEP:-"1"}); do cd ../; done; }

June 28, 2017alireza6677

Preserve your fingers from cd ..; cd ..; cd..; cd..;

upup(){ DEEP=$1; [ -z "${DEEP}" ] && { DEEP=1; }; for i in $(seq 1 ${DEEP}); do cd ../; done; }

June 9, 2015andreaganduglia

Do something in another directory without going there

(cd /path/to/somewhere; tar c .) > somewhere.tar

April 2, 2012bashoneliners

Come back quickly to the current directory after doing some temporary work somewhere else

pushd /some/where/else; work; cd /somewhere; work; cd /another/place; popd

January 15, 2012bashoneliners

Mirror from one Subversion repository to another Subversion repository

bzr co https://repo1/proj1/trunk proj1 && cd proj1 && bzr push https://repo2/vendor/proj1/trunk

August 5, 2011bashoneliners