Find directories that are older than 60 days by name - bash

I have a directory with some files and subdirectories. In that directory I need to find files by name pattern ".cm-2015.10.10" that are older than 30 days. This command finds me the needed directories:
find comdit/ -type d -name .cm-[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9] -print
but how can I specify that I only need folders older than 60 days? Adding -ctime +60 did nothing for me. What am I doing wrong?

It's -mtime which you are looking for:
find comdit/ -type d -mtime +30 -name .cm-[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9] -print
Here are the docs:
-atime n
File was last accessed n*24 hours ago. When find figures out
how many 24-hour periods ago the file was last accessed, any
fractional part is ignored, so to match -atime +1, a file has to
have been accessed at least two days ago.
-ctime n
File's status was last changed n*24 hours ago. See the comments
for -atime to understand how rounding affects the interpretation
of file status change times.
-mtime n
File's data was last modified n*24 hours ago. See the comments
for -atime to understand how rounding affects the interpretation
of file modification times.

Related

Find and list files from last 3-days starting from midnight of first day [duplicate]

This question already has answers here:
Using find to locate files modified within yesterday
(2 answers)
Closed 2 years ago.
please assist, I am trying to list the files from last 3 days in directory starting from midnight of first day from last 3-days. I created this so far " find ./* -type f -mtime -3 -exec ls {} ;" This only pulls data from current time to - 3days. It doesn't get data from midnight of first day. I need a data start from last 3-days starting midnight to today.
Please assist.
thank you
You can touch a file with the earliest date you want to use. Then use find's -newer option.
touch -t <earliestDate> someTempFile
find . -type f -newer someTempFile -exec ls -l {} \;
You don't need -exec if you execute ls with no options and you're constrained to normal files.

Copy the file created newly in UNIX

I have a directory where so many files created daily and need to copy the new files which were generated. And all files will be created with starting name abc_
Ex:I have a file abc_0520123.pdf on the next day two files were created abc_0521234.pdf and abc_0521254.pdf now I want to copy only these two files created newly.
Please help me how can I compare old files with new one and to copy them.
You can use find.
find /my_directory -mtime -1d # Finds everything modified less than one day ago.
find /my_directory -ctime -1d # Finds everything created less than one day ago.
find /my_directory -ctime +5d # Finds stuff created more than 5 days ago.
If you want to move the files you can use -exec
find /my_directory -mtime -1d -type f -exec mv {} /new_dir/. \;
Finds files only located under /my_directory which are less than 1 day old and moves them to /new_dir
Find is one of the most useful commands you can ever learn!

Bash command to delete everything older than half a day

I've got a simple bash script, cron'd to run at midnight each night, which creates a backup or files and stores them as a .tar.gz in my Dropbox. Before this happens, however, I need the script to delete the previous night's backup.
To do this I'm currently running this command:
find ~/Dropbox/Backups/casper/* -mtime +0.5 -exec rm {} \;
Which to my mind should delete anything older than half a day - but it doesn't seem to work (it keeps the previous nights back-up, but deletes anything before this)
Can someone point me in the right direction please? Thank you
From the manpage for find:
-mtime n
File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the
interpretation of file modification times.
-atime n
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last
accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days
ago.
From this we can see that the 0.5 is dropped, then 1 day ago is required. You probably want to use -mmin instead.
For example (from babah):
# 720 is 60 times 12
find ~/Dropbox/Backups/casper/* -mmin 720 -print -exec rm {} \;

Unix shell script to delete older log files

Could someone please give me the command to delete log files before today (except today and yesterday) date?
You can use find with the -mtime option to get a list of files modified more than N days ago.
For example:
find . -maxdepth 1 -name '*.txt' -mtime +2
will give you all the *.txt files in the current directory older than 48 hours.
You can add -delete to actually get rid of them.
find /path/to/files* -mtime +2 -delete

find -daystart argument explanation

So I understand that a line such as:
find /var/log/ -mtime +60 -type f -exec ls -l {} \;
Will list all files in /var/log which were modified 60 days or more ago.
After reading through the find man page though I noticed:
Measure times (for -amin, -atime, -cmin, -ctime, -mmin, and
-mtime) from the beginning of today rather than from 24 hours
ago. This option only affects tests which appear later on the
command line.
Can someone explain the rest? (-amin, -atime, -cmin, -ctime, -mmin) The man page itself does not seem to really declare what each of these do?
Some example questions which might help me understand:
Find files modified an hour or more ago?
Find files modified between 60 minutes and 10 minutes ago?
Find files modified 2 weeks ago?
Find files created in the last 5 minutes?
Find files modified an hour or more ago?
-mmin +60
Find files modified between 60 minutes and 10 minutes ago?
-mmin -60 -mmin +10
Find files modified 2 weeks ago?
-mtime +7 -mtime -8
Find files created in the last 5 minutes?
Can't be done. POSIX has no specification for creation time.
These options are explained in the TESTS subsection of the EXPRESSIONS section of the find(1) man page.

Resources