![]() |
|
|||||||
| FreeBSD General Other questions regarding FreeBSD which do not fit in any of the categories below. |
![]() |
|
|
Thread Tools | Display Modes |
|
|||
|
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.
__________________
using /LOOKAT/ with /var/db/pkg files and portmaster/aliases/pipes/find/grep to meteorically speedup port upgrades/installs... |
|
|||
|
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
|
|
||||
|
Code:
#!/bin/sh cd $DIR rm -rf * .* 2> /dev/null
__________________
OpenBSD LiveCDs/LiveDVDs |
|
||||
|
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.
__________________
OpenBSD LiveCDs/LiveDVDs |
|
|||
|
Quote:
That link is so cool. All it need is a little translation maybe.Thanks a ton Last edited by sharris; 3rd January 2012 at 08:50 PM. |
|
||||
|
Quote:
__________________
OpenBSD LiveCDs/LiveDVDs |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 01:55 AM |
| strange "~" directory in home directory | gosha | OpenBSD General | 5 | 23rd February 2009 05:12 PM |
| how to copy a directory. | bsdnewbie999 | OpenBSD General | 1 | 12th July 2008 02:36 PM |