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

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.

Related

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 loop through multiple folder and subfolders and remove file name start with abc.txt and 14 days old

I have folder and subfolder. I need to loop through each folder and subfolder and remove or move the file names which start with abc.txt and 14 days old to temporary folder. My folder tree structure is:
The file may be inside the folder or subfolder 'abc.txt'
I have used this below code but not working.
I took the folder paths into a list.txt file using below command
find $_filepath -type d >> folderpathlist.txt
I pass the path list to below code to search and remove or move files to temporary folder
find folderpathlist.txt -name "abc*" -mtime \+14 >>temp/test/
How do I achieve this scenario ?
You want to find files: -type f
that start with abc.txt: -name "abc.txt*"
that are 14 days old: -mtime +14
and move them to a dir.: -exec mv {} /tmp \;
and to see what moved: -print
So the final command is:
find . -type f -name "abc.txt*" -mtime +14 -exec mv {} /tmp \; -print
Adjust the directory as required.
Note that mtime is the modification time. So it is 14 days old since the last modification was done to it.
Note 2: the {} in the -exec is replaced by each filename found.
Note 3: \; indicates the termination of the command inside the -exec
Note 4: find will recurse into sub-directories anyway. No need to list the directories and loop on them again.

How do I recover files that disappeared after wrong MV command format?

I'm trying to move files from the current directory to another directory by date, but I accidentally used the wrong target format:
find . -maxdepth 1 -mtime +365 -type f -exec mv "{}" "..\folder" \;
instead of
find . -maxdepth 1 -mtime +365 -type f -exec mv "{}" "../folder" \;
Then my files just disappeared.
I can't seem to find it anywhere. I've tried on both target & source directories and even the non existent directory that I have accidentally sent the files to.
I would just like to know if I can still recover the files.
They're all gone. When you run:
find . -maxdepth 1 -mtime +365 -type f -exec mv "{}" "..\folder" \;
You are executing, for every file, the command:
mv filename ..folder
In other words, you renamed every file to the name ..folder. Each file overwrote the next one. The contents of the ..folder file are whatever file was last processed by your command, and all the rest are gone.

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

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/*/*

7zip archieving files that are newer than a specific date

I create 7zip files like this from command line in Linux:
# 7za a /backup/files.7z /myfolder
After that I want to create another zip file that includes all files inside /myfolder that are newer then dd-mm-YY.
Is it possible to archieve files with respect to file's last change time ?
(I don't want to update "files.7z" file I need to create another zip file that includes only new files)
The proposal by Gooseman:
# find myfolder -mtime -10 -exec 7za a /backup/newfile.7z {} \;
adds all files of each directory tree which got new files since the directory is also new and then adds all new files just archived again.
The following includes only new files but does not store the path names in the archive:
# find myfolder -type f -mtime -10 -exec 7za a /backup/newfile.7z {} \;
This stores only new files — with path names:
# find myfolder -type f -mtime -10 > /tmp/list.txt
# tar -cvf /tmp/newfile.tar -T /tmp/list.txt
# 7za a /backup/newfile.7z /tmp/newfile.tar
You could try this command:
find myfolder -mtime -10 -exec 7za a /backup/newfile.7z {} \;
In order to find the number to use by the mtime option you could use some of these answers:
How to find the difference in days between two dates? In your case it would be the difference between the current date and your custom dd-mm-YY (in my example dd-mm-YY is 10 days back from now)
From man find:
-n for less than n
-mtime n
File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times.

Resources