What is your favorite Bash one liner

I am collecting Bash one liners - I like collecting useful one liners which eventually I wish to publish in a free booklet.

Here are some examples - feel free to add yours.

Strip EXIF Data from files

find -name ‘*.png’ -print0 | xargs -0 -r mogrify -format png

*Replace spaces in filenames with underscores

for f in *;do mv “$f” “${f// /_}”;done

I am sure that the community has others - what do you think is the most useful?


My is:

history | grep 'command'

I think the one I use most often is when I forget to sudo something.


Not bash specific but convert command is my recent obsession. It can convert between image formats and do other stuff.

convert input.png input.jpg

Monitor log file and continuously print new entries as they are appended to the systemd journal and log file

tail -f /var/log/log_file
# they have `tailf` too
tailf  /var/log/log_file
systemctl -f
journalctl -f -u nginx.service

And yes sudo !! is so awesome.

Here is another that I have found useful when doing image galleries -

for i in $(ls *.png); do convert-scale 125 $i tn-$i; done

It thumbnail all of the images in a directory (requires Imagemagik- http://www.imagemagik.org)
and it is very fast

No need to use the ls. Also do add double quote to prevent globbing and word splittin. Here is a better and safe version that can deal with file names having white spaces:

for i in *.png; do convert-scale 125 "$i" "tn-$i"; done

I like it - I’ll use that one from now on.


Here is another one that comes in handy

Find listening ports
netstat -tulpn

You can also use …
nmap -sT -O <server-name (ie: localhost)>

Isn’t it netstat deprecated? I think we have to use ss now.

ss -tulpn

Yes it is deprecated - been using it for years and had it in my notes for quick reference - guess I have to “get with the times”.

Here is another that I found that might come in handy

Make a new folder and then cd into it

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

Quote “$1” and replace with “$@”:

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

So that example like will work too (make a dir named “Dir Name Here”):

mkcd Dir Name Here

Thanks Monk I appreciate that suggestion

Mine is this for monitoring a command output

tl(){ COMMAND="$@"; while true; do date; $COMMAND; sleep 1; done }

List biggest files first:

du -ak | sort -rn | more

Do MD5 sum on all files in current dir and its subdirs:

find * -type f -exec md5sum -b '{}' ';' >MD5SUM

(Do md5sum -c MD5SUM to verify)

A command that finds all files & directories inside the current directory without secure perms(600 for files and 700 for directories) using find. It then interactively asks if you want to change the perms to secure perms.

find . \( -type f -not -perm 0600 -ok chmod 0600 '{}' ';' \) -or \( -type d -not -perm 0700 -ok chmod 0700 '{}' ';' \)

I think you need to pass the -i to the chmod to make it interactive?

No, the -ok action in find will ask you for each file if you want to execute the command.

1 Like

Oh, right. I missed the -ok which is from the find itself. Thanks!