LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 12-07-2010, 10:22 PM   #1
newtolinux78
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

Code:
for i in $1
	do 
	   file=`ls l $i`
             sizelimit=`ls l $i | awk {print $5}`
		if [[ -f $i && $2 lt $sizelimit ]]
		   then 
		      echo $file
		fi
	done
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, 11:33 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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
Code:
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
Code:
set -xv
to the top of your file to see exactly what's really happening.

Useful texts
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

You may find this enlightening
http://linux.die.net/man/1/find

 
Old 12-07-2010, 11:57 PM   #3
stress_junkie
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.
Example:
Code:
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.
Code:
man find

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

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


Reply


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 08:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 02:45 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 01:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


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