Search code examples
linuxbashsoxbrace-expansion

Prefixing an output file in bash with brace expansion after sox processing


Given several subdirectories of mixed files on a Ubuntu 16.04 server, I want to take all the ones named like 1.al, 2.al etc, process them, keep the original files, and prefix the resulting files with quick_. So I have this...

find . -name '[[:digit:]]*.al' -print0 | xargs -0 -I {} sh -c 'for f; do sox  -c1 --rate 8k "$f" -e a-law "quick_${f}" tempo -q -s 1.5; done' sh {}

But I get

can't open output file `quick_./1.al': No such file or directory

I can see why it is happening, and I have the feeling that the answer lies in brace expansion. I've found recipes for stripping and adding suffixes, but the prefix answer is eluding me. Thanks.


Solution

  • There's not a lot to go on here, but I think you want to use the basename command to get just the filename:

    find . -name '[[:digit:]]*.al' -print0 | xargs -0 -I {} sh -c 'for f; do sox  -c1 --rate 8k "$f" -e a-law "$(dirname "$f")/quick_$(basename "$f")" tempo -q -s 1.5; done' sh {}
    

    Personally, I find it a bit easier working with the -exec flag to find. It's one less level of indirection to worry about. {} represents the filename, and the command is terminated with a ; (escaped so the shell doesn't grab it.)

    find . -name '[[:digit:]]*.al' -exec sh -c 'sox -c1 --rate 8k "{}" -e a-law "$(dirname "{}")/quick_$(basename "{}")" tempo -q -s 1.5' \;