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

Clash 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?
bash find
add a comment |Â
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?
bash find
5
See Why is looping over find's output bad practice?
â Stéphane Chazelas
Oct 3 '17 at 20:32
add a comment |Â
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?
bash find
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
bash find
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
add a comment |Â
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
5
See Why is looping over find's output bad practice?
â Stéphane Chazelas
Oct 3 '17 at 20:32