LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

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

Rep: Reputation: 0
bash script question


Hi,

I want to store a list of files in a variable

Code:
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.

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

echo $FILELIST
echo $FILECOUNT
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, 03:52 PM   #2
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Dual boot :: Slackware 14.1 64bit multilib | Kali linux 64bit multi lib
Posts: 192

Rep: Reputation: 42
Quote:
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:

Code:
tr ' ' '\012'
Cheers

////
 
Old 10-18-2006, 05:29 PM   #3
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

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

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

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

Hope this helps,

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

Original Poster
Rep: Reputation: 0
Great! Many thanks!!
 
Old 10-18-2006, 05:37 PM   #5
NilsR
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

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

Rep: Reputation: 63
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.

Code:
tempfile=$(mktemp)
find . -type f -name \*.ext > "$tempfile"
echo "FILES:"
cat "$tempfile"
echo "FILECOUNT:"
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"
    fi
done

# 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.
 
  


Reply

Tags
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 03:54 PM
BASH script question drj000 Programming 11 03-07-2005 11:25 PM
Bash script question deiussum Programming 6 08-14-2004 12:43 PM
bash script question xscousr Programming 5 07-03-2003 06:04 PM
Bash script question J_Szucs Linux - General 4 05-29-2003 09:48 AM


All times are GMT -5. The time now is 04:03 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration