LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 08-04-2010, 08:00 AM   #1
vincent.dang
LQ Newbie
 
Registered: Jan 2010
Posts: 15

Rep: Reputation: 0
how to check folder if it has files in it using Bash Shell script


Good day gurus!
Say, there is a folder. Its empty. When every I drag a new file and put into it it echo out "there is file in there" and keep monitoring the folder. How can I do it?

Before dragging file into the folder it should appear
[admin@linux shell]$
folder is being monitor....


when I drag a file into the folder it say
[admin@linux shell]$
There is file in folder.....

If no action to be taken, after 300 seconds, it appears like
[admin@linux shell]$
There is file in folder.....
There is file in folder.....

And it keeps going on. Its kinda monitoring and warning people

Please advise...
Thanks in advance
 
Old 08-04-2010, 08:06 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Rep: Reputation: 99
Lightbulb

you can use the below script, I am using it for /tmp you can give any directory (absoulte path) here

Code:
fname=/tmp
c=`ls -l $fname | wc -l`
if [ $c -ge 0 ]
then
echo "$fname has file in it !!!"
fi
 
Old 08-04-2010, 11:01 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Here's an idea. Needs folder name passing to the script. Should have error trap for folder not existing, including when script called without being passed the folder name.
Code:
#!/bin/bash

dir=$1
echo "folder '$dir' is being monitor...."

while true
do
    [[ "$( echo "$dir/"* )" != "$dir/*" ]] && echo "There is file in folder '$dir' ....."
    sleep 300
done
Incidentally, "folder" is Windows-speak, is "directory" in Linux-speak.
 
Old 08-05-2010, 09:18 AM   #4
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 209

Rep: Reputation: 66
The linux facility for watching for changes is "inotify", and there's a package inotify-tools to use it from the shell. "inotifywait" in that package does everything you want.
 
Old 08-05-2010, 07:39 PM   #5
vincent.dang
LQ Newbie
 
Registered: Jan 2010
Posts: 15

Original Poster
Rep: Reputation: 0
thanks for your help. I appreciate it
 
Old 08-05-2010, 08:24 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by vikas027 View Post
you can use the below script, I am using it for /tmp you can give any directory (absoulte path) here

Code:
fname=/tmp
c=`ls -l $fname | wc -l`
if [ $c -ge 0 ]
then
echo "$fname has file in it !!!"
fi
the result of ls -l on an empty directory gives "total 0". If you pipe to wc, it will be counted as one.
 
Old 08-05-2010, 08:49 PM   #7
vincent.dang
LQ Newbie
 
Registered: Jan 2010
Posts: 15

Original Poster
Rep: Reputation: 0
However, I come up with this solution:
Quote:
while true; do
if [ "$(ls -A $dir)" ]; then
echo "--------------------------------------------"
echo "File exists..."
for var in $dir/*; do
echo "The file name is: $(basename $var)"
done
else
echo "There is no file in this directory.."
echo "the directory is being monitored"
fi
echo "refresh after 10 seconds"
sleep 10
done
The above is what exactly I need...
Anyway, thanks for you helps guys!!!
 
Old 08-05-2010, 08:58 PM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Nice work - you got a working solution. If you are happy with that, you can mark this [SOLVED] using "Thread Tools" above the first post.

P.S. - it might make for a nicer (easier to read) display output from the script, if you put a "clear" command after the "sleep" command.

Cheers,
Sasha
 
  


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
[SOLVED] Bash shell script to check the if empty files are being created & start the process hussa1n Linux - General 8 06-29-2010 06:49 PM
bash script that will check folder x for new folders. Techno Guy Linux - Newbie 14 08-08-2009 11:50 AM
Check if actions in bash shell script are performed well proNick Linux - Newbie 6 02-09-2009 08:25 PM
BASH Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 11:27 AM


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