I have some working code, it’s very simple – it copies every *.jpg file, renames it into *1.jpg, no worries.
for i in *.jpg; do cp $i ${i%%.jpg}1.jpg; done
how can I run this, so that it works on every file in a directory, every file in the subdirectories of that directory
example directory structure:
test/test1/test2/somefile.jpg
test/anotherfile.jpg
test/test1/file.jpg
etc
The command to do anything recursively to a directory structure is
find:Using
-execinstead offor i in $(find ...)will handle filenames that contain a space. Of course, there is still one quoting gotcha; if the filename contains a", thefile="{}"will expand tofile="name containing "quote characters"", which is obviously broken (filewill becomename containing quoteand it will try to execute thecharacterscommand).If you have such filenames, or you might, then print out each filename separated with null characters (which are not allowed in filenames) using
-print0, and usewhile read -d $'\0' ito loop over the null-delimited results:As with any complex command like this, it’s a good idea to test it without executing anything to make sure it’s expanding to something sensible before running it. The best way to do this is to prepend the actual command with
echo, so instead of running it, you will see the commands that it will run:Once you’ve eyeballed it and the results looks good, remove the
echoand run it again to run it for real.