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.

Print the list of unique usernames currently logged in

w | tail -n +3 | cut -d ' ' -f1 | sort -u

September 25, 2023but-i-am-dominator

Upgrade outdated packages when using pip

pip install --upgrade $(pip list --outdated | tail -n +3 | awk '{print $1}')

April 16, 2020docdyhr

Kill a process running on port 8080

lsof -i :8080 | awk '{print $2}' | tail -n 1 | xargs kill

August 18, 2017kimbethwel

Show the 10 largest open files

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

February 28, 2014cellojoe

Md5sum the last 5 files in a folder

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

August 21, 2013openiduser113

Tail a file with "tail -f" until text is seen

tail -f /path/to/file.log | sed '/^Finished: SUCCESS$/ q'

August 22, 2012bashoneliners

Append to a file text, a blank line, and the last line of another file

{ echo some text; echo; tail -n1 /var/log/apache2/error.log; } >> /path/to/file

June 22, 2012bashoneliners

Append to a file text, a blank line, and the last line of another file

echo -e "From: me\n\n$(tail -n1 /var/log/apache2/error.log)" >> file

June 21, 2012kevin

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

find /path/to/dir -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail

October 4, 2011janos