Extracting list of filenames, including ones with blanks, from find

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











up vote
2
down vote

favorite












I'd like to do something with each file of some type in a particular directory. I've written, in a bash script,



HANDIN_FILES=`find . -type f -printf "%fn" | head -20 `
for i in $HANDIN_FILES
do
echo $i
done


as a kind of first version. This works great if there are no blanks in any filename. If there is a file silly name.txt, then i ends up being first silly and then name.txt, which is not what I want at all.



I know that I could put all the do ... done stuff in a -exec option in the find command, but I'd like to preserve the list of files in a shell variable for later use as well. Can someone suggest a quick fix that'll cause i to have the value silly name.txt on one iteration through the loop?










share|improve this question

















  • 5




    See Why is looping over find's output bad practice?
    – Stéphane Chazelas
    Oct 3 '17 at 20:32














up vote
2
down vote

favorite












I'd like to do something with each file of some type in a particular directory. I've written, in a bash script,



HANDIN_FILES=`find . -type f -printf "%fn" | head -20 `
for i in $HANDIN_FILES
do
echo $i
done


as a kind of first version. This works great if there are no blanks in any filename. If there is a file silly name.txt, then i ends up being first silly and then name.txt, which is not what I want at all.



I know that I could put all the do ... done stuff in a -exec option in the find command, but I'd like to preserve the list of files in a shell variable for later use as well. Can someone suggest a quick fix that'll cause i to have the value silly name.txt on one iteration through the loop?










share|improve this question

















  • 5




    See Why is looping over find's output bad practice?
    – Stéphane Chazelas
    Oct 3 '17 at 20:32












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'd like to do something with each file of some type in a particular directory. I've written, in a bash script,



HANDIN_FILES=`find . -type f -printf "%fn" | head -20 `
for i in $HANDIN_FILES
do
echo $i
done


as a kind of first version. This works great if there are no blanks in any filename. If there is a file silly name.txt, then i ends up being first silly and then name.txt, which is not what I want at all.



I know that I could put all the do ... done stuff in a -exec option in the find command, but I'd like to preserve the list of files in a shell variable for later use as well. Can someone suggest a quick fix that'll cause i to have the value silly name.txt on one iteration through the loop?










share|improve this question













I'd like to do something with each file of some type in a particular directory. I've written, in a bash script,



HANDIN_FILES=`find . -type f -printf "%fn" | head -20 `
for i in $HANDIN_FILES
do
echo $i
done


as a kind of first version. This works great if there are no blanks in any filename. If there is a file silly name.txt, then i ends up being first silly and then name.txt, which is not what I want at all.



I know that I could put all the do ... done stuff in a -exec option in the find command, but I'd like to preserve the list of files in a shell variable for later use as well. Can someone suggest a quick fix that'll cause i to have the value silly name.txt on one iteration through the loop?







bash find






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 3 '17 at 20:29









John

1133




1133







  • 5




    See Why is looping over find's output bad practice?
    – Stéphane Chazelas
    Oct 3 '17 at 20:32












  • 5




    See Why is looping over find's output bad practice?
    – Stéphane Chazelas
    Oct 3 '17 at 20:32







5




5




See Why is looping over find's output bad practice?
– Stéphane Chazelas
Oct 3 '17 at 20:32




See Why is looping over find's output bad practice?
– Stéphane Chazelas
Oct 3 '17 at 20:32










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










With bash 4.4+:



readarray -td '' files < <(find . -type f -print0 | head -zn 20)
for i in "$files[@]"; do
something with "$i"
done


With earlier bash versions:



files=()
while IFS= read -rd '' file; do
files+=("$file")
done < <(find . -print0 | head -zn 20)

for i...


Here, it's simpler to use zsh:



files=(**/*(D.[1,20]))
for i ($files) something with $i


(at least the file list will be sorted so the first 20 makes more sense)



See Why is looping over find's output bad practice? for other ways to process (or not) find's output.






share|improve this answer




















  • Thanks; that not only solved my problem, but reading the linked stuff provided a bit of education as well!
    – John
    Oct 3 '17 at 21:08










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
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%2f395915%2fextracting-list-of-filenames-including-ones-with-blanks-from-find%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










With bash 4.4+:



readarray -td '' files < <(find . -type f -print0 | head -zn 20)
for i in "$files[@]"; do
something with "$i"
done


With earlier bash versions:



files=()
while IFS= read -rd '' file; do
files+=("$file")
done < <(find . -print0 | head -zn 20)

for i...


Here, it's simpler to use zsh:



files=(**/*(D.[1,20]))
for i ($files) something with $i


(at least the file list will be sorted so the first 20 makes more sense)



See Why is looping over find's output bad practice? for other ways to process (or not) find's output.






share|improve this answer




















  • Thanks; that not only solved my problem, but reading the linked stuff provided a bit of education as well!
    – John
    Oct 3 '17 at 21:08














up vote
4
down vote



accepted










With bash 4.4+:



readarray -td '' files < <(find . -type f -print0 | head -zn 20)
for i in "$files[@]"; do
something with "$i"
done


With earlier bash versions:



files=()
while IFS= read -rd '' file; do
files+=("$file")
done < <(find . -print0 | head -zn 20)

for i...


Here, it's simpler to use zsh:



files=(**/*(D.[1,20]))
for i ($files) something with $i


(at least the file list will be sorted so the first 20 makes more sense)



See Why is looping over find's output bad practice? for other ways to process (or not) find's output.






share|improve this answer




















  • Thanks; that not only solved my problem, but reading the linked stuff provided a bit of education as well!
    – John
    Oct 3 '17 at 21:08












up vote
4
down vote



accepted







up vote
4
down vote



accepted






With bash 4.4+:



readarray -td '' files < <(find . -type f -print0 | head -zn 20)
for i in "$files[@]"; do
something with "$i"
done


With earlier bash versions:



files=()
while IFS= read -rd '' file; do
files+=("$file")
done < <(find . -print0 | head -zn 20)

for i...


Here, it's simpler to use zsh:



files=(**/*(D.[1,20]))
for i ($files) something with $i


(at least the file list will be sorted so the first 20 makes more sense)



See Why is looping over find's output bad practice? for other ways to process (or not) find's output.






share|improve this answer












With bash 4.4+:



readarray -td '' files < <(find . -type f -print0 | head -zn 20)
for i in "$files[@]"; do
something with "$i"
done


With earlier bash versions:



files=()
while IFS= read -rd '' file; do
files+=("$file")
done < <(find . -print0 | head -zn 20)

for i...


Here, it's simpler to use zsh:



files=(**/*(D.[1,20]))
for i ($files) something with $i


(at least the file list will be sorted so the first 20 makes more sense)



See Why is looping over find's output bad practice? for other ways to process (or not) find's output.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 3 '17 at 20:35









Stéphane Chazelas

283k53522859




283k53522859











  • Thanks; that not only solved my problem, but reading the linked stuff provided a bit of education as well!
    – John
    Oct 3 '17 at 21:08
















  • Thanks; that not only solved my problem, but reading the linked stuff provided a bit of education as well!
    – John
    Oct 3 '17 at 21:08















Thanks; that not only solved my problem, but reading the linked stuff provided a bit of education as well!
– John
Oct 3 '17 at 21:08




Thanks; that not only solved my problem, but reading the linked stuff provided a bit of education as well!
– John
Oct 3 '17 at 21:08

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395915%2fextracting-list-of-filenames-including-ones-with-blanks-from-find%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)