delete all files within a directory that are older than 1 day [duplicate] - linux

This question already has answers here:
How to delete files older than X hours
(9 answers)
Closed 9 years ago.
I need to ensure that I have no old files left in my directory so what I think I do is
find . -type f -mtime +1 -delete
i got that from the find man page but then
find . -type f -mtime +1 -exec /bin/rm
but again, now told that find: -exec requires an argument - didn't iI pass this. So I started Googling and I found that my command needs to look likee this:
find . -type f -mtime +1 -exec /bin/rm -f {} +
and now I'm just wondering what the two {} s and the + sign are for. Can anyone help me here?
Thanks!

The {} stands for the name of the file(s) found.
The + sign (instead of a ;) means that this command accepts multiple file names in the same command, so that find can run much faster because it is run less times. The number of files added to each execution of the command is limited by the maximum length of the command line find is willing to use.

Related

What does this cron do for each command?

find /home/root/public_html/_sess -type f -mtime +3 -name 'sess-*' -execdir rm -- {} \;
I feel like I understand find , but I'm not 100% sure what -type is, I think that is the file type f not sure yet -mtime I feel like -mtime means a time setting of some sort, and +3 means maybe that time setting +3? , I feel like -execdir rm -- just means remove the files in the directory call -name 'sess-*' as well. But again not 100% sure of all the command elements within and wanted to get clarification.
You can do man find to get information on how Linux find works and all the options you can pass to it.
In this case, the command is using the Linux find utility to search for files in the /home/root/public_html/_sess directory with the following options:
-file f - searches for files of filetype f, which is regular files (not directories, links, etc)
-mtime +3 - searches for files modified more than 3 days ago (the + is for more than, -3 would be less than 3 days old)
-name 'sess-* - searches for files whose name matches the regex sess-* (name starts with "sess-")
-execdir <command> {}; - executes <command> on each file that find finds in the directory that the file was found in, in this case <command> is rm to remove the file
So in summary, this job searches for files located in a certain directory, whose names start with a specific string, and which are more than 3 days old, and deletes them.

Inputing directories/files into a text file and then showing files older than certain date

I'm using "ls -alR" to input directories and files in those directories into another text file.
ls -alR > testalR.txt
text file is created like so:
./test-file_folders/test_folder_1:
total 400
drwx------ 5 "user" "group" "size" May 2 10:30 test_file_1
.... file info ... more file info ....test_file_2
...more files
./test-file_folders/test_folder_2:
total 400
drwx------ 5 "user" "group" "size" Oct 2 11:35 test_file_1
.... file info ... more file info ....test_file_2
...more files
I am trying to show files that have not been accessed since October 2 2018.
I've tried:
`sed -n '/Oct 2 00:00/,/Oct/ 2 23:59/p' /testalR.txt
..but it produces no results. Is there a better way to display this or even possible?
Sorry, should have added this to begin with. I know using find -atime variant would be the best option. But we are using a system and a process that is bogged down by the find command. I am trying to find alternatives so using "find" can be avoided and wouldn't have to access directories directly each time I wanted to run a search.
Parsing the output of ls is a slippery slope.
Use find:
find . -type f -atime +3 -print
find . -type f -atime +3 -exec ls -uld {} +
Using -print simply returns a list of the filenames. Using -exec ls -ld {} + causes ls to be run for every file returned, giving you the details you may want.
The argument. to atime (or mtime or ctime) is in 24-hour steps. The argument can be positive or negative (or zero). Using -atime +3 finds a files that have been accessed at least FOUR days ago.
Using -exec ... {} + causes the command in "..." to be executed for every object returned, bundling as many objects (files) as possible at a time. This is much more efficient than forking a process for every file returned, as with:
... -exec ls -uld {} \;
One way to limit your results to a specific date, is to create two reference points (files) like this:
touch -amt 201809302359 f1
touch -amt 201810012359 f2
find . -type f \( -anewer f1 -a ! -anewer f2 \) -exec ls -uld -exec {} +
try with find:
find /folder -atime +30
where +30 = days
others params: man find

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

Deleting files that are older than one day [duplicate]

This question already has answers here:
find files older than X days in bash and delete
(3 answers)
Closed 7 years ago.
I have a server which creates several log files in the log directory. Due to this logging mechanism it eats up a lot of disk space on my server. I want to write a script that deletes all the files that are older than one day and keep the latest ones.
I am able to list the directories in sorted form using ls -trl command. But I am not able to understand how to remove these files. Please help.
You can use the following command:
/usr/bin/find <Your Log Directory> -mtime +1 | xargs rm -f
mtime - provides the file modification time.
+1 - indicates greater than one day.
Try using rm and find command like:
find . -mmin +$((60*24)) -exec rm {} \;
You don't want ls, you want find.
It has a neat argument, -mtime, that limits the results to a specific time delta, and -exec which allows you to provide a command to run on the results.
So for example,
find -mtime +10 -name "*tmp*" -exec rm {} \;
Does an rm on all files older than 10 days, with tmp in the name.
Oh, and be careful.
Very careful.
find . -mtime +1 -exec rm {} \;

find files which have been modified in the last 30 minutes in Linux

how to find files based upon time information, such as creation, modified and accessed. It is useful to find files before a certain time, after a certain time and between two times. what command in Linux would i have to use ?
I understand to find setuid files on linux computers i would have to use :
find / -xdev ( -perm -4000 ) -type f -print0 | xargs -0 ls -l
How do i check for files which have been modified in the last 30 minutes. (I created a new file called FILE2)
Just add -mtime -30m. I might be wrong about the actual syntax, but you get the idea. See man find.
Answer on your question is
find . -cmin -30 -exec ls -l {} \;

Resources