Bash command to delete everything older than half a day - bash

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

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.

Find directories that are older than 60 days by name

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.

How to pick random images so the newer ones are more likely to be selected in Linux

I have a bash script on a headless Linux server that accepts a file name as parameter and displays the provided image fullscreen on a beamer.
Users can upload images to a shared folder. I want a random picture of that folder to be chosen each few seconds and displayed. But not completely random. I want that the newer (according to its creation time) a photo is, the more likely it is shown. So all old images are shown as well, but the newer ones will be displayed more frequently.
Is there any way (using Bash and the standard unix tools) to select a random picture from the folder (there are no nested folders, and all files end with the suffix ".jpg"), and consider their age, so the probability a picture is chosen is higher the newer it is?
It would be best if this probability is not linear: while seconds matter when the picture is only a few seconds or minutes old, there does not need to be a huge difference between a picture that is 2 hours or one that is 2 hours and 15 minutes old.
This random selects jpg files with files created or modified in the last hour being selected three times as likely and files created or modified in the last six hours selected twice as likely as older files:
{
find . -name '*.jpg' -mmin -60
find . -name '*.jpg' -mmin -360
find . -name '*.jpg'
} | shuf -n1
How it works
shuf -n1 randomly selects one line from the lines it is given on standard input. The find commands provide its standard input. Files modified within the last 60 minutes, -mmin -60, are listed by all three find commands. Those modified within the last 360 minutes, -mmin -360 are listed by two of the three commands. The last find command lists all jpg files. This is what gives the variation in probability.
You may adjust both the number of find commands and the cutoff times to suit your preferences.
Improvement
The above works as long as files do not have newlines in their names. To handle that possibility, use nul-terminated lists:
{
find . -name '*.jpg' -mmin -60 -print0
find . -name '*.jpg' -mmin 360 -print0
find . -name '*.jpg' -print0
} | shuf -z -n1

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!

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