Find + -printf + sort conflict?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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










share|improve this question
























  • Do you have to use 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)

    – Jeff Schaller
    Jan 20 at 1:48







  • 1





    I think what you're overlooking is that sort sorts lines (or, with GNU sort -z, null-delimited elements) - it should be no surprise that it doesn't sort filenames when they're all on a single line

    – steeldriver
    Jan 20 at 1:58






  • 2





    If you a) are using GNU sed and b) you really need to find your files recursively, you can use this: find . -iname '*.flac' -print0 | sort -z | xargs -0 sox ... instead of messing with quoting and unquoting.

    – mosvy
    Jan 20 at 2:00











  • @mosvy I was just experimenting with that idea; that seems like a good additional answer!

    – Jeff Schaller
    Jan 20 at 2:00











  • If you're trying to collect filenames to pass to SoX as command-line arguments, you do not want to put quotes around them. Quotes are parsed only when they're actually part of the command, not when they're in date (e.g. from a variable or command substitution). See here for an example.

    – Gordon Davisson
    Jan 20 at 4:28















0















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










share|improve this question
























  • Do you have to use 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)

    – Jeff Schaller
    Jan 20 at 1:48







  • 1





    I think what you're overlooking is that sort sorts lines (or, with GNU sort -z, null-delimited elements) - it should be no surprise that it doesn't sort filenames when they're all on a single line

    – steeldriver
    Jan 20 at 1:58






  • 2





    If you a) are using GNU sed and b) you really need to find your files recursively, you can use this: find . -iname '*.flac' -print0 | sort -z | xargs -0 sox ... instead of messing with quoting and unquoting.

    – mosvy
    Jan 20 at 2:00











  • @mosvy I was just experimenting with that idea; that seems like a good additional answer!

    – Jeff Schaller
    Jan 20 at 2:00











  • If you're trying to collect filenames to pass to SoX as command-line arguments, you do not want to put quotes around them. Quotes are parsed only when they're actually part of the command, not when they're in date (e.g. from a variable or command substitution). See here for an example.

    – Gordon Davisson
    Jan 20 at 4:28













0












0








0








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










share|improve this question
















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 sort printf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 9:20









Rui F Ribeiro

39.9k1479135




39.9k1479135










asked Jan 20 at 1:39









atrocityatrocity

32




32












  • Do you have to use 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)

    – Jeff Schaller
    Jan 20 at 1:48







  • 1





    I think what you're overlooking is that sort sorts lines (or, with GNU sort -z, null-delimited elements) - it should be no surprise that it doesn't sort filenames when they're all on a single line

    – steeldriver
    Jan 20 at 1:58






  • 2





    If you a) are using GNU sed and b) you really need to find your files recursively, you can use this: find . -iname '*.flac' -print0 | sort -z | xargs -0 sox ... instead of messing with quoting and unquoting.

    – mosvy
    Jan 20 at 2:00











  • @mosvy I was just experimenting with that idea; that seems like a good additional answer!

    – Jeff Schaller
    Jan 20 at 2:00











  • If you're trying to collect filenames to pass to SoX as command-line arguments, you do not want to put quotes around them. Quotes are parsed only when they're actually part of the command, not when they're in date (e.g. from a variable or command substitution). See here for an example.

    – Gordon Davisson
    Jan 20 at 4:28

















  • Do you have to use 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)

    – Jeff Schaller
    Jan 20 at 1:48







  • 1





    I think what you're overlooking is that sort sorts lines (or, with GNU sort -z, null-delimited elements) - it should be no surprise that it doesn't sort filenames when they're all on a single line

    – steeldriver
    Jan 20 at 1:58






  • 2





    If you a) are using GNU sed and b) you really need to find your files recursively, you can use this: find . -iname '*.flac' -print0 | sort -z | xargs -0 sox ... instead of messing with quoting and unquoting.

    – mosvy
    Jan 20 at 2:00











  • @mosvy I was just experimenting with that idea; that seems like a good additional answer!

    – Jeff Schaller
    Jan 20 at 2:00











  • If you're trying to collect filenames to pass to SoX as command-line arguments, you do not want to put quotes around them. Quotes are parsed only when they're actually part of the command, not when they're in date (e.g. from a variable or command substitution). See here for an example.

    – Gordon Davisson
    Jan 20 at 4:28
















Do you have to use 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)

– Jeff Schaller
Jan 20 at 1:48






Do you have to use 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)

– Jeff Schaller
Jan 20 at 1:48





1




1





I think what you're overlooking is that sort sorts lines (or, with GNU sort -z, null-delimited elements) - it should be no surprise that it doesn't sort filenames when they're all on a single line

– steeldriver
Jan 20 at 1:58





I think what you're overlooking is that sort sorts lines (or, with GNU sort -z, null-delimited elements) - it should be no surprise that it doesn't sort filenames when they're all on a single line

– steeldriver
Jan 20 at 1:58




2




2





If you a) are using GNU sed and b) you really need to find your files recursively, you can use this: find . -iname '*.flac' -print0 | sort -z | xargs -0 sox ... instead of messing with quoting and unquoting.

– mosvy
Jan 20 at 2:00





If you a) are using GNU sed and b) you really need to find your files recursively, you can use this: find . -iname '*.flac' -print0 | sort -z | xargs -0 sox ... instead of messing with quoting and unquoting.

– mosvy
Jan 20 at 2:00













@mosvy I was just experimenting with that idea; that seems like a good additional answer!

– Jeff Schaller
Jan 20 at 2:00





@mosvy I was just experimenting with that idea; that seems like a good additional answer!

– Jeff Schaller
Jan 20 at 2:00













If you're trying to collect filenames to pass to SoX as command-line arguments, you do not want to put quotes around them. Quotes are parsed only when they're actually part of the command, not when they're in date (e.g. from a variable or command substitution). See here for an example.

– Gordon Davisson
Jan 20 at 4:28





If you're trying to collect filenames to pass to SoX as command-line arguments, you do not want to put quotes around them. Quotes are parsed only when they're actually part of the command, not when they're in date (e.g. from a variable or command substitution). See here for an example.

– Gordon Davisson
Jan 20 at 4:28










1 Answer
1






active

oldest

votes


















3














If the files are all in the same directory, and you want to pass a list of sorted filenames to sox, then the bash shell could do that directly:



shopt -s nocaseglob
sox *.flac


The nocaseglob shell option allows the wildcard to pick up foo.FLAC, foo.Flac, foo.flaC, etc. The wildcard expands to an alphabetically sorted list, much like a bare sort would. It would, for example, put 9-file.flac after any filename that started with 8.



Thanks to @mosvy for a simplification of the above bash-ism into a wildcard that should work with any shell:



sox *.[Ff][Ll][Aa][Cc]


The brackets allow any combination of upper- and lower-case letters in the "flac" extension, in order to match what your find ... -iname "*.FlAc" was doing.






share|improve this answer




















  • 2





    no need for nocaseglob; sox *.[fF][lL][aA][cC] will do just fine, in any shell.

    – mosvy
    Jan 20 at 1:57











  • Thank you everyone. sox *.[fF][lL][aA][cC] looks like it works while printf '%sn' *.[Ff][Ll][Aa][Cc] looks like it gives me what I need to double-check that the order is correct.

    – atrocity
    Jan 20 at 5:15










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495547%2ffind-printf-sort-conflict%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














If the files are all in the same directory, and you want to pass a list of sorted filenames to sox, then the bash shell could do that directly:



shopt -s nocaseglob
sox *.flac


The nocaseglob shell option allows the wildcard to pick up foo.FLAC, foo.Flac, foo.flaC, etc. The wildcard expands to an alphabetically sorted list, much like a bare sort would. It would, for example, put 9-file.flac after any filename that started with 8.



Thanks to @mosvy for a simplification of the above bash-ism into a wildcard that should work with any shell:



sox *.[Ff][Ll][Aa][Cc]


The brackets allow any combination of upper- and lower-case letters in the "flac" extension, in order to match what your find ... -iname "*.FlAc" was doing.






share|improve this answer




















  • 2





    no need for nocaseglob; sox *.[fF][lL][aA][cC] will do just fine, in any shell.

    – mosvy
    Jan 20 at 1:57











  • Thank you everyone. sox *.[fF][lL][aA][cC] looks like it works while printf '%sn' *.[Ff][Ll][Aa][Cc] looks like it gives me what I need to double-check that the order is correct.

    – atrocity
    Jan 20 at 5:15















3














If the files are all in the same directory, and you want to pass a list of sorted filenames to sox, then the bash shell could do that directly:



shopt -s nocaseglob
sox *.flac


The nocaseglob shell option allows the wildcard to pick up foo.FLAC, foo.Flac, foo.flaC, etc. The wildcard expands to an alphabetically sorted list, much like a bare sort would. It would, for example, put 9-file.flac after any filename that started with 8.



Thanks to @mosvy for a simplification of the above bash-ism into a wildcard that should work with any shell:



sox *.[Ff][Ll][Aa][Cc]


The brackets allow any combination of upper- and lower-case letters in the "flac" extension, in order to match what your find ... -iname "*.FlAc" was doing.






share|improve this answer




















  • 2





    no need for nocaseglob; sox *.[fF][lL][aA][cC] will do just fine, in any shell.

    – mosvy
    Jan 20 at 1:57











  • Thank you everyone. sox *.[fF][lL][aA][cC] looks like it works while printf '%sn' *.[Ff][Ll][Aa][Cc] looks like it gives me what I need to double-check that the order is correct.

    – atrocity
    Jan 20 at 5:15













3












3








3







If the files are all in the same directory, and you want to pass a list of sorted filenames to sox, then the bash shell could do that directly:



shopt -s nocaseglob
sox *.flac


The nocaseglob shell option allows the wildcard to pick up foo.FLAC, foo.Flac, foo.flaC, etc. The wildcard expands to an alphabetically sorted list, much like a bare sort would. It would, for example, put 9-file.flac after any filename that started with 8.



Thanks to @mosvy for a simplification of the above bash-ism into a wildcard that should work with any shell:



sox *.[Ff][Ll][Aa][Cc]


The brackets allow any combination of upper- and lower-case letters in the "flac" extension, in order to match what your find ... -iname "*.FlAc" was doing.






share|improve this answer















If the files are all in the same directory, and you want to pass a list of sorted filenames to sox, then the bash shell could do that directly:



shopt -s nocaseglob
sox *.flac


The nocaseglob shell option allows the wildcard to pick up foo.FLAC, foo.Flac, foo.flaC, etc. The wildcard expands to an alphabetically sorted list, much like a bare sort would. It would, for example, put 9-file.flac after any filename that started with 8.



Thanks to @mosvy for a simplification of the above bash-ism into a wildcard that should work with any shell:



sox *.[Ff][Ll][Aa][Cc]


The brackets allow any combination of upper- and lower-case letters in the "flac" extension, in order to match what your find ... -iname "*.FlAc" was doing.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 20 at 2:02

























answered Jan 20 at 1:53









Jeff SchallerJeff Schaller

41k1056130




41k1056130







  • 2





    no need for nocaseglob; sox *.[fF][lL][aA][cC] will do just fine, in any shell.

    – mosvy
    Jan 20 at 1:57











  • Thank you everyone. sox *.[fF][lL][aA][cC] looks like it works while printf '%sn' *.[Ff][Ll][Aa][Cc] looks like it gives me what I need to double-check that the order is correct.

    – atrocity
    Jan 20 at 5:15












  • 2





    no need for nocaseglob; sox *.[fF][lL][aA][cC] will do just fine, in any shell.

    – mosvy
    Jan 20 at 1:57











  • Thank you everyone. sox *.[fF][lL][aA][cC] looks like it works while printf '%sn' *.[Ff][Ll][Aa][Cc] looks like it gives me what I need to double-check that the order is correct.

    – atrocity
    Jan 20 at 5:15







2




2





no need for nocaseglob; sox *.[fF][lL][aA][cC] will do just fine, in any shell.

– mosvy
Jan 20 at 1:57





no need for nocaseglob; sox *.[fF][lL][aA][cC] will do just fine, in any shell.

– mosvy
Jan 20 at 1:57













Thank you everyone. sox *.[fF][lL][aA][cC] looks like it works while printf '%sn' *.[Ff][Ll][Aa][Cc] looks like it gives me what I need to double-check that the order is correct.

– atrocity
Jan 20 at 5:15





Thank you everyone. sox *.[fF][lL][aA][cC] looks like it works while printf '%sn' *.[Ff][Ll][Aa][Cc] looks like it gives me what I need to double-check that the order is correct.

– atrocity
Jan 20 at 5:15

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495547%2ffind-printf-sort-conflict%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?