Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-18-2006, 02:25 PM   #1
LQ Newbie
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Rep: Reputation: 0
bash script question


I want to store a list of files in a variable

FILELIST="$( find . -name *.ext )"
and later in the script I want to print the list to the user and count the number of files in the list.

FILECOUNT="$( echo $FILELIST | wc -l )"

That doesn't work tho because the list is separated by white spaces instead of new lines, so the print out is ugly and line count is always equal to 1. How can I work around this?

Thanks in advance!
Old 10-18-2006, 02:52 PM   #2
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: win 10 | OpenBSD 6.3 bridge | Fedora 28 | Fedora 27 Server
Posts: 341

Rep: Reputation: 126Reputation: 126
Originally Posted by armandino101
That doesn't work tho because the list is separated by white spaces instead of new lines
You can change spaces to newlines with this command:

tr ' ' '\012'

Old 10-18-2006, 04:29 PM   #3
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
This isn't very elegant, but here goes

FILELIST="$( sed 's/ /\\n/g' < `find . -name '*sh'` )"
Should populate $FILELIST with your files in by line. As for the $FILECOUNT you could use:

FILECOUNT="$( find . -name '*sh' | wc -l )"
and of course you can echo both those variables to get your output.

Hope this helps,

Old 10-18-2006, 04:33 PM   #4
LQ Newbie
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Original Poster
Rep: Reputation: 0
Great! Many thanks!!
Old 10-18-2006, 04:37 PM   #5
LQ Newbie
Registered: Jan 2005
Location: Oslo, Norway
Distribution: Debian GNU/Linux
Posts: 2

Rep: Reputation: 0
wc means 'word count'

$ wc --help
gives you an answer: Use wc -w instead of wc -l

Old 10-18-2006, 05:32 PM   #6
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 64
There's two things to watch for here.
  1. File with spaces in the name. In this case you'll want to be using wc -l not wc -w (-l means count nunmber of lines of output, -w means count the words as separated by whitespace).
  2. If you run two commands, there is a chance that the files will change between the two runs. Not a problem if you're just printing some rough stats, but if you're using the count to iterate over the list there could be trouble if the number of files changes between the two operations.

Another approach is to create a temporary file with the list of files in it, e.g.

find . -type f -name \*.ext > "$tempfile"
echo "FILES:"
cat "$tempfile"
cat "$tempfile" | wc -l

# you can iterate over the list like this:
cat "$tempfile" | while read f; do
    # here's something weird to do, just as an example
    if [ -w "$f" ]; then
        echo "writable: $f"

# don't forget to delete the tempfile when you're done
rm -f "$tempfile"
Another tip for handling lists of files - find out about xargs - it's great.


bash, scripting

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash script question edward_scott Linux - Newbie 4 08-26-2006 02:54 PM
BASH script question drj000 Programming 11 03-07-2005 10:25 PM
Bash script question deiussum Programming 6 08-14-2004 11:43 AM
bash script question xscousr Programming 5 07-03-2003 05:04 PM
Bash script question J_Szucs Linux - General 4 05-29-2003 08:48 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:00 AM.

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