|
Home > Archive > Unix Shell > February 2007 > To find the files bigger than the given size
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
To find the files bigger than the given size
|
|
| chweety 2007-02-25, 1:17 pm |
| hi all
In order to list all the files bigger than the given size in the
current directory(not using 'awk'). I written the shell script as
follows
# read size
echo -n "enter size"
read si
# remove first line of output of "ls -l" as it prints total files
which is not necessary
nof=`ls -l|wc -l`
nof=`expr $nof - 1`
#now redirect the output to file "lstest"
ls -l|tail -$nof > lstest
# read the file line by line and check its size
while read a b c d e f g h i
do
if [ $e -gt $si ]
then
echo $i
fi
done <lstest
Above shell script will do required job. Is there any improvements
that can be made to the above shell script.
Thanks in advance
| |
| Janis Papanagnou 2007-02-25, 7:16 pm |
| chweety wrote:
> hi all
> In order to list all the files bigger than the given size in the
> current directory(not using 'awk'). I written the shell script as
> follows
>
> # read size
> echo -n "enter size"
> read si
>
> # remove first line of output of "ls -l" as it prints total files
> which is not necessary
>
> nof=`ls -l|wc -l`
> nof=`expr $nof - 1`
>
> #now redirect the output to file "lstest"
> ls -l|tail -$nof > lstest
>
> # read the file line by line and check its size
>
> while read a b c d e f g h i
> do
> if [ $e -gt $si ]
> then
> echo $i
> fi
> done <lstest
>
> Above shell script will do required job. Is there any improvements
> that can be made to the above shell script.
Yes, there are quite a lot.
Why use 'ls -l' if plain 'ls' will suffice if you send it to 'wc'?
(Simple 'ls' does not have the "total" line.)
But you may drop all the three external processes (ls, wc, subshell)
at all if you let your shell do the file counting
set -- * ## and you have the number of files in variable $#
Another two superfluous and costly external processes (expr, subshell)
are in the next line. Use standard shell features instead
nof=$(( $# - 1 ))
but you don't even need the "-1" because the "total" that you want to
skip is an artifact of the option '-l' of 'ls' and we haven't used it
with 'set -- *' so you can just use plain '$#' where you intended to
use '$nof'.
Instead of creating a temporary file (that you missed to remove when
finished), you could just pipe the output of the file list into the
while command
ls -l | tail +2 | while read a b c d e f g h i
do ...
done
You may have noticed that I even ignored any '$nof' or '$#' variable,
because I instructed 'tail' to start from the second line with option
'+2'. So you yould completely omit all the 'set' and 'nof' handling.
Finally you would get problems with 'echo' if there are files that
start with a '-' (dash), so use 'printf' instead.
My comments should cover most improvements (but not all possible or
even necessary, like fixing the 'read' and double quoting variables).
Hope that helps.
BTW, you may also want to have a look at the 'find' command and it's
options (-size [+|-]n).
Janis
>
> Thanks in advance
>
| |
| Bo Yang 2007-02-26, 1:21 am |
| chweety :
> hi all
> In order to list all the files bigger than the given size in the
> current directory(not using 'awk'). I written the shell script as
> follows
>
> # read size
> echo -n "enter size"
> read si
>
> # remove first line of output of "ls -l" as it prints total files
> which is not necessary
>
> nof=`ls -l|wc -l`
> nof=`expr $nof - 1`
>
> #now redirect the output to file "lstest"
> ls -l|tail -$nof > lstest
>
> # read the file line by line and check its size
>
> while read a b c d e f g h i
> do
> if [ $e -gt $si ]
> then
> echo $i
> fi
> done <lstest
>
> Above shell script will do required job. Is there any improvements
> that can be made to the above shell script.
>
> Thanks in advance
>
I think there is no need to use a script to achieve this task, find can
do it very well.
find . -type f -size +nc -print
n is your desired size in bytes.
Hope this will help you!
| |
| Stephane CHAZELAS 2007-02-27, 7:21 am |
| 2007-02-26, 09:54(+08), Bo Yang:
[...]
> I think there is no need to use a script to achieve this task, find can
> do it very well.
>
> find . -type f -size +nc -print
>
> n is your desired size in bytes.
> Hope this will help you!
[...]
Also, with zsh, you have
ls -ld -- *(LM+1)
for files greater than 1MB,
ls -ld -- *(LK+10)
for files greater than 1kB
ls -ld -- *(L+1234)
for files greater than 1234 bytes
Replace * with **/* to descend into directories or ***/* to also
follow symlinks when descending.
Add "." in the globbing qualifiers for only regular files (or
"-." for symlinks to regular files as well), and "D" for dot
files.
ls -ld -- **/*(D-.L+1234)
finds all regular files (or symlinks to them) whose size
(st_size, not disk usage) is greater that 1234 bytes.
--
Stéphane
|
|
|
|
|