remove empty directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove empty directory
# 1  
Old 02-29-2008
remove empty directory

Hi,
I need to delete an empty directory in a temp directory except "dir5" (keep everything that is not empty).
Plese advise.

Here is an example of my directory.

/dir/temp/
dir1 - delete if this is empty
dir2 - delete if this is empty
dir3 - delete if this is empty
dir4 - delete if this is empty
dir5 - I don't want to delete this folder at all.

Here is what I got so far

Code:
if [ "$(ls -1 /dir/temp |grep -v "dir5")" ]
then
        echo "directory is Not Empty"
        rmdir $(ls -1 /dir/temp |grep -v "dir5")
else
        echo "this directory is Empty"
        rmdir $(ls -1 /dir/temp |grep -v "dir5")
fi


Last edited by Yogesh Sawant; 04-09-2009 at 08:22 AM.. Reason: added code tags
# 2  
Old 02-29-2008
This should do it:

Code:
#!/bin/sh

for dirname in `find /tmp -name "dir[^5]" -type d`
do
        # see if directory is empty
        if [ "$(ls $dirname)" ]
        then
                echo "$dirname is not empty; no action taken"
        else
                rmdir $dirname
        fi
done


Last edited by matt.d; 02-29-2008 at 01:57 PM.. Reason: Simplified regex / simplified "if" condition
# 3  
Old 03-03-2008
Perfect...thank you so much.
# 4  
Old 03-04-2008
Hi,
>find /tmp -name "dir[^5]" -type d
Whats the purpose of using "dir[^5]" here?

We can also get the desired result as follows.

find /tmp -name "dir5" -type d

any idea pls.

cheers
RRK
# 5  
Old 03-04-2008
Code:
find /tmp -name "dir[^5]" -type d

gives you all directories that begin with "dir" and end in something other than 5.
Code:
find /tmp -name "dir5" -type d

gives you only directories named "dir5." The original poster wanted to remove all empty directories, and keep dir5 regardless of whether or not it contained files.
# 6  
Old 03-04-2008
unless there is actually a dir5, otherwise -name dir[^5] might not work.
Code:
find . -type d ! -name dir5 -exec rmdir "{}" \; > /dev/null 2>&1

# 7  
Old 03-04-2008
yes,you are correct!


cheers
RRK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove empty files in home directory

how to remove empty files tried below command its remove only zero bytes not empty file which is greater then zero byte. for x in * do if then rm $x fi done (8 Replies)
Discussion started by: Kalia
8 Replies

2. Shell Programming and Scripting

How to remove empty line.?

Hi gurus, I have a script which works fine. https://www.unix.com/shell-programming-and-scripting/239347-how-pass-string-into-sql-query.html while read p do && para="'${p}'" || para="${para},'${p}'" done < filePlease use code tags as required by forum rules! a few days... (6 Replies)
Discussion started by: ken6503
6 Replies

3. Shell Programming and Scripting

Remove CR only on empty lines

Dear community, I have two output files that contains some CR # cat first.out 1234567890 598679857648566 9 1234567234 365837465873465 4 2342343243 289374982374894 4 # cat second.out 2342342342 ... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

4. Shell Programming and Scripting

Remove empty records

Hello: Is there a simple way to remove empty records of FASTA format file? A FASTA format consists of two parts: header and sequence (for non-biologist, Wiki for details of course!). The header always start with ">" for the name of the sequence. The header must be in this ONLY single line.... (9 Replies)
Discussion started by: yifangt
9 Replies

5. UNIX for Dummies Questions & Answers

remove empty field

Hi all ! I'm sure it is a basic question but I didn't find any threads that fit my need. How to remove empty fields with awk? Or in other words, how to shift all the fields after an empty field on the left? input: 1|2||3|4|5||6 wanted: 1|2|3|4|5|6 I tried: awk '{for(i=1; i<=NF;... (7 Replies)
Discussion started by: lucasvs
7 Replies

6. Shell Programming and Scripting

Remove empty line and the next one

Hi all, I'm trying to remove when this condition is met: an empty and the next one I'm using this command: sed '/^$/N; s/&//' file Which searches for an empty line, N attaches it to the next line, and substituing the combination with nothing.... but it is not working. What I'm missing... (1 Reply)
Discussion started by: meuser
1 Replies

7. Shell Programming and Scripting

using vi -c to remove empty lines

Hello: I searched here for "vi -c" but found no hits. How can I use vi -c to remove ALL empty lines, regardless of how many? I tried <code> vi -c ":g/^$/d | wq" filename </code> but I have to run it several times. This is NOT homework. :) Thanks for your time. (3 Replies)
Discussion started by: Habitual
3 Replies

8. UNIX for Dummies Questions & Answers

Remove only Empty Directories

I know this one was answered before in forum below - https://www.unix.com/unix-dummies-questions-answers/58210-removing-empty-folders-using-find-command.html But that one is closed & I have a question so here it goes. I want to delete all 2006 files. Now if along with the files, if the... (2 Replies)
Discussion started by: kedar.mehta
2 Replies

9. Shell Programming and Scripting

Script to remove all empty files within the directory structure?

Hi I need to write a shell script which basically searches for all the empty files within the directory structure, lists them before asking the user to confirm if they would like to delete them. If the user deletes the file then a notice would appear confirming the file is deleted. I've be... (5 Replies)
Discussion started by: cat123
5 Replies

10. Solaris

Remove non empty dirctory

Hi, Any command or means to delete a director which is not empty rmdir or similar iam using Sun Solaries 2.6 :confused: I have many full directories with subdirectories and I can not go on emptying them all (5 Replies)
Discussion started by: adol3
5 Replies
Login or Register to Ask a Question