Skip to content
loading...

re: I love writing scripts to solve small problems VIEW POST

FULL DISCUSSION
 

I see I've become a command line snob because I was expecting two bash commands separated by a pipe.

 
 

@eddinn , would you like to help me learn how to solve this with bash?

hey, I just saw your reply, and also see that @teroyks beat me to it.
He did it well! :)

 

My bash skills are good to a limit but especially any time I need regex stuff or conditionals, I find myself grabbing Python because I'm more comfortable building things with it.

Would love to see and learn how this could be done in bash!

 

Perhaps not the most elegant way (and uses a few more than two commands), but this is how I would have done it:

find . -iname "*.pdf" | while read F; do FILE=$(basename "$F"); NR=$(printf "%02d" "$(echo "$F" | sed "s/.*(\(.*\)).*/\1/")"); cp -v "$F" "./Output/$NR - $FILE"; done

So, basically:

  1. Find all the pdf files under the current directory (case insensitive name search) and output a list of their full (relative) paths
  2. Loop through the list one by one
  3. Save the file name (without path) into the var FILE
  4. Grep the episode number with sed and a regex
  5. Pad the number with leading zeros with printf and save into NR
  6. Copy the file into Output with a new name (prepended with the episode number)

Could use internal bash functions instead of some of the external utilities, but internal shell logic is (at least for me) harder to remember than simple utility commands.

The biggest gotcha in handling file names is to remember to surround the values with quote marks when outputting them – otherwise, bash will split file names with spaces into several values, and everything will break.

This is so cool, thanks Tero for taking the time to educate!

Individually, all parts are familiar to me but I probably couldn't have constructed such a beautiful pipe.

I added this as an edit into the original post so people interested in bash scripting can also find it more easily.

code of conduct - report abuse