find and delete command deletes all files in that folder - linux

Currently, I am using this command to delete all files older than 30 minutes on my Linux server.
sudo find /var/www/html/folder/* -type f -mmin +30 -delete
But it deletes all the files in that folder irrespective of their age. What's wrong with this?

Related

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

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.

bash delete older files

I have this unique requirement of finding 2 years older files and delete them. But not only files as well as corresponding empty directories. I have written most of the logic but only thing that is still pending is , when I delete particular file from a directory , How can I delete the corresponding directory when it is empty. As when I delete the particular file , the ctime/mtime would also accordingly get updated. How do I target those corresponding older directories and delete them?
Any pointers will be helpful.
Thanks in advance.
Admin
I would do something like this:
find /path/to/files* -mtime +730 -delete
-mtime +730 finds files which are older than 730 days.
Please be careful with this kind of command though, be sure to write find /path/to/files* -mtime +730 beforehand and check that these are the files you want to delete!
Edit:
Now you have deleted the files from the directories, -mtime +730 won't work.
To delete all empty directories that you have recently altered:
find . -type d -mmin -60 -empty -delete

Linux cron job remove .zip files from folder every 12 hours

I'm looking for help to make a cron job that every 12 hours will delete all .zip files from a subfolder in my hostgator hosting account.
http://prntscr.com/7rr22d
After some google research I tried this command but nothing seems to happen
find /home/username/domain.com -type f -name "*.zip" |xargs rm
What should I put in the "Command:" field ?
Maybe try:
find /home/username/domain.com -name "*.zip" -exec rm -rf {} \;

Script to delete selected 60 days old files in Linux

i have a requirement to delete 60+ days old files from our growing linux server. There are folders for every month and files are stored in it. I just want to delete files from all monthly folder EXCEPT the December folder (named as 2012_12).
what condition should i put in the script to not find files in all the folder like '%_12' ?
find /path/ -name '*_12' -prune -o -type f -mtime +59 -delete

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