I need to pass a list of sorted, quoted FLAC file names to SoX for concatenation but am having trouble getting sort to work the way I expect it to.
If I use:
find . -maxdepth 1 -type f -iname "*.flac" | sort
I get exactly what I expect:
./01-Lordy.flac
./02-Both Sides Now.flac
./03-Solitary Man.flac
./04-Holly Holy.flac
./05-Cherry Cherry.flac
./06-Kentucky Woman.flac
./07-Sweet Caroline.flac
./08-Thank the Lord for the Nightime.flac
./09-And the Singer Sings His Song.flac
./10-Brother Loves Traveling Salvation Show.flac
However, I can't really use that for what I'm doing since I need a quoted list with no newlines. I know -printf can do that for me, but when I try:
find . -maxdepth 1 -type f -iname "*.FlAc" -printf "\"%p\" " | sort
I wind up with a list of file names that are quoted and separated by a single space (good!) but they aren't sorted (bad!). Or at least they aren't sorted the way I expect them to be:
"./08-Thank the Lord for the Nightime.flac" "./03-Solitary Man.flac" "./09-And the Singer Sings His Song.flac" "./05-Cherry Cherry.flac" "./06-Kentucky Woman.flac" "./10-Brother Loves Traveling Salvation Show.flac" "./07-Sweet Caroline.flac" "./02-Both Sides Now.flac" "./01-Lordy.flac" "./04-Holly Holy.flac"
Maybe even weirder, if I just for the sake of testing use the same code but add a newline:
find . -maxdepth 1 -type f -iname "*.FlAc" -printf "\"%p\" "\\n | sort
The sort works even if the output is back to not being what I need:
"./01-Lordy.flac"
"./02-Both Sides Now.flac"
"./03-Solitary Man.flac"
"./04-Holly Holy.flac"
"./05-Cherry Cherry.flac"
"./06-Kentucky Woman.flac"
"./07-Sweet Caroline.flac"
"./08-Thank the Lord for the Nightime.flac"
"./09-And the Singer Sings His Song.flac"
"./10-Brother Loves Traveling Salvation Show.flac"
This is under Ubuntu 18.04.1
find
here, for the recursion? Or are all the desired files in the same directory? (I ask, given the-maxdepth 1
and the sample output indicating the same directory)sort
sorts lines (or, with GNUsort -z
, null-delimited elements) - it should be no surprise that it doesn't sort filenames when they're all on a single linefind . -iname '*.flac' -print0 | sort -z | xargs -0 sox ...
instead of messing with quoting and unquoting.