LinuxQuestions.org
Visit Jeremy's Blog.
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 05-30-2011, 05:59 PM   #1
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Rep: Reputation: 0
code which lists all files in directory whose size is greater than zero


Quote:
for dir in $*
do
if [ -d $dir ]
then

for file in ls $dir
do
if [ -f $file -a -s $file ]
then
echo $file

fi
done

fi
done
I tried to write a code in which the user is supposed to supply the directory name from the command prompt and the list all the files in that directory & filename is displayed only if the size of the file is greater than zero.

when i execure the above code it displays nothing.
Quote:
~$ ./11ce exercises
where 11ce is the script file and 'exercises' is the directory .
Both script and directory(exercises in this case ) is kept in the same folder.

Where to edit so that the above code produces some output?
 
Old 05-30-2011, 07:45 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
How about
Code:
list=("$@")
[ ${#list[@]} -eq 0 ] && list=(.)
find "${list[@]}" -maxdepth 1 -type f '!' -size 0b
First, all items specified on the command line are saved into array list. Note that this preserves all file names, including those with whitespace.

If none were specified, . (current directory) is used instead.

The find command will look at all files and directories listed in the list array. For directories, it will check their contents, but will not descend into subdirectories. (-maxdepth 0 would tell find to only look at the parameters specified, and not check any directories at all.)

The find command will output the names of all normal files that are not zero-byte-sized.
 
Old 05-30-2011, 11:12 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
A quick note about these:

Code:
for dir in $* ; do

for file in ls $dir ; do
A for loop will default to the current parameter list, so "in $*" is redundant. Also "$@" should be used instead of "$*" in this kind of situation anyway, as you want a series of individual parameters instead of a single string.

As for the second one, you should almost never need to use ls to process a list of files. Regular shell globbing will do the job better.

Code:
for dir ; do

for file in "$dir"/* ; do
Be sure to quote any variable expansions that may contain spaces or other reserved characters in them, to avoid word splitting.
 
1 members found this post helpful.
Old 05-31-2011, 02:05 AM   #4
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
How about if the files printed should be of size greater than 1000bytes.
Quote:
if [ -f $file -a -s $file ]
where

Quote:
-s $file
indicates those files whose size is greater than zero. But if required files of size greater than 1000bytes ,where to make changes then.?

Also I tried to count the number of files in the directory and for it I did these changes:

Quote:

total=0
for dir in $@
do
if [ -d $dir ]
then

for file in "$dir"/*
do
if [ -f $file -a -s $file ]
then
echo $file
total= $( expr $total + 1 )
fi
done

fi
echo "total files : $total "
done
But it generates error :
Quote:
exercises/10aa
./11ce: line 14: 1: command not found
exercises/10ab
./11ce: line 14: 1: command not found
exercises/10ac
total files : 0
What to do?
 
Old 05-31-2011, 09:02 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Pay attention to the error message in the output. You have a syntax mistake in line 14. There can be no spaces around the equals sign when setting a variable.

And again as I cautioned, quote all variable expansions unless you want word-splitting to occur. This is doubly true inside [..] tests. Consider using bash's more advanced [[..]] test operator instead, which doesn't do word-splitting on variables.

"$@" should almost always be quoted too, which will act as if each expanded element is quoted separately. But as I also pointed out, you don't need to use it at all when feeding a for loop.

When doing purely arithmetical operations and comparisons on integers, don't use expr, use $((..)), or ((..)) when no output is required. Things get more complex if you have floating point numbers, as bash can't handle them internally. Then you have to use an external tool like bc or awk.

Note also the $ isn't needed for variables inside arithmetic fields. All non-integer/non-math strings will be automatically interpreted as variables.

To alter your code, you need to get the file size from somewhere and test that. I recommend stat.

Finally, you should get into the habit of using a clean indentation and formatting style, and use liberal comments. Scripts are much easier to debug when they're easily readable.

Here's an example with my suggested modifications.
Code:
#!/bin/bash

# loop through each parameter given
for dir ; do

     #preset the counter to zero
     total=0

     # first test if it's a directory
     if [[ -d $dir ]] ; then

	  # then loop through each name in the directory
          for file in "$dir"/* ; do

	       # if the name is a file, and over 1000 bytes
               # print the name and increment the counter
               if [[ -f $file && $( stat -c %s $file ) -gt 1000 ]] ; then

                    echo "$file"
                    (( total = total + 1 ))

               fi

          done

     fi

     # print the total number of files at the end, and an empty separator line.
     echo "total files : $total"
     echo

done
Of course I'm really only doing this for educational purposes. For real work, something using find like NA's code above would probably be more useful.
 
1 members found this post helpful.
Old 06-01-2011, 04:25 AM   #6
hd_pulse
Member
 
Registered: Dec 2010
Posts: 35

Original Poster
Rep: Reputation: 0
What if the file names to printed in ascending order?
What change to make then?
 
Old 06-01-2011, 07:17 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Use the sort command.
 
1 members found this post helpful.
  


Reply



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
Total particular files size in a directory kirukan Linux - Newbie 3 01-03-2010 02:48 PM
Which directory contains files that normally change their size frequently? Simon Adebisi Linux - Software 4 06-28-2005 03:37 PM
finding size of total files in a directory blackzone Linux - Newbie 3 01-07-2005 03:01 AM
To know the size of all files in a directory ganninu Linux - General 6 08-01-2003 12:33 PM
trying to determine the size of all files in a directory riddlebox80 Programming 7 06-11-2003 07:27 PM

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

All times are GMT -5. The time now is 01:18 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