How to delete all files older than 3 days when "Argument list too long"? - linux

I've got a log file directory that has 82000 files and directories in it (about half and half).
I need to delete all the file and directories which are older than 3 days.
In a directory that has 37000 files in it, I was able to do this with:
find * -mtime +3 -exec rm {} \;
But with 82000 files/directories, I get the error:
/usr/bin/find: Argument list too long
How can I get around this error so that I can delete all files/directories that are older than 3 days?

To delete all files and directories within the current directory:
find . -mtime +3 | xargs rm -Rf
Or alternatively, more in line with the OP's original command:
find . -mtime +3 -exec rm -Rf -- {} \;

Can also use:
find . -mindepth 1 -mtime +3 -delete
To not delete target directory

Another solution for the original question, esp. useful if you want to remove only SOME of the older files in a folder, would be smth like this:
find . -name "*.sess" -mtime +100
and so on.. Quotes block shell wildcards, thus allowing you to "find" millions of files :)

Related

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 to delete files and directories older than n days in linux

I have a directory named repository which has a number of files and sub directories. I want to find the files and directories which have not been modified since last 14 days so that I can delete those files and directories.
I have wrote this script but it is giving the directory name only
#!/bin/sh
M2_REPO=/var/lib/jenkins/.m2/repository
echo $M2_REPO
OLDFILES=/var/lib/jenkins/.m2/repository/deleted_artifacts.txt
AGE=14
find "${M2_REPO}" -name '*' -atime +${AGE} -exec dirname {} \; >> ${OLDFILES}
find /path/to/files* -mtime +5 -exec rm {} \;
Note that there are spaces between rm, {}, and \;
Explanation
The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.
This should work on Ubuntu, Suse, Redhat, or pretty much any version of linux.
You can give the find -delete flag to remove the files with it. Just be careful to put it in the end of the command so that the time filter is applied first.
You can first just list the files that the command finds:
find "${M2_REPO}" -depth -mtime +${AGE} -print
The -d flag makes the find do the search depth-first, which is implied by the -deletecommand.
If you like the results, change the print to delete:
find "${M2_REPO}" -mtime +${AGE} -delete
I know this is a very old question but FWIW I solved the problem in two steps, first find and delete files older than N days, then find and delete empty directories. I tried doing both in one step but the delete operation updates the modification time on the file's parent directory, and then the (empty) directory does not match the -mtime criteria any more! Here's the solution with shell variables:
age=14
dir="/tmp/dirty"
find "$dir" -mtime "+$age" -delete && find "$dir" -type d -empty -delete

How to copy the recent updated multiple files in another directory in Solaris

I want to copy the recently updated multiple file into another directory.
I am having 1.xml,2.xml,3.xml.... in this directory recently someone updated file or added new file into the directory,So i want to copy those files into the destination directory ..Its like synchronization of 2 directories.
For that I have tried below commend
find home/deployment/server/services/ -type f -mtime 1 | xargs cp /home/application/
and below one also
find home/deployment/server/services/ -type f -mtime 1 -exec cp /home/application/
I am not getting any file into destination after updating 1.xml file,So I have added new file 4.xml even that also not updating in destination directory.
How to process recently updated or newly added multiple files.
Thanks in advance.
Short answer:
use xargs to mv the "find" directory into another directory
Long answer: As I recall (not tested) for exec syntax is
find . -type f --mtime 1 -exec cp {} /destination/path/ +
"{}" is an argument which came from command "find"
For xargs
find . -type f --mtime 1 | xargs -0 -I {} cp {} /destination/path/
I do this often but use \; instead of + and usually -cnewer rather than -mtime.
\; executes the cp command on files individually instead of as a group.
+ executes as a group with as many paths as xterm will take. It may do this multiple time if there are a lot of files.
the \ in front of the ; option is required or bash will think it is the end of the command.
find ./ -mtime -1 -exec cp {} /path/ \; -print
Use the -print at the end to get a list of the files that were copied.

Bash-Performing the same command on several directories

I want to create a script that will delete any files older than 7 days on a specified list of directories, but wondering what would be the best way to go about it...
I want to perform the following command on all directories specified:
find DIRECTORY_PATH -type f -mtime +7 -exec rm {} \;
Maybe an array holding a list of directories, and loop through each element in the array performing the find command on that?
Any help/advice would be appreciated.
You can directly store all the directories in a file, say dirs.txt and loop through it:
while read dir
do
find "$dir" -type f -mtime +7 -exec rm {} \;
done < dirs.txt

Delete all folders older than X days using Shell [duplicate]

This question already has answers here:
Shell script to delete directories older than n days
(5 answers)
Closed 9 years ago.
my backups are stored in folders. e.g
**05092013** >
- File1.sql
- File2.sql
- File1.tar
- File2.tar
and so on.
Now I want to delete all Folders that are older than X Days.
I tried this
find $FILEDIR -mtime +14 -exec rm {} \;
but it only deletes all files and not the folders. how can i delete all files and folders that are older?
can someone help me?
Thx in advance cSGermany
Use -r?
find "$FILEDIR" -mtime +14 -exec rm -ir {} \;
Change -ir to just -r if you know what you're doing.
Or use -delete:
find "$FILEDIR" -mtime +14 -delete
But please, please make sure you know what you're doing.
You could add checks like this too to make sure $FILEDIR is always somewhere in your home directory:
[[ $FILEDIR == /home/abc/* ]] && find "$FILEDIR" -mtime +14 -delete
to find only directory, you could add find $FILEDIR -type d ... it could avoid to remove files (e.g. files under your given root dir) by mistake.
to remove a non-empty directory, you need rm -r, so -r option is important here.

Resources