[SOLVED] Bash: how to populate a list of arbitrarily named files?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[SOLVED] Bash: how to populate a list of arbitrarily named files?
Hello
How to build a list of files under a directory that may have any permissible characters in the name, that is anything except NUL?
The only possible (?) bash data structure to contain a list of such names is an array because NUL cannot be used as a list item separator so no X-separated list can safely be used; there is no "X" that might not be part of a file name.
OK -- but how to populate such an array? Here's what I've tried.
Code:
#!/bin/bash
# Set up test files
dir=$(mktemp -d "/tmp/${0##*/}.XXXXXX")
touch $dir/foo $dir/bar
# This does populate the array but is useless because assignments set
# in a pipeline are lost after execution
i=0
/usr/bin/find $dir -type f -print0 | while IFS= read -r -d '' filename
do
echo "DEBUG: pipeline: filename is '$filename'"
files[$i]="$filename"
let i=i+1
done
# And this fails. I don't know why; maybe the NUL characters are lost
# in expansion.
while IFS= read -r -d '' filename
do
echo "DEBUG: here string: filename is '$filename'"
files[$i]="$filename"
let i=i+1
done <<< "$( /usr/bin/find $dir -type f -print0 )"
# Remind to remove test files
echo "You probably want to run /bin/rm -fr $dir now"
#!/bin/bash
# Set up test files
dir=$(mktemp -d "/tmp/${0##*/}.XXXXXX")
touch $dir/foo $dir/bar
i=0
for filename in $(ls | xargs)
do
echo "DEBUG: pipeline: filename is '$filename'"
files[$i]="$filename"
let i=i+1
done
Thanks Ian but that will not work for file names including difficult characters such as linefeed and backspace as explained in detail here (and it will not descend into subdirectories).
Actually, that fails for spaces in filenames as well. for thing in $() construct is almost certainly wrong and the exact behavior is very dependent on the value of IFS.
Use this instead:
Code:
while IFS="" read -r -d "" file ; do
files+=("$file")
done < <(find ... -print0)
Actually, that fails for spaces in filenames as well. for thing in $() construct is almost certainly wrong and the exact behavior is very dependent on the value of IFS.
Use this instead:
Code:
while IFS="" read -r -d "" file ; do
files+=("$file")
done < <(find ... -print0)
And as always, Use More Quotes!
Thanks tuxdev
I had just discovered that and came back to this thread to mark it [UNSOLVED] so it was great to find your solution.
How does the done < <( <command> ) construct work? I don't understand "< <".
In case anyone is interested here's the test script (some stylistic changes from tuxdev's suggestion but essentially the same thing).
Code:
#!/bin/bash
# Set up test files
dir=$(mktemp -d "/tmp/${0##*/}.XXXXXX")
touch $dir/foo
touch $dir/'path^Ha^Mlogical'
touch $dir/'name with spaces and tab'
rm -f /tmp/trash
while IFS= read -r -d '' file
do
files+=("$file")
done < <(find $dir -type f -print0)
for ((i=0;i<${#files[*]};i++))
do
echo "${files[$i]}" >> /tmp/trash
done
vi /tmp/trash
Just curious: What does your "arbitrary character file name except null" code do about slash (/) characters in the file names? Oh, wait: You aren't generating the names, just trying to read them, so a slash, in addition to null, would be impossible.
All the << does is use the result of the find command as stdin until it's exhausted. See section 3.6 (Redirection) of info bash for details.
All the << does is use the result of the find command as stdin until it's exhausted. See section 3.6 (Redirection) of info bash for details.
That's what I thought at first -- that < < is the same as << but I tried it and << generates a syntax error whereas < < does what is required.
I would also like to know what the parentheses in files+=("$file") do. Without them, when the /tmp/trash generated by the test script is viewed in vi all the file names are concatenated. With the parentheses, the file names are listed one per line. ???
EDIT: Have understood the parentheses. It is explained in The GNU Bash Reference, 3.6 Shell Parameters. <array name>+=<value> adds <value> to the existing contents of the last element of the array whereas <array name>+=(<value>) adds a new element to the array containing <value>. Nice!
The <(command) is known as process substitution. The results of "command" is temporarily stored in a file (/dev/fd) and then the file is used as input as if it had been classically redirected.
The <(command) is known as process substitution. The results of "command" is temporarily stored in a file (/dev/fd) and then the file is used as input as if it had been classically redirected.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.