Skip to content
dozens edited this page Nov 7, 2025 · 6 revisions

bash

Bash and bash utils: how to do some stuff on the command line. Scripting, common utils, etc.

  1. GNU Readline
  2. renaming files
  3. finding files
  4. searching
  5. suspend and resume jobs
  6. zero pad
  7. loops
  8. diffing and patching
  9. converting images
  10. expansion and substituion

GNU Readline

I never learned EMACS, so I never remember this stuff. But it's handy e.g. when you're trying to edit a line in IRC or something.

https://en.wikipedia.org/wiki/GNU_Readline

renaming files

for file in *.png; do mv "$file" "${file/pattern/pattern}"; done

finding files

  • Find by file name/type: find . -type f -name *.css

  • Find by recently changed: find . -type f -name *.vue -ctime -3

searching

  • Search .js, .jsx, .vue files, ignore case: ag --js -i orderservice

suspend and resume jobs

  1. C-z to suspend a job, like vim e.g.

  2. jobs to see suspended job

  3. fg %n to "foreground" a suspended job by its number. (Or just fg to resume the most recent job.)

zero pad

change png to jpg or whatever as needed

#!/bin/bash
num=`echo $1 | sed -n 's/\([0-9]*\).png/\1/p'`
paddednum=`printf "%03d" $num`
echo ${paddednum}.png

Test it with ./zeropad.sh 1.png or whatever. When you're confident it works, you can:

for i in *.jpg; do mv $i `./zeropad.sh $i`; done

loops

for i in {1..5}
do
  echo "Hello `printf "%03d" $i`"
done

diffing and patching

Create a patchfile:

diff -u original new > patchfile

apply the patch:

patch < patchfile

Diff two directories (optionally include -pN where N = depth):

diff -ru original/ new/ > patchfile

Reverse a diff

patch -R < patchfile

converting images

make some weird lo-res images:

convert img.png +dither -remap netscape: convert_img.png

expansion and substituion

http://mywiki.wooledge.org/BashFAQ/073

Clone this wiki locally