file and directory deletion script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file and directory deletion script
# 1  
Old 06-29-2009
file and directory deletion script

I saw many post about this but could not find a specific answer to this question.

i have the following code

Code:
find . -depth  -name "$FILEEXT" -ctime -30 -exec rm {} \;

when i type ./deletefiles.sh *.txt for example, it will find all the txt files less than 30 days old and delete them.

my question is, what do i need to add to it to delete the directory ONLY if its EMPTY too... thannks
# 2  
Old 06-29-2009
Lazy version, use rmdir. It works only if dir is empty.
Code:
find . -type d    ....  -exec rmdir {} \;

# 3  
Old 06-29-2009
where would i put that

Where would I put that?
# 4  
Old 06-29-2009
after your file find + rm
1st find remove files = your find cmd
2nd find remove empty directory ... = if you like to add some other options.
find . -type d -exec rmdir {} \;
# - remove empty dirs
In the script usually rm, mv, ... use -f option = and I mean it = force
rm -f {} \;
# 5  
Old 06-29-2009
results

Code:
FILEEXT=$1

find .  -depth  -name "$FILEEXT" -ctime -30  -exec rm -f {} \;
find  -type d -ctime -30 -exec rmdir {}   \;





$ ./deletefiles.sh *txt
rmdir: `.': Invalid argument
rmdir: `./testfiles': Directory not empty
find: ./testfiles/folder1: No such file or directory

it removed the file and folder but i got those errors, i dont want those, what can i do
# 6  
Old 06-30-2009
Quote:
it removed the file and folder but i got those errors, i dont want those, what can i do
Redirect stderr to nowehere:
Code:
FILEEXT=$1
exec 2>/dev/null

find .  -depth  -name "$FILEEXT" -ctime -30  -exec rm -f {} \;
find  -type d -ctime -30 -exec rmdir {}   \;

oh and you should get better performance by piping to exargs instead of exec rm, and GNU's find has an "-empty" test that will evaluate to true if the directory is empty. This leaves the situation where a directory contains only directories to be removed, in which case GNU's rmdir has a -p option that might prove useful.
Code:
find .  -depth  -name "$FILEEXT" -ctime -30  -print |xargs rm -f
find  -type d -empty -ctime -30 -print |xargs rmdir -p

# 7  
Old 06-30-2009
its works..... but

it works, but when i get rid of the argument exec 2>/dev/null , i still get rmdir to few errors and when its deletes an entire folder i get rmdir '.' : invald argument, is there anything i can do so i dont get these errors, or shall i just have them pipe to no where
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

3. Shell Programming and Scripting

Script which removes files from the first directory if there is a file in the second directory

Script must removes files from the first directory if there is a file with same name in the second directory Script passed to the two directories, it lies with them in one directory: sh script_name dir1 dir2 This is my version, but it does not work :wall: set - $2/* for i do set -... (6 Replies)
Discussion started by: SLAMUL
6 Replies

4. UNIX Desktop Questions & Answers

Trying to delete a directory pattern-selective deletion

Trying to delete a directory or a file using a pattern-selective deletion (using “*” and “?” ) (4 Replies)
Discussion started by: herberwz
4 Replies

5. Shell Programming and Scripting

Deletion and recreation within a script

All printers are created on the PROD server and my script must pick up the differences in a diffs.txt file delete the printer if exist on the DEV server recreate it as it currently stands on the PROD server from the diffs.txt The following script I have, should allow me in synching the local... (0 Replies)
Discussion started by: ggoliath
0 Replies

6. Shell Programming and Scripting

Unique Directory and Folder Deletion Script

Ok, so I just got charged with the task of deleting some 300 user folders in a FTP server to free up some space. I managed to grep and cut the list of user folders to delete into a list of one user folder per line. Example: bob00 jane01 sue03 In the home folder, there are folders a-z, and... (5 Replies)
Discussion started by: b4sher
5 Replies

7. Solaris

Script for automatic deletion of old folder

Hi, I have a folder with limited space. So i have to delete folder which are more than 5 days old automatically. So my script should be like delete the folder more than 5 days old. Can someone help me to generate a script for this. Thank you... Cheer Summer (5 Replies)
Discussion started by: summerpeh
5 Replies

8. UNIX for Dummies Questions & Answers

script for deletion using wildcards

find ./ -name t\* | sed "s@^./@@" > .filestobedeleted j=$(wc -l < .filestobedeleted) typeset -i cnt=0 typeset -i i=0 while read line do myarray=$line ((cnt = cnt + 1)) done < .filestobedeleted while (1 Reply)
Discussion started by: aishu
1 Replies

9. Solaris

Script for automatic deletion of trash file of mail server

Hi, I have a mail server with limited space and operating system is sun solaris 8 (sparc). I do not have provisions to increase the space for home directory. So i have to delete files from /home/username/mail/trash which are more than 10 days old automatically. So my script should be like... (1 Reply)
Discussion started by: crown2100bd
1 Replies

10. Solaris

Deletion of Data from Lost+Found Directory

Hie I am running a sun solaris server of about 300 gigabytes disk capacity. The problem is that the machine has been having problems over the past year and at times the machine would just freeze or hang and had to be re-booted. Consequently there are too many entries in the lost+found... (1 Reply)
Discussion started by: Ranganai
1 Replies
Login or Register to Ask a Question