find with -prune and -delete
Howdy!
I am trying to go through my filesystem, looking for files that end in ~ (back up files from Emacs) and delete them. The tricky bit is that I have some NFS-mounted directories in /import that sometimes go stale, so I don't want to even go into that directory at all. I have the following, which prints out everything nicely: Code:
find / -path /import -prune -o -regex ".*~" -type f -ctime +5 -ls I thought about using something like: Code:
find / -path /import -prune -o -regex ".*~" -type f -ctime +5 -exec rm -rf {} \; Thanks!! Seth |
I would use your find command to generate a list of target files (saved to a file), review the list and make any edits, then do a for loop on the list for the deletes.
|
Quote:
|
I don't see why your second one wouldn't work. The names are all handled internally and are never subjected to shell parsing, so odd filenames shouldn't be a problem.
The other usual option is to pipe the names into xargs, using null separators to avoid shell word-splitting. Code:
find / -path /import -prune -o -regex ".*~" -type f -ctime +5 -print0 | xargs -0 rm -f On another note,I wonder if bracketing the the two expressions separately would keep them from affecting each other. Code:
find / \( -path /import -prune \) -o \( -regex ".*~" -type f -ctime +5 -delete \) |
An alternative approach would be to use the '-xdev' option to stop find crossing mountpoints, but you'll need to explicitly list the filesystems you do want it to process (which is possibly not a bad thing to do anyway)
e.g. for /, /home, and /var (assuming you have a separate /home and /var filesystem - which I always do) Code:
find / /home /var -xdev -regex ".*~" -type f -ctime +5 This approach will also prevent it needlessly traversing /proc and /sys and other filesystems that you're not interested in without having to prune them out |
Quote:
|
Quote:
|
Ahh, ok, I'd assumed that -xdev would stop it from running stat() on the mountpoints, I guess it just stops it descending into them. Thanks for the feedback, I'll have to remember that one. :)
If you're using the xargs solution, you'll want to add the '-r' switch for when there aren't any matches. |
Quote:
Code:
find / \( -path /import -prune \) -o \( -depth -regex ".*~" -type f -ctime +5 \): Quote:
|
All times are GMT -5. The time now is 06:38 PM. |