Delete files older than 7 days in perl [duplicate] - linux

This question already has answers here:
delete folders older than 12 hours in perl
(3 answers)
Closed 5 years ago.
How to delete files from directories who are older than certain days (7 days) OR files who was created on any certain date?

Be careful with special file names (spaces, quotes) when piping to rm.
There is a safe alternative - the -delete option:
find /path/to/directory/ -mindepth 1 -mtime +5 -delete
That's it, no separate rm call and you don't need to worry about file names.
Replace -delete with -depth -print to test this command before you run it (-delete implies -depth).

Related

How to get deleted files into a log file. Bash script [duplicate]

This question already has answers here:
Linux find and delete files but redirect file names to be deleted
(5 answers)
Closed 1 year ago.
So im using the script
find /path/to/files/* -mtime +60 -exec rm {} \;
How can i collect the deleted files and transfer them into a logfile in Bash script
You could do something like:
find /path/... -print ... | tee -a <log.file>
The -print will print out all the hits, and the tee will append that to some log.file.
Side note: the * at the end of your /path/to/files/* seems superfluous.
Side note2: if you just want to delete the files, find has a built-in -delete.

Bash - find files older than X minutes and move them [duplicate]

This question already has answers here:
find files older than X days in bash and delete
(3 answers)
Closed 7 years ago.
I need to create a bash script and put in CRON - on linux.
I need to loop through or just find all files in directory DIR_A and if files are older than, for example, 10 minutes, move them to directory DIR_B.
Any ideas? Thanks!
If anyone else needs it:
find DIR_A -maxdepth 1 -type f -mmin +10 -exec mv "{}" DIR_B/ \

How to find both files and folders that are used in some process? [duplicate]

This question already has answers here:
Delete files older than 10 days using shell script in Unix [duplicate]
(3 answers)
Closed 7 years ago.
I want to delete files and folders older than 15 days.
But Before deletion I want to check that those file and folders shouldn't use anywhere. How can i do that ?
is ps -ef works for this ?
Using the find command:
find /path/to/folder -type f -mtime +15 -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 {} \;

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.

Resources