Printing empty subdirs before delete


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing empty subdirs before delete
# 15  
Old 01-13-2012
Thanks itkamaraj for your help. It is working fine from command prompt but when I write it in the script I am getting
rmchk.sh: syntax error at line 1: `(' unexpected
Script
Code:
 
count=`find . -type d | while read d; do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d; done | wc -l`
echo $count

Quote:
Originally Posted by itkamaraj
try this..

Code:
 
find . -type d | while read d; do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d; done | wc -l

# 16  
Old 01-13-2012
spoon feeding Smilie

read my previous posts and do the change for script
# 17  
Old 01-13-2012
Following code is working fine in bash but it is not working fine in sh
strange ! Smilie
Code:
#!/bin/bash

#Count number of subdirs

Code:
 
count=`find . -type d | while read d
do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d
done | wc -l`
echo "Number of empty subdir = $count "

#if count of empty subdirs is 0 exit the script 
if [ $count -eq 0 ]
then
echo "Exiting the script as there are no subdirs to delete "
exit 1
fi

for sh i am getting rmchk.sh: syntax error at line 1: `(' unexpected Smilie
Now I want to display and then delete all empty dirs ...efficent way to delete is
find . -type d | xargs rmdir -ps but not sure how I can print empty dir names before deletion
trying following
find . -type d | while read d; do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d; done ; -print
Thanks,
Ajay

Quote:
Originally Posted by itkamaraj
spoon feeding Smilie

read my previous posts and do the change for script
---------- Post updated at 06:09 AM ---------- Previous update was at 02:24 AM ----------

Final Script : May be useful for all
Code:
#Validating dir parameter
if [ $# -eq 0 ]
then
echo "No parameters detected"
exit 1
fi

#Change dir to the dir to the parameter dir
cd $1

#Cleaning the log file
echo "" >> log.txt
echo "Checking $1 for empty subdirs"

#Count number of subdirs
count=`find . -type d | while read d
do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d
done | wc -l`
echo "Number of empty subdir in $1 = $count " > log.txt

#If count of empty subdirs is 0 exit the script
if [ $count -eq 0 ]
then
echo "Exiting the script as there are no subdirs to delete " >> log.txt
echo "`cat log.txt`"
mailx -s "Empty subdirs deleted at $1 on `date`" somone@somesite.com < log.txt
exit 1
fi

#Displaying all empty subdirs
find . -type d | while read d; do [ $(( $(ls -a1 $d | wc -l) >= 3 )) == 0 ] && echo $d; done >> log.txt

#Deleting all the empty subdirs
find . -type d | xargs rmdir -ps
echo "All the empty subdirs in $1 are deleted successfully" >> log.txt
echo "`cat log.txt`"

#Sending email to team
mailx -s "Empty subdirs deleted at $1 on `date`" someone@somesite.com < log.txt

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