How to exclude multiple subdirectories (same directory name) when using find command to delete files older than 30 days in a batch file? - linux

Given the below linux directory structure, how would I skip each excluded directory, (/assess), to remove files older than 30 days for the rest of the entire directory structure?
Thanks for your assistance...
/mnt/nfsmountpoint/location1/appliance1
/assess
/discover
/bkup
/mnt/nfsmountpoint/location1/appliance2
/assess
/discover
/bkup
etc...
I cobbled together an answer and proofs:: (my asterisks * are not showing sorry)
Run from the /mnt/nfsmountpoint/ directory.
find .// -not -path "/assess/" -type f -mtime +30 -delete
validate::
Does it skip the directory?:
find .// -not -path "/assess/" -type f -mtime +30 -ls|more
Verify no current month (January 2021) files included?:
find .// -not -path "/assess/" -type f -mtime +30 -ls|grep Jan
How much space is made free?:
find .// -not -path "/assess/" -type f -mtime +30 -print0 | du --files0-from=- hc | tail -n1

find /path/to/dir -mtime +30 -type f -not -name "*assess*" -delete
Find files (-type f) in /path/to/dir as well as children directories. Specify only files that have been modified more than 30 days ago (-mtime +30) and do not include files that contain "assess" (-not -name "assess")

Related

Delete files older than 30 days, but in 1 directory the retention 6 months

I'd like to create a find which delete a files older than 30 days, but I have 1 directory where the retention should be 6 months.
How would that be possible?
This one would delete all files in all subdirectories which is older than 30 days if I'm correct.
/bin/find /root/script/* -type f -ctime +30 -exec rm {} \;
Bu how I can set that this directory needs different retention:
/root/script/owner
You can exclude the /root/script/owner from the find output using -path or -regex, combined with '!' to negate the test
find /root/script -type f -ctime +30 '!' -path '/root/script/owner/*' -exec rm {} \;
OR
find /root/script -type f -ctime +30 '!' -regex '/root/script/owner/.*' -exec rm {} \;
Then execute the custom delete on the special folder
find /root/script/owner -type f -ctime +180 -exec rm {} \;
You can combine multiple operators so that they restrict what is included. You actually already do this because you have a -type and -ctime, joined with an implicit AND.
The one you need to add is regex, and you can do something like:
/bin/find /root/script/* -type f ! -regex '/root/script/owner/.*' -ctime +30 -exec rm {} \;
This should exclude files in that particular tree since the ! -regex will be false for them. The basic idea is that only those that pass all the conditions will be subject to further operations.
In this case, any non-regular files will be excluded. Of the others, any that don't match the regex will be excluded. Of those remaining ones, we'll throw away any that don't match the 30-day requirement. Whatever's left will be actioned by the rm.
The remaining directory, of course, is done with the greater time:
/bin/find /root/script/owner -type f -ctime +186 -exec rm {} \;

Linux find -type f ignored

I'm creating a cron job, that will find all *.log* files (it will be used to remove them later, when it works).
The find command looks like this:
find /data/dg \( -path /data/dg/kf/data -o -path /data/dg/pg/data \) -prune -o -name "*.log*" -type f
And it should find all files with name ".log" that are not in directories /data/dg/kf/data and /data/dg/pg/data
However the output this command gives contains also the directories.
...
/data/dg/kf/log/controller.log.2019-09-08-22
/data/dg/kf/log/server.log.2019-09-09-07
/data/dg/kf/data
/data/dg/pg/log/postgresql-2019-09-27_000000.log
/data/dg/pg/log/postgresql-2019-09-27_100859.log
/data/dg/pg/log/postgresql-2019-09-27_102411.log
/data/dg/pg/data
/data/dg/sim/log/sim_2019-09-27-11.0.log
/data/dg/sim/log/sim_2019-09-27-12.0.log
...
It seems that -type f doesn't work. What's wrong?
put -type f right after /data/dg
find /data/dg -type f -not -path "/data/dg/kf/data*" -not -path "/data/dg/pg/data*" -name "*.log*"

How display only files inside subdirs using the -haltr format?

How do I add the output looks of ls -haltr into the command below?
find 20180913/ -maxdepth 5 -not -type d -and -not -name '.*'
Basically I am trying to list only files inside my subdirs but need to display the file time stamp sorted by date with latest at the bottom.
Thank you
You can try using the exec option of find:
find 20180913/ -maxdepth 5 -not -type d -and -not -name '.*' -exec ls -haltr {} +
Updated my answer based on PesaThe's correction.

Delete files older than certain days that not have certain substring in name (Linux)

I have a folder with backup files with names like:
backup_2017_12_01__09_00_01.sql.gz
backup_2017_12_01__10_00_01.sql.gz
...
backup_2017_12_01__19_00_01.sql.gz
backup_2017_12_01__20_00_01.sql.gz
backup_2017_12_02__09_00_01.sql.gz
backup_2017_12_02__10_00_01.sql.gz
...
backup_2017_12_02__19_00_01.sql.gz
backup_2017_12_02__20_00_01.sql.gz
and so on.
I have a cron that should perform the deletion of the files respecting these rules:
delete all files older than 45 days; solved with find. -mtime +45 -exec rm {} \;
delete all files older than 7 days except those with the string __20_ in the name (the last backup in the evening); a command that is based on the last modification time rather than the name would also be fine
Can someone help me on the second point?
Thanks.
find /p/a/t/h \( -mtime +45 -o \( -mtime +5 ! -name '*__20_*' \) \) -delete
If you want, you can be more explicit:
find /p/a/t/h \( -mtime +45 -o \( -mtime +5 -and ! -name '*__20_*' \) \) -delete
Note that you should be more precise with language. This does not delete files that are "older than 45 days". It deletes files based on their modification time, which can be very different than age.
With find's -name test:
find . -type f -name "*.gz" ! -name "*__20_*.gz" -mtime +7 -delete

Deleting old files using cron job in Linux

I was setting up a cron job where I wanted to delete log files older than 1 day. The command to do this is as below. I am doing this on a AWS Linux EC2 instance.
find /var/log/tomcat8/ -mindepth 1 -mtime +1 -delete
But what I want to achieve is I want to exclude .log files from getting deleted and want to just delete the files with .gz extension. Can any body let me know how I achieve that exclusion in find command.
Just look for *.gz files and delete them.
find /var/log/tomcat8/ -name '*.gz' -mindepth 1 -mtime +1 -delete
Before deleting, just list the files to make sure you are deleting the correct ones.
find /var/log/tomcat8/ -name '*.gz' -mindepth 1 -mtime +1 -print
Print all files selected as below:
find /var/log/tomcat8/ -name '*.gz' -mindepth 1 -mtime +1
Above files can be deleted as below:
find /var/log/tomcat8/ -name '*.gz' -mindepth 1 -mtime +1 -exec rm{} \

Resources