Find files created 1 hour ago - bash

I want to list files with long description on AIX that was created from 1 hour ago. I am trying
find . -cmin -60 but it only shows the file names. Was trying also find . -cmin -60 -exec ls -l {} \; but it was displaying the whole files in the directory.
Thank you

I think what you want is
find . -cmin +60 -exec ls -al {} \;
It will list all the files in current directory created more than 60 minutes agp.
The '+' in the '+60' means more than 60 minutes ago while a '-' in the '-60' means less than 60 minutes ago.

Some options:
find . -cmin -60 -exec ls -ld {} \;
find . -cmin -60 -type f -exec ls -l {} \;
find . -cmin -60 -print0 | xargs -0 ls -ld
find . -cmin -60 -type f -print0 | xargs -0 ls -l
The last two are better, but require GNU-findutils.
Edit: As noted by others, -cmin -60 means recently modified files, -cmin +60 means the not-recently modified files.

When your find lacks a cmin option, you can touch a file with a timestamp 1 hour ago and use the find with -newer.

Related

Find files created over a certain number of seconds ago then copy it

So I'm trying to make a script that watches for files that haven't been modified for at least 10 seconds then to execute an rsync on the file. My original line was this:
find "/Volumes/Media/" -type f -size +2G -cmin +1 -cmin -60 -exec rsync -aq --update {} /Volumes/LocalMedia/ \;
But that does 1 minute, which is too long. So far I've gotten down to this:
find "/Volumes/Media/" -type f -size +2G -exec bash -c 'echo $(( $(date +%s) - $(stat -f%c "{}") ))' \;
which gives me the output of the files in the directory by seconds. But I seem to be having trouble evaluating that and performing the aforementioned rsync. This is macosx so it's bsd find not GNUfind.
Any thoughts/help would be lovely.
Thanks,
-N
You should be able to accomplish this using mtime:
find "/Volumes/Media/" -type f -size +2G -mtime +10s -exec rsync -aq --update {} /Volumes/LocalMedia/ \;
Using -mtime +10s would return to find only files older than 10 seconds

I am getting an error "arg list too long" in unix

i am using the following command and getting an error "arg list too long".Help needed.
find ./* \
-prune \
-name "*.dat" \
-type f \
-cmin +60 \
-exec basename {} \;
Here is the fix
find . -prune -name "*.dat" -type f -cmin +60 |xargs -i basename {} \;
To only find files in the current directory, use -maxdepth 1.
find . -maxdepth 1 -name '*.dat' -type f -cmin +60 -exec basename {} \;
In all *nix systems the shell has a maximum length of arguments that can be passed to a command. This is measured after the shell has expanded filenames passed as arguments on the command line.
The syntax of find is find location_to_find_from arguments..... so when you are running this command the shell will expand your ./* to a list of all files in the current directory. This will expand your find command line to find file1 file2 file3 etc etc This is probably not want you want as the find is recursive anyway. I expect that you are running this command in a large directory and blowing your command length limit.
Try running the command as follows
find . -name "*.dat" -type f -cmin +60 -exec basename {} \;
This will prevent the filename expansion that is probably causing your issue.
Without find, and only checking the current directory
now=$(date +%s)
for file in *.dat; do
if (( $now - $(stat -c %Y "$file") > 3600 )); then
echo "$file"
fi
done
This works on my GNU system. You may need to alter the date and stat formats for different OS's
If you have to show only .dat filename in the ./ tree. Execute it without -prune option, and use just path:
find ./ -name "*.dat" -type f -cmin +60 -exec basename {} \;
To find all the .dat files which are older than 60 minutes in the present directory only do as follows:
find . -iregex "./[^/]+\.dat" -type f -cmin +60 -exec basename {} \;
And if you have croppen (for example aix) version of find tool do as follows:
find . -name "*.dat" -type f -cmin +60 | grep "^./[^/]\+dat" | sed "s/^.\///"

Copying Files that have been modified in the last 5 days to a new folder

I need to copy files modified last 5 days in a folder. I am having this command
find /media/karunakar/Suppliers/xyz/ORD20130908 -name "*.DAT" -type f -mtime +5 -exec cp '{}' /media/karunakar/ord
it is not giving result
Try this:
$(which find) $FOLDER_FROM_COPY -type f -mtime +5 -regex '$\|.*dat$\|.*DAT$' -print | xargs -I '{}' -P4 -n1 $(which cp) {} $FOLDER_TO_COPY

Trying to remove a file and its parent directories

I've got a script that finds files within folders older than 30 days:
find /my/path/*/README.txt -mtime +30
that'll then produce a result such as
/my/path/jobs1/README.txt
/my/path/job2/README.txt
/my/path/job3/README.txt
Now the part I'm stuck at is I'd like to remove the folder + files that are older than 30 days.
find /my/path/*/README.txt -mtime +30 -exec rm -r {} \;
doesn't seem to work. It's only removing the readme.txt file
so ideally I'd like to just remove /job1, /job2, /job3 and any nested files
Can anyone point me in the right direction ?
This would be a safer way:
find /my/path/ -mindepth 2 -maxdepth 2 -type f -name 'README.txt' -mtime +30 -printf '%h\n' | xargs echo rm -r
Remove echo if you find it already correct after seeing the output.
With that you use printf '%h\n' to get the directory of the file, then use xargs to process it.
You can just run the following command in order to recursively remove directories modified more than 30 days ago.
find /my/path/ -type d -mtime +30 -exec rm -rf {} \;

find -mtime files older than 1 hour [duplicate]

This question already has answers here:
How to delete files older than X hours
(9 answers)
Closed 6 years ago.
I have this command that I run every 24 hours currently.
find /var/www/html/audio -daystart -maxdepth 1 -mtime +1 -type f -name "*.mp3" -exec rm -f {} \;
I would like to run it every 1 hour and delete files that are older than 1 hour. Is this correct:
find /var/www/html/audio -daystart -maxdepth 1 -mtime **+0.04** -type f -name "*.mp3" -exec rm -f {} \;
I am not sure of my use of the decimal number??
Thanks for any corrections.
EDIT
OR could I just use -mmin 60? Is this correct?
EDIT2
I tried your test, good thing you suggested it. I got an empty result. I want all files OLDER than 60mins to be deleted! How can I do this?? Does my command actually do this?
What about -mmin?
find /var/www/html/audio -daystart -maxdepth 1 -mmin +59 -type f -name "*.mp3" \
-exec rm -f {} \;
From man find:
-mmin n
File's data was last modified n minutes ago.
Also, make sure to test this first!
... -exec echo rm -f '{}' \;
^^^^ Add the 'echo' so you just see the commands that are going to get
run instead of actual trying them first.

Resources