Home > front end >  Recursive folder action
Recursive folder action

Time:01-09

For the files in a folder I'm using

for file in *.wav; do sox "$file" "n_$file" silence 1 0.1 0.1% reverse silence 1 0.1 0.1% reverse; done

I want to strip silence all the files in sub & subsub folders. I want the strip silenced versions in the same folder as originals.

The code I use just does it inside the current folder.

CodePudding user response:

This may work, depending on exactly how your version of the find command works. It should work with the version that comes with macOS, but I don't think it'll work with GNU find. You should test it first, by replacing the sox command with echo, to see what it's going to do.

find -name "*.wav" -execdir sox {} n_{} silence 1 0.1 0.1% reverse silence 1 0.1 0.1% reverse \;

find will find files with matching names, and run the -execdir command, after replacing the {} in the command with the filename.

Using the plain -exec option won't work, because it'd give the file's path, rather than just the filename, so prepending "n_" wouldn't do what you want. For example, it'd turn "./subdir/file.wav" into "n_./subdir/file.wav" rather than "./subdir/n_file.wav". -execdir instead does a cd into the file's directory, and then runs it with just the filename.

At least, that's what the bsd version that comes with macOS does. The GNU version that Linuxes use seems to put "./" in front of the filename; this usually doesn't matter, but in this case it gives silliness like "n_./file.wav". So test with echo first, and make sure it gives the expected filenames.

BTW, I think there are some other versions of find that won't substitute the filename in n_{} (only with {} by itself) in which case this'll fail in yet another way.

  • Related