Printing empty subdirs before delete


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing empty subdirs before delete
# 1  
Old 01-11-2012
Printing empty subdirs before delete

I am using following code to delete all the empty sub dirs from the inputted dir $1.
Before deleting empty dirs, I want to print those dirs which are going to be deleted.
Can this be done with little modification in following code

Code:
#!/bin/sh
if [ $1 -eq '' ]; then
echo "Searching '$1' dir for empty subdirs \n"
fi
find $1 -type d | xargs rmdir -ps
echo "All empty subdirs in '$1' are deleted successfully"


Last edited by radoulov; 01-11-2012 at 02:26 AM.. Reason: Code tags!
# 2  
Old 01-11-2012
Code:
$ find $1 -type d -print -exec rmdir -ps {} \;

# 3  
Old 01-11-2012
Thanks jayan_jay,
This works fine but one problem.
I have dir structure ./a/b/c/d/e/f/g/h ...it displays all of them even if they are not empty...

Quote:
Originally Posted by jayan_jay
Code:
$ find $1 -type d -print -exec rmdir -ps {} \;

# 4  
Old 01-11-2012
check whether your find supports -empty
# 5  
Old 01-11-2012
No it doesnt (:-
I have modified script as below
Code:
#!/bin/sh
if [ $1 -eq '' ]; then
  echo "Searching '$1' dir for empty subdirs \n"
fi
echo "Following  empty dirs are going to be deleted \n"
ls -Rl * | awk '/total 0/{print last}{last=$0}'
find $1 -type d | xargs rmdir -ps
echo "All above empty subdirs in '$1' have been deleted successfully"

Now I need one more help. I need to display " No empty dir exists " is there are no empty dirs.
How to incorporate it in this script.
Thanks,
Ajay
Quote:
Originally Posted by itkamaraj
check whether your find supports -empty

Last edited by Franklin52; 01-11-2012 at 05:01 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 01-11-2012
printing empty subdirs before delete

just tried to tweak jayan code , give it try :
Code:
find $1 -type d -size 0 ac -print -exec rmdir -ps {} \;

SmilieSmilie


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-11-2012 at 05:02 AM.. Reason: Please use code tags for code and data samples, thank you
# 7  
Old 01-12-2012
This is not working . I am getting error
Code:
find: bad option ac
find: path-list predicate-list

Quote:
Originally Posted by mann2719
just tried to tweak jayan code , give it try :
Code:
find $1 -type d -size 0 ac -print -exec rmdir -ps {} \;

SmilieSmilie


Moderator's Comments:
Mod Comment How to use code tags
---------- Post updated 01-12-12 at 12:24 AM ---------- Previous update was 01-11-12 at 04:21 AM ----------

Is there anyway to ls for $1 dir ...currently below ls is feting o/p from current dir.
I want to execute it for $1 dir

Code:
 ls -Rl * | awk '/total 0/{print last}{last=$0}'

Quote:
Originally Posted by ajaypatil_am
No it doesnt (:-
I have modified script as below
Code:
#!/bin/sh
if [ $1 -eq '' ]; then
  echo "Searching '$1' dir for empty subdirs \n"
fi
echo "Following  empty dirs are going to be deleted \n"
ls -Rl * | awk '/total 0/{print last}{last=$0}'
find $1 -type d | xargs rmdir -ps
echo "All above empty subdirs in '$1' have been deleted successfully"

Now I need one more help. I need to display " No empty dir exists " is there are no empty dirs.
How to incorporate it in this script.
Thanks,
Ajay
---------- Post updated at 05:07 AM ---------- Previous update was at 12:24 AM ----------

I actually require script as follows.

STEP 1 count number of empty dirs
STEP 2 if number of empty dirs is 0 echo "There are no empty dirs in this dir"
exit
else
print number of dirs
display all empty dir name
STEP 3 delete all empty dirs

Can any expert here help

Thanks,
A
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete everything after empty line in file

Hello, I am trying to remove everything coming after empty line under ubuntu14.04. How may I do it? file: aaa 020390 bb00id3 c03wisi 209dl x092sia 0290s I expect: aaa 020390 bb00id3 c03wisi (3 Replies)
Discussion started by: baris35
3 Replies

2. UNIX for Dummies Questions & Answers

How to delete some empty folders?

I have an amount of folders and I want to delete only the empty ones. But I have more than 200 empty folders, so I would preffer do not delete one by one... I know it is possible, but I don't know how. I've tried with the size, using 'du' command, and saving the result in a file. After that, I made... (3 Replies)
Discussion started by: saitsug
3 Replies

3. UNIX for Advanced & Expert Users

Delete empty directories recursively - HP-UX

Hi, I want to delete all empty directories in a long directore tree structure. I want to use that from a script that will run on HP-UX 11. My definition of empty directory is that there is no regular file under it and directly beneath it. To elaborate, I have below directories. /app/dev/java... (14 Replies)
Discussion started by: asutoshch
14 Replies

4. Shell Programming and Scripting

Script to delete empty files

I'm trying to write a shell script to files of zero length in a specified directory, but I keep getting errors. Would anybody be kind enough to look it over for issues? Thanks a bunch in advance. #!/bin/sh if then if then find $1 -type f -size 0 -print|xargs rm exit 0... (1 Reply)
Discussion started by: ScriptingIssues
1 Replies

5. Shell Programming and Scripting

delete lines with empty second column

hi, I'd like to ask you if you can help me with such surely easy thing - I have some data and need to plot them. I have txt of data in two columns, tab separated, but some second columns are empty. like 1```` 2 1.2`` 3```` 2 4.44` 5````` 6.1```1 how can I erase all the lines... (2 Replies)
Discussion started by: bsco
2 Replies

6. UNIX for Dummies Questions & Answers

Help required on printing the line if third field is not a empty space

Hi Experts, I want to print the lines whose third field in non-empty/blank space. i.e. INPUT FILE/B] dcdccghjrry0yd cont dcdccttrk820529 cont rdekedfsSCr dcdccttrdky2d0y cont rdekedfsSC2 ... (3 Replies)
Discussion started by: krao
3 Replies

7. UNIX for Advanced & Expert Users

How to delete empty lines

abc# abc#this is a test abc#this is a test to delete abc# xyz# xyz#this is a test two xyz# In the above example '#' is common. How to do delete the emply lines. In specific to observe the output as: abc#this is a test abc#this is a test to delete xyz#this is a test two . . . . (5 Replies)
Discussion started by: Aejaz
5 Replies

8. Shell Programming and Scripting

Delete an empty file from dir.

Hi, I have an dir which has 50K file, some of them are empty. I want to delete the empty file from the dir. I am planning to use the following command. ls -l | grep " 0 " | awk '{print $9}' I can copy this to an file and then how do I use this file to delete the empty files.... Any help... (2 Replies)
Discussion started by: Script40
2 Replies

9. Shell Programming and Scripting

delete multiple empty lines

Hi, I need to delete the lines with empty name. What is the best way to do it? Thanks a lot for your help! EMID MMDDYY HOURS JOB EMNAME 0241 051605 11.40 52062 someone 0520 051605 10.63 52062 0520 051605 10.66 52062 0520 051605 10.65 52062 (3 Replies)
Discussion started by: whatisthis
3 Replies

10. Shell Programming and Scripting

printing an empty line in a file (perl)

I know this must be really easy, but i can't get it to work I've got a perl script, with a file. I want to print an empty line, and the following doesn't seem to work: print nameoffile "\n" thanks for your help!! (3 Replies)
Discussion started by: kfad
3 Replies
Login or Register to Ask a Question