deleting files in directories but with exclusions of some directories
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
deleting files in directories but with exclusions of some directories
Hi,
I'm trying to delete files older then 1 year on a server with +1000 vhosts.
location of the files:
/var/www/vhosts/CLIENT1/httpdocs/files/fotos/
/var/www/vhosts/CLIENT2/httpdocs/files/fotos/
...
/var/www/vhosts/CLIENT1000/httpdocs/files/fotos/
I used to do this with a simple:
find /var/www/vhosts/ -type f -name '*.jpg' -mtime +356 -exec rm {} \;
This worked very good but now some clients don't want these files removed.
Is there a way to exclude multiple /CLIENTX/httpdocs/files/fotos directories?
It would be great if I could put these in a blacklist.txt or something which i can simply adjust before running the removal command.
I'm new here and relatively new to linux, if i did not post this in the right forum, my excuses!
If that works, then append a -delete after the -print option. For the types of regular expression patterns supported, try find -regextype help or see the manual page.
Otherwise you can make a shell script to generate the find command.
Edit: There is an implied logical AND between all the find options. It doesn't have to be written but it is there.
Last edited by Turbocapitalist; 10-02-2020 at 07:35 AM.
tnx for the answers. I must say that unfortunately regex does not apply because the client names are all different (like companynames)
I'm looking for a nice overview also, so i can quickly read the clients who are excluded, instead of one long sentence with all directories after another one.
Mixing up some solutions provided by you, would this work then? (i will definitely test this also myself in a testing environment, but a quick hint is always welcome :-)
I'm not sure there's an easy way with find, if you are reading a file containing excluded directories. You could pipe things through some other utilities though and maybe the combined result does what you want.
The file 'directories.to.exclude.txt' would then contain the directory patterns to exclude. So it would be good to anchor them to the start of the line with a caret ^ on each one and treat them as absolute paths rather than relative paths.
it looks like (for me) we need a client specific setup/config and a script to process them one by one.
I would probably use something else instead of find and shell: perl/python/...
> I'm trying to delete files older then 1 year on a server with +1000 vhosts.
#1 be VERY CAREFUL. PC's are infamous for "occasionally having the wrong time", either the PC clock is off at boot, or some files (that you moved or copied) have a wrong date (perhaps they were altered when the PC clock was off), etc.
in general: never do backups by time unless you don't care what is lost
Hi,
I'm trying to delete files older then 1 year on a server with +1000 vhosts.
location of the files:
/var/www/vhosts/CLIENT1/httpdocs/files/fotos/
/var/www/vhosts/CLIENT2/httpdocs/files/fotos/
...
/var/www/vhosts/CLIENT1000/httpdocs/files/fotos/
I used to do this with a simple:
find /var/www/vhosts/ -type f -name '*.jpg' -mtime +356 -exec rm {} \;
This worked very good but now some clients don't want these files removed.
Is there a way to exclude multiple /CLIENTX/httpdocs/files/fotos directories?
It would be great if I could put these in a blacklist.txt or something which i can simply adjust before running the removal command.
I'm new here and relatively new to linux, if i did not post this in the right forum, my excuses!
It would seem to me very easy to create a file with a list of all the clients who desire that the files be deleted in the format
CLIENT1
CLIENT2
CLIENT3
etc.
Yes, the initial creation of the file may take a while if done manually, but only a few minutes if done with a script which could pull the client name from the directory names, and maintenance should be easy.
Then add a while loop to the find command which reads the client name from the file, one at a time, and for each client do the find and delete similar to
Code:
read CLIENT from clientfile
while not end of clientfile
do
find /var/www/vhosts/$CLIENT/httpdocs/files/fotos/ -type f -name '*.jpg' -mtime +356 -exec rm {}
read CLIENT from clientfile
done
something like this would make it easy to add or remove clients from the list whose files would be deleted by a simple edit of the client file.
I was going to suggest basically the same thing; create a whitelist of those that DO need to be deleted and loop through it - much simpler than all that fancy negative matching; K.I.S.S
This would also be a good time to add a cmd at the end to check the disk space, as the uncleared companies' files are going to eat up the disk ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.