LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-31-2009, 02:57 PM   #1
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Rep: Reputation: 30
bash script to check how many files in directory


hey guys,
i was wondering if there's a bash script to check the amount of files in a directory with an IF statement..

like:

IF directory has less than 100 files
(executes command)
ELSE
Done

Thanks in advance guys!!

I know this is to check if directory is empty:

Code:
#!/bin/bash
DIR="/tmp"
# init
# look for empty dir
if [ "$(ls -A $DIR)" ]; then
     echo "Take action $DIR is not Empty"
else
    echo "$DIR is Empty"
fi
# rest of the logic
But how to check if a directory has less than 100 files

Last edited by guest; 01-31-2009 at 03:17 PM. Reason: example
 
Old 01-31-2009, 03:27 PM   #2
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by guest View Post
But how to check if a directory has less than 100 files
Try "ls -l|wc -l"
 
Old 01-31-2009, 03:32 PM   #3
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
hmm how do i specify it in the script? where does 100 go? i'm super noob at this..
 
Old 01-31-2009, 04:06 PM   #4
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
You could use something like this:


filecount() { echo $# ; }

if [ $(filecount *) -le 100 ]; then

do_this
else
do_that

fi
 
Old 01-31-2009, 04:11 PM   #5
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
but filecount doesn't work in bash?

Last edited by guest; 01-31-2009 at 04:19 PM.
 
Old 01-31-2009, 05:18 PM   #6
alan_ri
Senior Member
 
Registered: Dec 2007
Location: Croatia
Distribution: Debian GNU/Linux
Posts: 1,733
Blog Entries: 5

Rep: Reputation: 127Reputation: 127
Hi!
I just wrote a little script for you.Hope you will find it useful;
Code:
#!/bin/bash
ls -l | grep -v ^l | wc -l > log.txt
log=`ls -l | grep -v ^l | wc -l`
if [ "$log" -ge "100" ]
then
echo "This directory has more then 100 files!"
else
echo "This directory has less then 100 files"
your command
fi
You can name the script 100.sh and just save it in the directory where you want to run it.Just cd into that directory and run;
Code:
./100.sh
If you have some questions,just ask.
 
Old 01-31-2009, 05:45 PM   #7
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
sweet thanks~! what is the "grep -v ^l" for btw?

Last edited by guest; 01-31-2009 at 06:02 PM.
 
Old 01-31-2009, 08:36 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
It's "grep -v ^l"

Print all lines that do not begin with "l"
 
Old 01-31-2009, 11:55 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You guys are leaving out a whole mess of . files:
Code:
if [ $( find -maxdepth 1 | grep -cv '^\.$' ) -lt 100 ]; then
  echo less
else
  echo more or equal
fi
ta0kira

PS The grep pattern will prevent . from showing up.

PPS If you only want real files, add -type f after -maxdepth 1.

Last edited by ta0kira; 02-01-2009 at 12:02 AM.
 
  


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
Bash script to access all files in a directory shinni Programming 5 04-24-2009 03:46 PM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
Traversing files in a given directory using a bash script kaujot Programming 7 03-07-2008 12:39 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
How do I check for directory in bash script? nadavvin Programming 1 09-13-2006 04:03 PM

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

All times are GMT -5. The time now is 11:56 PM.

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