LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2010, 03:29 AM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
[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"

Last edited by catkin; 03-14-2010 at 02:59 PM.
 
Old 03-14-2010, 03:39 AM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
What about:-
Code:
#!/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
 
Old 03-14-2010, 03:58 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by blacky_5251 View Post
What about:-
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).
 
Old 03-14-2010, 05:40 AM   #4
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
How about this then:-
Code:
for filename in $(find $dir -type f -print0 | xargs -0)
do
  files[$i]="$filename"
  let i=i+1
  echo DEBUG "$filename"
done
 
1 members found this post helpful.
Old 03-14-2010, 06:04 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks Ian That works perfectly.
 
Old 03-14-2010, 11:44 AM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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!

Last edited by tuxdev; 03-14-2010 at 11:55 AM.
 
1 members found this post helpful.
Old 03-14-2010, 12:05 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by tuxdev View Post
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
 
Old 03-14-2010, 12:52 PM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 03-14-2010, 01:04 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by PTrenholme View Post
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!

Last edited by catkin; 03-14-2010 at 01:11 PM.
 
Old 03-14-2010, 02:46 PM   #10
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
Process Substitution

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.
 
1 members found this post helpful.
Old 03-14-2010, 02:58 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578

Original Poster
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by blacky_5251 View Post
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.
Thanks Ian Given that info, found the reference at http://www.gnu.org/software/bash/man...s-Substitution. Always something to learn about bash!
 
  


Reply

Tags
bash, filename



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need bash script to list files, drop extension and dump to file talwar_ Programming 10 06-03-2011 09:18 AM
[SOLVED] bash command or sript to list files sepi Linux - Software 7 08-25-2009 04:15 PM
How do you list dot files only in bash? sysslack_linux General 7 11-10-2007 01:19 PM
Bash scripting problem: Can't get a list of all files, including hidden ones oxi Programming 24 03-12-2007 06:19 AM
A list of files I own in bash? subnet_rx Linux - Software 4 07-10-2006 12:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration