How to write find command to delete 7 days older files with selected JPGs ? - linux

I use Linux on Centos
And I need to remove the JPG in a file for more than 7 days.
But can't delete the JPG of the main directory
example: find /users/mac/desktop/test/*
Will output
/users/mac/desktop/test/test.jpg
/users/mac/desktop/test/test01
/users/mac/desktop/test/test01/test01.jpg
/users/mac/desktop/test/test02
/users/mac/desktop/test/test02/test02.jpg
But I only need delete this two .jpg
/users/mac/desktop/test/test01/test01.jpg
/users/mac/desktop/test/test02/test02.jpg

I need to remove the JPG in a file for more than 7 days. But can't delete the JPG of the main directory
find /users/mac/desktop/test -mtime +7 -mindepth 2 -type f -name '*.jpg' -delete
-mtime +7 the modification time of the file is older then 7 days
-mindepth 2 ignore the "main directory"
-type f only files
-name '*.jpg' only jpg files
-delete delete them

find /users/mac/desktop/test/*/*

Related

How to delete files in LINUX which are older than 30 days and are of specific extension *.pdf

I have a peculiar challenge, we have one directory where there is close to 15000 PDF files, and the file names also contain spaces (plus we have other config file which we are not supposed to touch).
I am trying to delete all the PDF files (Please note PDF file name has spaces) from this directory which are older than 30 days/1 month. how can I achieve this?
For find all PDF on your linux system with +30 days old and delete them you can use this command :
find / -name -ls -o -regex '.*\.pdf' -type f -mtime +30 -exec rm {} \;
The / is the path where recursively the command search PDF file.
The -regex '.*.pdf' is the regex for only match PDF file
The -type f only file
The -mtime +30 match file with 30 days minimum old (delete too file which have 32 days old)
The -exec rm {} ; Execute the rm command with {} is the full file name found.

how to move jpg and jpeg files whose size is greater than 10kb [duplicate]

I have some automated downloads in a proprietary linux distro.
They go to a temp scratch disk. I want to move them when they're finished to the main RAID array. The best way I can see to do this is to check the folders on the disk to see if the contents have changed in the last minute. If not then its probably finished downloading and then move it.
Assuming there could be hundreds of folders or just one in this location and its all going to the same place. Whats the best way to write this?
I can get a list of folder sizes with
du -h directory/name
The folders can contain multiple files anywhere from 1.5mb to 10GB
Temp Loc: /volume2/4TBScratch/Processing
Dest Loc when complete: /volume1/S/00 Landing
EDIT:
Using this:
find /volume2/4TBScratch/Processing -mindepth 1 -type d -not -mmin +10 -exec mv "{}" "/volume1/S/00 Landing" \;
find: `/volume2/4TBScratch/Processing/test': No such file or directory
4.3#
yet it DOES copy the relevant folders and all files. But the error worries me that something might go wrong in the future.... is it because there is multiple files and it's running the same move command for EACH file or folder in the root folder? But since it moves it all on the first iteration it cant find it on the next ones?
EDIT2:
Using Rsync
4.3# find /volume2/4TBScratch/Processing -mindepth 1 -type d -not -mmin +10 -exec rsync --remove-source-files "{}" "/volume1/S/00 Landing" \;
skipping directory newtest
skipping directory erw
RESOLVED: EDIT3
Resolved with the help in the comments below. Final script looks like this:
find /volume2/4TBScratch/Processing -mindepth 1 -type d -not -mmin +10 -exec rsync -a --remove-source-files "{}" "/volume1/S/00 Landing" \;
find /volume2/4TBScratch/Processing -depth -type d -empty -delete
rsync to move folders and files but leaves empty root dir
the next command finds empty folders and removes them.
Thanks all!
You can use GNU find with options -size for detecting files/folders of certain size and use mv with the -exec option to move to destination directory. The syntax is
find /volume2/4TBScratch/Processing -type d -maxdepth 1 -size -10G -exec mv "{}" "/volume1/S/00 Landing" \;
Using rsync
find /volume2/4TBScratch/Processing -type d -maxdepth 1 -size -10G -exec rsync --remove-source-files "{}" "/volume1/S/00 Landing" \;
The size with a - sign to indicate less than the mentioned size which in this case is 10GB. A note on each of the flags used
-type d -> For identifying only the folders from the source path.
-maxdepth 1 -> To look only on the current source directory and not
being recursive.
-exec -> Execute command following it.
Alternatively, if you want to find files that are last modified over a certain time(minutes), find has an option for -mmin which can be set to a value. E.g. -mmin -5 would return files modified five minutes ago.
So suggest adding it to your requirement, for x as you need and see if the directories are listed, then you can add the -exec option for moving the directories
find /volume2/4TBScratch/Processing -type d -maxdepth 1 -mmin -2 -size -10G
Refer to the GNU documentation for finding files according to size on how this works.
Note:- The double quotes("") are added to avoid Bash from splitting the names containing spaces.

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

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")

find directory older than 3 days and zip all files in it

Can i find any directories with a condition like older than 3 days
and zip them then delete the directories?
I have 2 solutions.
zip all directories in 1 zip under working directory
I tried
zip -rm ${WORKDIR}/date +%Y%m%d -d "${DAY_TO_ZIP} days ago".zipfind ${WORKDIR} -daystart -mtime +${DAY_TO_ZIP} -type d ! -name "*.zip"``
this command will zip all files include non-directory file.
1 directory 1 zip same path with a directory
thank you very much
Execute bellow command to find all directory older than 3 days and zip all file
# find / -mtime +3 -type d -exec zip -r zipfile.zip {} +
-mtime +3 means you are looking for a file modified 3 days ago.
-mtime -3 means less than 3 days.
-mtime 3 If you skip + or – it means exactly 3 days.
Finally If you delete all directory then execute bellow command
# find / -mtime +3 -type d -exec rm -f {} \;
find ./ -mtime +x -print -exec gzip {} ;

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