Quickly Updating Files using SED

I had a client send several PDFs (around 100) and I wanted to quickly update the names of all of the files using a similar pattern. For example, I wanted to switch all files from something like this: "1812 Governors Race.pdf", to "1812_Governor.pdf"

All files had different years, so I needed to preserve the year.


The following bash script took care of it:

for old in *.pdf
do new=$(echo $old | sed -e 's/ Governors Race/_Governor/')
mv -v "$old" "$new"

Note that this doesn't work recursively. It's a rather quick and dirty solution. This will only work for the files in the same directory.

Blog Logo

Phil Birnie



Phil Birnie

Full Stack Web Developer from Columbus, Ohio

Back to Overview