Delete files whose last access time exceeds N days - bash

The task is to write a bash scenario to delete in a directory those files whose last access time exceeds N days.
If there are no such files, the message should be displayed.
I'm trying to delete 1 file and find the last access time for it:
stat -c%x test.txt
The command returns a date: 2018-12-01 16:45:30.390000000 +0000
Then I'm writing a current date command:
date '+%Y-%m-%d %H:%M:%S.%N'
The command returns a date: 2018-12-01 18:39:16.873718766
Now I have no idea how to subtract this dates and apply to all files of the directory.

Use the command find with a parameter -atime.
find /path/to/dir -atime +100 -delete
deletes files accessed 101 or more days ago.

Related

Grep file after loaded after a particular minute

Can anyone please help me in getting the command to search files loaded after a particular minute.
Example:-
I want the files loaded after the time 2017/02/15.11.
That means all the files loaded after 11
First, you need to convert you custom date into a recognizable date format:
DATE="2017/02/15.11"
TIME="${DATE##*.}:00"
DATE="${DATE%.*}"
echo "DATE: ${DATE} TIME: ${TIME}"
Then just pass it to find:
find /dir -newermt "${DATE} ${TIME}"

How to get access_log summary by goaccess starting from certain date?

Currently I keep 6 weeks of apache access_log. If I generate a access summary at month end:
cat /var/log/httpd/access_log* | goaccess --output-format=csv
the summary will include some access data from previous month.
How can I skip logs of previous month and summarise from first day of month?
p.s. the data-format is: %d/%b/%Y
You can trade the Useless Use of cat for a useful grep.
grep -n $(date +'[0-3][0-9]/%b/%Y') /var/log/httpd/access_log* |
goaccess --output-format=csv
If the logs are by date, it would be a lot more economical to skip the logs which you know are too old or too new, i.e. modify the wildcard argument so you only match the files you really want (or run something like find -mtime -30 to at least narrow the set to a few files).
(The cat is useless because, if goaccess is at all correctly written, it should be able to handle
goaccess --output-format=csv /var/log/httpd/access_log*
just fine.)

Finding the files which are not accessed/modified in last 30min?

I've one requirement, i want to give the notifications to the users who have not accessed the files in 30 min. using the shell script. Is it possible to find the files which was not accessed in 30 min using find. I'd checked.
find /opt/SP/tibmft/scripts/ -mtime 0
which will fetch the files which was modifed in last 24 hours.
My requirement is fetch the files which was created in the last 30 min and not been accessed by the user? Please suggest the solution, how to achieve this?
Try using
find PATH -cmin -30 -and -amin +30
Is it possible to find the files which was not accessed in 30 min using find.
According to man find:
Numeric arguments can be specified as
+n for greater than n,
-n for less than n,
n for exactly n.
Please note, this is greater and less not greater/less or equal. So, you have to take some care in order to not have a 1 minute error due to that:
find PATH -not -amin +30
or
find PATH -amin -31
My requirement is fetch the files which was created in the last 30 min and not been accessed by the user?
Here is an attempt:
find PATH -amin -31 -not -newerBt "-30 minutes"
# ^^^^^^^^
# (B)irth date newer (t)han ...
... unfortunately, it does not work on my computer with ext4 file-system, as, to quote a comment by Barmar above: "Most Unix file-systems don't record file creation time. They just have modification, access, and inode change times."
Some random ideas:
According to one of your comment, this is potentially a periodic task. So one might investigate the use of a cron job to record the list of files at regular intervals?
If your file-system has snapshots you might build on that too. Maybe.
Try the following:
find $PATH -type f -cmin -30 amin +30

How to fix the error in the bash shell script?

I am trying a code in shell script. while I am trying to convert the code from batch script to shell script I am getting an error.
BATCH FILE CODE
:: Create a file with all latest snapshots
FOR /F "tokens=5" %%a in (' ec2-describe-snapshots ^|find "SNAPSHOT" ^|sort /+64') do set "var=%%a"
set "latestdate=%var:~0,10%"
call ec2-describe-snapshots |find "SNAPSHOT"|sort /+64 |find "%latestdate%">"%EC2_HOME%\Working\SnapshotsLatest_%date-today%.txt"
CODE IN SHELL SCRIPT
#Create a file with all latest snapshots
FOR snapshot_date in $(' ec2-describe-snapshots | grep -i "SNAPSHOT" |sort /+64') do set "var=$snapshot_date"
set "latestdate=$var:~0,10"
ec2-describe-snapshots |grep -i "SNAPSHOT" |sort /+64 | grep "$latestdate">"$EC2_HOME%/SnapshotsLatest_$today_date"
I want to sort the snapshots according to dates and to save the snapshots that are created in latest date in a file.
SAMPLE OUTPUT OF ece-describe-snapshots:
SNAPSHOT snap-5e20 vol-f660 completed 2013-12-10T08:00:30+0000 100% 109030037527 10 2013-12-10: Daily Backup for i-2111 (VolID:vol-f9a0 InstID:i-2601)
It will contain records like this
I got this code :
latestdate=$(ec2-describe-snapshots | grep ^SNAPSHOT | sort -k 5 | awk '{print $5}')
ec2-describe-snapshots | grep SNAPSHOT.*$latestdate | > "$EC2_HOME/SnapshotsLatest_$today_date"
but getting this error :
grep: 2013-12-10T09:55:34+0000: No such file or directory
grep: 2013-12-11T04:16:49+0000: No such file or directory
grep: 2013-12-11T04:17:57+0000: No such file or directory
i have some snapshots made on amazon, i want to find the latest snapshots made on a date and then want to store them in a file. like date 2013-12-10 snapshots made on this date should be stored in file. Contents of snapshotslatest file should be
SNAPSHOT snap-c17f3 vol-f69a0 completed 2013-12-04T09:24:50+0000 100% 109030037‌​527 10 2013-12-04: Daily Backup for Sanjay_Test_Machine (VolID:vol-f66409a0 InstID:i-26048111)
SNAPSHOT snap-c7d617f9 vol-3d335f6b completed 2013-12-04T09:24:54+0000 100% 1090‌​30037527 10 2013-12-04: Daily Backup for sacht_VPC (VolID:vol-3db InstID:i-ed6)
please not that if there are snapshots created on 2013-12-10, 2013-12-11, 2013-12-12. It means that the latest_date should be 2013-12-12 and all the snaphshot created on 2013-12-12 should be saved in file.
Any suggestion or lead is appreciated.
Neither the batch script nor the shell script you posted are a good starting point so let's start from scratch. Sorry, this is too big for a comment.
You want to find the latest snapshots made on a date and then want to store them in a file.
What does that mean?
Do the snapshot files have a timestamp in their name or in their content?
If not - UNIX does not store file creation timestamps so is a last-modified timestamp adequate?
Do you literally want to concatenate all of your snapshot files into one singe file or do you want to create a file that has a list of the snapshot file names?
Post some sample input (e.g. some snapshot file names and contents if that's where the timestamp is stored) and the expected output given that input.
Update your question to address all of the above, do not try to reply in a comment.
Minor issue, you don't need a pipe when re-directing output, so your line to save should be
ec2-describe-snapshots | grep SNAPSHOT.*$latestdate > "$EC2_HOME/SnapshotsLatest_$today_date"
Now the main issue here, is that the grep is messed up. I haven't worked with amazon snapshots, but judging by your example descriptions, you should be doing something like
latestdate=$(ec2-describe-snapshots | grep -oP "\d+-\d+-\d+" | sort -r | head -1)
This will get all the dates containing the form dddd-dd-dd from the file (I'm assuming the two dates in each snapshot line always match up), sort them in reverse order (latest first) and take the head which is the latest date, storing it in $latestdate.
Then to store all snapshots with the given date do something like
ec2-describe-snapshots | grep -oP "SNAPSHOT(.*?)$lastdateT(.*?)\)" > "$EC2_HOME/SnapshotsLatest_$today_date"
This will get all text starting with SNAPSHOT, containing the given date, and ending in a closing ")" and save it. Note, you may have to mess around with it a bit, if ")" can be present elsewhere.

Find directories created less than a week ago

I know I can use this command to find directories created or modified less than a week ago:
find /home -type d -mtime -7
But how do I find directory that were created less than a week ago?
Creation time is not stored.
There is only 3 timestamps you can check
Last access time
Last modification time
Last change time
"Change" is one of: permission changes, rename etc.
While the modification is contents only.
Short answer: you can't.
There are three times stored in an inode
ctime: time of creation or change of the inode
mtime: time of last change of the file that the inode refers to
atime: time of last access to the file
The point is: ctime is altered not only by create, but also by chmod / chown, maybe even by ln (not sure). Man stat and man touch are your friends.
If you try to find fresh directories by means of find /home -type d -mtime -7 be prepared to also find older directories that had their mode or owner changed.

Resources