bash delete older files - linux

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

Related

Finding and following symbolic links but without deleting them

The current find command is utilized to find and delete outdated files and directories. The expired data is based on a given properties file and the destination.
If the properties file says…
"/home/some/working/directory;.;180"
…then we want files and empty subdirectories deleted after 180 days.
The original command is…
"find ${var[0]} -mtime +${var[2]} -delete &"
…but I now need to modify now that we've discovered is has deleted symbolic links that existed in specified sub-directories after the given expiration date in the properties file. The variable path and variable expiration time are designated in the properties file (as previously demonstrated).
I have been testing using…
"find -L"
…to follow the symbolic links to make sure this clean up command reaches the destinations, as desired.
I have also been testing using…
"\! -type l"
…to ignore deleting symbolic links, so the command I've been trying is…
"find -L ${var[0]} ! -type l -mtime +${var[2]} -delete &"
…but I haven't achieved the desired results. Help please, I am still fresh into Linux and my research hasn't lead me to a desired answer. Thank you for your time.
Change
\! -type l
to
\! -xtype l
find -L ${var[0]} \\! -xtype l -mtime +${var[2]} -delete &

Can't find a way to delete files that match a date pattern

I'm trying to delete all files in a folder structure (recursively) except the youngest one for each month.
In other words.... only keep the first ones from each month in each folder.
On a Linux system (bash) ... ;-) (or even more precise on a Synology NAS)
May thanks for your help !
Alex
Please be careful! I take no responsibility!
Try find:
Remove files which are older than 7 days:
find . -type f -ctime +7 -delete

find and delete command deletes all files in that folder

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?

Find numbered subdirectories below number X and delete them

I have a folder 'masterfolder' that has subfolders with a numbered naming scheme:
\masterfolder\S01
\masterfolder\S02
\masterfolder\S03
\masterfolder\S04
\masterfolder\S05
Now I want to find and delete all folders below a specific number, for example S03. This means, S03, S04, S05 etc should not get deleted, S01 and S02 should get deleted.
I normally use this command to find and delete a specific folder:
find "/mnt/USBDRIVE/masterfolder" -type d -name "S02" -exec rm -rf '{}' \;
I tried finding a solution myself, but the only method I have found is to delete everything except the number I know I want to keep:
find "/mnt/USBDRIVE/masterfolder" -mindepth 1 -maxdepth 1 -type d -not -name "S03" -exec rm -rf '{}' \;
This will keep S03, but delete all others. I want to keep S03 and any other folder with a higher number than S03.
Any ideas appreciated.
There are many ways to solve this.
Since your numbers are zero padded, the easiest way is to just send a list of the directories to a file sorted alphabetically. Then delete the ones you want ignored (they'll all be together), do a global change to add "rm " to the beginning of each line, and run the file as a script.
This will take you less than 30 seconds. Any programmatic solution will take longer.

I wanna to delete all files and directories in linux

I have to delete all directories and files which should be 3 years back from current date what should be the specific command for that in linux.
It depends on how you define "3 years back": created, last modified... If that's last modified, you can do something like this to list those files
find /directory -mtime +1095
/directory is the starting directory, +1095 meaning modified 1095 days ago, 365*3.
If you're okay with the list, then add the delete option
find /directory -mtime +1095 -delete
Be careful not to put -delete before -mtime, there's a specific order there. See man find for more informations.

Resources