Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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.
Thanks, thesnow, but I need it to be automated (this, and similar commands need to run on 20+ servers.
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.
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)
You should be able to add '-delete' to that once you're happy with it.
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
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)
You should be able to add '-delete' to that once you're happy with it.
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
GazL - The -xdev is a good idea, but it doesn't work by itself. When an NFS mount goes stale (using hard mounting), any "ls" of the parent directory (or any command referencing the NFS link) will hang until the server comes back. That's why we can't go into the /imports directory at all.
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.
find: warning: you have specified the -depth option after a non-option argument (, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
So it unfortunately doesn't do the job. Using xargs seems to be the best option.
Last edited by phrenq; 01-10-2013 at 04:10 AM.
Reason: precised command used
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.