Deleting files that are older than one day [duplicate] - linux

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 {} \;

Related

Cron Job email find: missing argument to `-exec'

I'm setting up a webserver with Plesk on Ubuntu 18.04 and I would like to use a part of the space I've available to store security footage. I succeeded in automatically uploading the photos and videos to the correct folder, but the problem is that they are not automatically removed, so that the server is full of security images. I upload the footages to a folder on the server that is also available from the internet (secured). I did some research on the internet to a cron job that automatically deleted the files older than 7 days where I found this:
find /var/www/vhosts/path to files/* -mtime +7 -exec rm -f {} \;
I also found that you can name a file to, for example: delete-files and which can be executed with crontab -e. (Yes, I made it executable;-)
I added this cron to run every hour and stated that I received notifications from the cron. Now, however, I get the following output: find: missing argument to `-exec '
Is there anything else that I need to share? Like logs?
change find /var/www/vhosts/path to files/* -mtime +7 -exec rm -f {} \;
to
find /var/www/vhosts/path to files/ -mtime +7 -exec rm -f {} \;
the * is unnecessary in the path
Can you try this as well?
find /var/www/vhosts/path to files/ -mtime +7 | xargs rm -f

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

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.

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

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.

In linux shell, How to cp/rm files by time?

In linux shell, When I run
ls -al -t
that show the time of files.
How to cp/rm files by time? just like copy all the files that created today or yesterday. Thanks a lot.
Depending on what you actually want to do, find provides -[acm]time options for finding files by accessed, created or modified dates, along with -newer and -min. You can combine them with -exec to copy, delete, or whatever you want to do. For example:
find -maxdepth 1 -mtime +1 -type f -exec cp '{}' backup \;
Will copy all the regular files in the current directory more than 1 day old to the directory backup (assuming the directory backup exists).
Simple Example
find /path/to/folder/ -mtime 1 -exec rm {} \; // Deletes all Files modified yesterday
For more examples google for bash find time or take a look here

Resources