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 have a small script which using the find command will delete certain files with in a given path. All works but now I need to exclude a directory or directories but cant seem to get it to work can anyone help?
-path pattern
File name matches shell pattern pattern. The metacharacters do not treat `/' or `.' specially; so, for example,
find . -path "./sr*sc"
will print an entry for a directory called `./src/misc' (if one exists). To ignore a whole directory tree, use -prune rather than checking every file in the tree. For example, to skip the
directory `src/emacs' and all files and directories under it, and print the names of the other files found, do something like this:
find . -path ./src/emacs -prune -o -print
Note that the pattern match test applies to the whole file name, starting from one of the start points named on the command line. It would only make sense to use an absolute path name here if
the relevant start point is also an absolute path. This means that this command will never match anything:
find bar -path /foo/bar/myfile -print
Presumably, you can specify a non-match in the pattern, i.e.:
Code:
-path ./some/directory* -prune
but I've not tested that. Suggest careful reading of man find...there's LOTS to learn there.
Classic use case for prune - but then you can't use -delete.
Pipe it to xargs for rm. This also allows you to use echo first to test the list you generate - a very prudent step IMHO.
Good point, but simple xargs creates a problem (and even a risk) if there are space characters in file mames.
Rather than
-print0 | xargs -0 rm
I suggest to do everything in find
-exec rm {} +
Have the -prune first and continue with -o (otherwise)!
GNU find's "The -delete action automatically turns on -depth" is a bad idea IMHO.
It still could delete files (and maybe even empty directories), and otherwise would silently fail.
Thanks All for the help. prune seems to have done the job.
Is there a way to record to a log the files that are about to be deleted with their creation date? If I use the -print option, only the processed files are recorded, I could use the find and ls command before the find and delete something like -exec ls -ltr {} + | awk '{print $6,$7,$8,$9}' > $log but this doubles the time it takes to run the script. -print was perfect as it does it all in one go and there is no discrepancy with what was deleted and logged, But having the creation date along with the file name would be ideal as the script works out the older than date so anyone can browse the list to ensure that no files later than the 'older than date' have been deleted.
You can use "-printf '%t %p\n'", which also accepts a very complete list of format directives if you want other information or specific formatting for the timestamp.
Note that none of these suggestions is going to give you "creation time". The best you can do is "modification time". Some filesystems do support "creation time", but support for that is lacking in the tools.
I should add that the disadvantage of using "-printf ..." is that the output is not sorted. The order will be that in which the files were found, and for many filesystems that is essentially unpredictable.
Last edited by rknichols; 04-05-2018 at 05:57 PM.
Reason: I should add that ...
As long as $excludepath is just a single directory (and with no embedded spaces in the name -- you really shold quote variables just to be safe) it looks good to me, though my reputation is somewhat tarnished in this thread. I'd certainly try it without the "-exec rm -f {} +" first, just to see what it would find.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.