Sponsored Content
Top Forums Shell Programming and Scripting How to delete directories and files with xargs? Post 302903432 by Don Cragun on Tuesday 27th of May 2014 03:52:04 PM
Old 05-27-2014
Quote:
Originally Posted by alister
Your original post uses find -print0 and xargs -0. Is your customer using a GNU userland? If not, those extensions are probably not supported. If they are using a GNU userland and if that's all that needs to be supported, then Don Cragun already gave you the answer.

... ... ...

Regards,
Alister
The + terminator for find -exec is not a GNU userland extension; it has been required by the standards since 1991.

Note, however, that the command I suggested only removes the regular files; not empty directories. The original code in the OP's script:
Code:
find /usr/IBM/HTTPServer/apache_cache/ -name '*' -mtime +1 -print0|xargs -0 rm -r --

which would more efficiently be written as:
Code:
find /usr/IBM/HTTPServer/apache_cache/ -mtime +1 -exec rm -r -- {} +

attempts to remove directories and all of their contents and then to remove some of the files in those directories after they have already been removed while removing their parent directory recursively. Furthermore, if a directory hasn't changed in a day, but files in one or more of it subdirectories changed in the last hour, those directories and the new files in them will also be removed.
A safer way to do it would be a two step process: remove the old regular files in the 1st step and then remove old directories in the 2nd step:
Code:
find /usr/IBM/HTTPServer/apache_cache/ -type f -mtime +1 -exec rm -- {} +
find /usr/IBM/HTTPServer/apache_cache/ -type d -mtime +1 -exec rmdir -- {} +

Using rmdir instead of rm -r guarantees that you won't remove new files accidentally, but you might get error messages trying to remove directories that aren't empty. If that is a concern, you could redirect stderr from the 2nd find. This method will keep directories that have been emptied by the 1st find around for an extra day (since the directory timestamps will be updated when the regular files in them are removed), but they will be cleaned up one day later if they are then empty.

Quote:
Originally Posted by Aia
Thank you for running those.

That's an indication that find is not passing any findings to xargs to execute.

... ... ...
No, it isn't. If no filenames were passed through the pipe to xargs, xargs should not exec rm. Something else is going on here, but it isn't clear to me what.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

delete pattern files in sub directories

Hello friends, I am compiling some set of SQL scripts in a set of sub directories demoed as below. After compiling log files are being created. Each and every time after compiling time I had to go subdir by subdir to delete the log files. I am sure there should be simple way to look for all log... (4 Replies)
Discussion started by: adurga
4 Replies

2. Shell Programming and Scripting

A Batch job to delete files from various directories

Hi, I have a shell script to find files older than 'X' days ($2) in directory path ($1) and delete them. Like this: my_file_remover.sh /usr/home/c 90 Now, I need to modify this script and add it in CRON, so that it checks other directories also. Like: my_file_remover.sh /usr/home/c... (3 Replies)
Discussion started by: guruparan18
3 Replies

3. Shell Programming and Scripting

Search directories for files with zero sizes to delete

Hi, I am trying to write a script that will use ls on a directory and list the files one at a time and their size. If the size is 0 i want it to ask me if I want to delete it (yes or no). If I say yes, I want it to delete but it won't know what the file name is just from running from the script.... (2 Replies)
Discussion started by: akeenabawa
2 Replies

4. Shell Programming and Scripting

Delete files from sub-directories over 7 days

Can any one please help me in deleting all the Files over 7 days from sub-directories A, B, C... Top-Directory Sub-Directory-A File-1 File-2 ..... File-n Sub-Directory-B File-1 File-2 ..... File-n Sub-Directory-C File-1 ... (1 Reply)
Discussion started by: sureshcisco
1 Replies

5. Red Hat

Find files older than 30 days in directories and delete them

Hi, I have dummies questions: My script here can find the files in any directories older than 30 days then it will delete the files but not the directories. I would like to also be able to delete the directories that hold old files more than 30 days not just the files itself. find . -type f... (2 Replies)
Discussion started by: lamoul
2 Replies

6. Homework & Coursework Questions

Find and delete empty files and directories

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Need to make a script, to remove all empty files and folders from current category. It also should show the name... (2 Replies)
Discussion started by: Itixop
2 Replies

7. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

8. Shell Programming and Scripting

Delete files in group of directories

OS: SUNOS 5.10 i386 Hello guys I wrote a shell script in bash shell to delete the files less than 30 days old. The following is the script. ======================================= #!/bin/bash for dirs in `/clu04/oracle/directory_list.lst` do find $dirs -type f -mtime -30 -exec rm {} \;... (3 Replies)
Discussion started by: zacsparrow
3 Replies

9. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

10. UNIX for Beginners Questions & Answers

Delete Directories and its files that begin with the same character string

I wrote a shell script program that supposed to delete any directories and its files that begin with the same character string . The program is designed to delete directories and its files that are one day old or less and only the directories that begin with the same character string. The problem... (1 Reply)
Discussion started by: dellanicholson
1 Replies
All times are GMT -4. The time now is 07:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy