DaemonForums  

Go Back   DaemonForums > FreeBSD > FreeBSD General

FreeBSD General Other questions regarding FreeBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 3rd January 2012
sharris sharris is offline
Package Pilot
 
Join Date: Jun 2010
Posts: 146
Thanked 0 Times in 0 Posts
Default How do we verify if directory is empty?

How do one verify that a directory is empty using an if else statement (script.sh). So far I use:

Code:
rm -rf /temp_1/*
... but if there is hidden files or files that has a format like this (.file) BSD will not delete it.

Code:
if directory_not_empty
do_something
else
continue
How is this done?

Thanks in advance
Reply With Quote
  #2   (View Single Post)  
Old 3rd January 2012
jb_daefo jb_daefo is offline
Package Pilot
 
Join Date: May 2008
Posts: 201
Thanked 5 Times in 5 Posts
Default

Code:
 /bin/rm -rf /temp_1
mkdir -p /temp_1 # if you wanted it to remain.
might work. One typographical error, though, and you'd best restore from backups... as you probably are aware. (I don't use your second method hardly ever).
__________________
FreeBSD v9... pkg2ng somewhat working...
Reply With Quote
  #3   (View Single Post)  
Old 3rd January 2012
sharris sharris is offline
Package Pilot
 
Join Date: Jun 2010
Posts: 146
Thanked 0 Times in 0 Posts
Default

jb_daefo, that's a good solution for the immediate problem, I was doing that all day practicing (deleting the entire directory and create an new one, filing it with new data, etc, so that I don't screw up elsewhere) but than it came to me, there may be an need to *NOT* touch the directory and only remove the files with-in, etc or use only wild-cards style for whatever reason... Than all of a sudden, just hours ago, it got very interesting to me. For what , I don't know but now I'm hook. I always get stuck on stupid but it usually pay-off in the long run. Anyway, just after posting this questions, google FINALLY turn up a decent clue after all that searching, coming up with zip for a simple question, using 20 keywords or more: unbelievable!!!

This is really something:
http://xpt.sourceforge.net/techdocs/...dCheck/single/

The problem is how to point to my directory with-out all of that code. I tried lots of ways to replace/work around with [-z] with many test directory but still can't get it to work:

Code:
> check if there is any file exist in /home/my_directory
if [ -z "`ls`" ]
        then
        echo "no files found."
else
        echo "files found."
fi
Reply With Quote
  #4   (View Single Post)  
Old 3rd January 2012
sharris sharris is offline
Package Pilot
 
Join Date: Jun 2010
Posts: 146
Thanked 0 Times in 0 Posts
Default

First off, this week is my first real try at shell scripting so excuse me for carrying on, I'm just a little excited. If anyone has tried this already, it's easy to over look the results but this old 1999 script IS working because it return/echo the files inside the brackets [.this file that file]

I remove the -z and placed cd /test_directory over the if/else statement. If no cd command is used it will return a list of what is in the directory the script is called from. It's a start but it shouldn't stop there.

The first problem is to get BSD not to generate the "No such file or directory" or "command not found" error because your results are in the bracket anyway.

Code:
./isEmpty.sh: line 10: [.this file that file]: command not found
file found
This cause the script to exit to the *else* statement telling you *file found* even if the directory is empty . That's all I figured out so far. How do you suppress or even turn-off shell scripting error? I would have no problem living with-out it since you can do that with (if/else) just like other programming languages. My guest is it will work just because of that.

Code:
cd /test_directory
if ["`ls`" ]
        then
        echo "no files found."
else
        echo "files found."
fi
Reply With Quote
  #5   (View Single Post)  
Old 3rd January 2012
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 3,886
Thanked 214 Times in 189 Posts
Default

Code:
#!/bin/sh
cd $DIR
rm -rf * .* 2> /dev/null
Completely untested. The intent is to delete all regular and all dot files, suppressing the stderr message complaining about "." and ".." not being deleted.
Reply With Quote
  #6   (View Single Post)  
Old 3rd January 2012
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 3,886
Thanked 214 Times in 189 Posts
Default

I should also point out that my little untested script doesn't use /bin/rm, and it should. It will also fail on any files that can't be rm'ed due to to permissions or immutability settings. It also doesn't test the cd; if that fails you could be clearing out the wrong directory.

I would think find(1) piped through xargs(1) for rm(1) would be a better way of managing the deletions. No need to even use cd(1).

No bourne shell handy today for testing any of these ideas, though.
Reply With Quote
  #7   (View Single Post)  
Old 3rd January 2012
sharris sharris is offline
Package Pilot
 
Join Date: Jun 2010
Posts: 146
Thanked 0 Times in 0 Posts
Default

Quote:
jggimi wrote:: I would think find(1) piped through xargs(1) for rm(1) would be a better way of managing the deletions. No need to even use cd(1).
I get it now! You are right because I did a lot at the command-line using the *find command* early last year. I'm burn-out now from googling and studying scripting tutes all night long. Scripting is fun. I plan to post a few clever scripts based on jb_daefo code & ideas, yours code/suggestions, and the link, hopefully before this month is out even if someone beat me to the punch That link is so cool. All it need is a little translation maybe.

Thanks a ton

Last edited by sharris; 3rd January 2012 at 09:50 PM.
Reply With Quote
  #8   (View Single Post)  
Old 4th January 2012
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,088
Thanked 198 Times in 156 Posts
Default

Quote:
Originally Posted by jggimi
I would think find(1) piped through xargs(1) for rm(1)
find has -delete for deleting stuff ...
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #9   (View Single Post)  
Old 4th January 2012
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 3,886
Thanked 214 Times in 189 Posts
Default

Quote:
find has -delete for deleting stuff ...
Wonderful. My particular *BSD's find(1) doesn't.
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Snapshot Packages Empty - part of commit? IronForge OpenBSD Packages and Ports 3 16th September 2011 10:32 PM
How-To get a directory count sharris FreeBSD General 3 29th April 2011 10:15 PM
bridge no such directory hehehehe OpenBSD General 1 15th December 2009 02:55 AM
strange "~" directory in home directory gosha OpenBSD General 5 23rd February 2009 06:12 PM
how to copy a directory. bsdnewbie999 OpenBSD General 1 12th July 2008 02:36 PM


All times are GMT. The time now is 11:18 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick