Visit Jeremy's Blog.
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 12-07-2010, 09:22 PM   #1
LQ Newbie
Registered: Sep 2010
Posts: 4

Rep: Reputation: 0
Shell Script that uses ls -l

I am trying to write a script that uses ls -l to print out a file if the size is larger than the parameter that is passed in.
This is what I have so far

for i in $1
	   file=`ls l $i`
             sizelimit=`ls l $i | awk {print $5}`
		if [[ -f $i && $2 lt $sizelimit ]]
		      echo $file
the $1 parameter will be the directory to look in and $2 parameter will be the size to compare with. I know that the size of the file is in the 5th column when you do an ls -l. This is a lab for a class and if I can't be helped because of this I will understand. I have just been working on it for 2 days and it runs but nothing is printed to the screen.
Old 12-07-2010, 10:33 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
The 1st for loop i will be the dir name as a string ie bash doesn't know its a dir, so that won't work.

If you run this
file=`ls –l $i`
from the cmd line by hand, you'll see it lists ALL the files at once... this isn't what you want.

You can add
set -xv
to the top of your file to see exactly what's really happening.

Useful texts

You may find this enlightening

Old 12-07-2010, 10:57 PM   #3
Senior Member
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Are you required to use the ls command? If not I recommend using the find command. It has a -size parameter and a -name parameter. You can specify files larger than a particular size by adding the + to the size.
find /directory -type f -size +100M -name chair*
This will find all of the regular files (type f) in the directory named /directory that are more than 100 megabytes in size and whose name begins with "chair".

The find command is a wonderful tool to select files of various characteristics.
man find

Last edited by stress_junkie; 12-07-2010 at 10:59 PM.
Old 12-08-2010, 02:19 PM   #4
LQ Newbie
Registered: Sep 2010
Posts: 4

Original Poster
Rep: Reputation: 0
for i in $1
if [ -f $1 ]
             sizelimit=`ls l $i | awk {print $5}`
		if [[ $2 lt $sizelimit ]]
		      echo $i
thanks for the help I fixed it byt using another if statement and changed what was printing out.


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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM > Forums > Linux Forums > Linux - Newbie

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