|
FreeBSD General Other questions regarding FreeBSD which do not fit in any of the categories below. |
|
Thread Tools | Display Modes |
|
|
|||
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/* Code:
if directory_not_empty do_something else continue Thanks in advance |
|
|||
Code:
/bin/rm -rf /temp_1 mkdir -p /temp_1 # if you wanted it to remain.
__________________
FreeBSD 13-STABLE |
|
|||
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 |
|
|||
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 Code:
cd /test_directory if ["`ls`" ] then echo "no files found." else echo "files found." fi |
|
|||
Quote:
Thanks a ton Last edited by sharris; 3rd January 2012 at 09:50 PM. |
|
||||
Quote:
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. |
|
||||
Quote:
|
|
|
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 |