DEV Community

Discussion on: I love writing scripts to solve small problems

Collapse
 
teroyks profile image
Tero Y • Edited

Just for fun, here is the way I would most likely currently do this – using the fish shell instead of bash. Bash is, of course, more standard and concise, but the fish version is much more readable – fish has IMO much of the same elegance and delight as Python. Fewer special characters ($), more intelligent variable handling (less worrying about remembering to quote everything), etc.

Less street cred for producing esoteric incantations though. :-)

for f in (find . -iname "*.pdf")
    set file (basename $f)
    set number (string match -r "\((.*)\)" $f)[2]
    set number (printf "%02d" $number)
    cp -v $f "./Output/$number - $file"
end

(The string matching and printf could be combined into one line, but since fish has multi-line command editing as a standard feature, doing the two things separately makes the code more readable.)

Collapse
 
hamatti profile image
Juha-Matti Santala

This looks great!

Much easier to read and understand than the bash pipe.