递归文件夹操作

huangapple go评论66阅读模式
英文:

Recursive folder action

问题

对于文件夹中的文件,我正在使用以下代码:

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

我想要删除所有子文件夹和子子文件夹中的文件的静音部分。我希望删除静音后的版本保存在与原始文件相同的文件夹中。

我目前使用的代码只在当前文件夹中执行此操作。

英文:

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.

答案1

得分: 0

可能会起作用,具体取决于您使用的find命令版本的工作方式。它应该可以在 macOS 自带的版本中工作,但我不认为它会在 GNU find 中起作用。您应该首先进行测试,将sox命令替换为echo,以查看它将执行什么操作。

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

find将查找具有匹配名称的文件,并在替换命令中的{}与文件名之后运行-execdir命令。

使用普通的-exec选项不会起作用,因为它会提供文件的路径,而不仅仅是文件名,因此在前面添加“n_”不会产生您想要的效果。例如,它会将“./subdir/file.wav”变成“n_./subdir/file.wav”,而不是“./subdir/n_file.wav”。-execdir会进入文件所在的目录,然后只使用文件名运行它。

至少,在 macOS 自带的 bsd 版本中是这样的。Linux 使用的 GNU 版本似乎在文件名前面加上了“./”,这通常不重要,但在这种情况下会导致“n_./file.wav”等荒谬的结果。因此,首先使用echo进行测试,并确保它提供了预期的文件名。

顺便说一下,我认为还有一些其他版本的find不会在n_{}中替代文件名(只有使用{}本身),在这种情况下,这种方法将以另一种方式失败。

英文:

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 . -type f -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.

huangapple
  • 本文由 发表于 2023年1月9日 18:35:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定