Under one of my directories on server I have more than 500 files with different type and name. When I run the find command to list the files with 'ABC_DEFGH' in the begining of its name and older than 20 days, nothing is return as result. Though I know there are more than 400 files which their name begins with 'ABC_DEFGH_'.
Here is the command that I'm running.
find . -name 'ABC_DEFGH*.*' -mtime +20 | more
but when I run the following code it shows the list of all files:
Can you list a few files that it does find without the modification time restriction? I suspect that they are all modified (contents edited) more recently than 20 days. Have you got the search backwards perhaps? What is the requirement?
You also have an odd search pattern which doesn't match your requirements, so you are rather getting-away-with-it. Would your search pattern not be just 'ABC_DEFGH_*' ?
cd /path/to/files
# are we in the right directory?
pwd
# filename to find is ABC_DEFGH*
ls -l ABC_DEFGH* |tail -1
# if all of this works out correctly - and you carefully read the date from the ls command
# then the find command you gave will return files
Also note: mtime does this:
filetimes are stored as seconds since Jan 1 1970, which is usually a large number:
$ date +%s
1507899718
mtime gets that number for right now, then mutiplies the number of seconds, 86400, times the number of days: 20 * 86400 = 1728000.
Next, it subtracts that smaller number of seconds from right now: 1507899718 - 1728000 = 1506171718
So find is really looking for files that have filetimes of 1506171718 or less. Nowhere did I mention anything about calendars. This does not exactly match what your calendar tells you.
You could have a file that is 20 calendar days old but find still would not see it because the calculation is not based on calendars.
Thank you all The issue is resolved. Now I can see the list of files when I run the command.
But I have another question. In this directory i have some other subdirectories that contains files with similar name(begins with ABC_DEFGH). How can I remove/delete these files only in the current directory and not in other subdirectories?
Actually, I'm working with a shell code so that it removes files older than 32 days in current directory(and not in subdirectories).
Here is my code, first I try to test my program by finding the right files and then add remove command:
#!/bin/sh
for filename in /home/linux/txt/output/ABC_DEFGH*
do
if test 'find . maxdepth 1 -type f -name "ABC_DEFGH*" -mtime +32'; then
#remove command should be here***
fi
done
exit 0
How can I add 'remove' command with propper options(I'm new in Linux)? Any suggestion?
I have a script that does something similar, and this is what I use, but mine is older than 15 days.. only changed to your search name terms.
you might want to keep your maxdepth, and change your length of days
BTW - -exec rm {} + the + tells find it can run the rm command with more than one single filename as a parameter. This provides a performance boost when you expect to delete hundreds of files, since the process required to run the rm command (or other commands using this syntax) is forked (created) much less frequently.