Linux find command returns nothing

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:

find . -name 'ABC_DEFGH*.*'

Why -mtime doesn't work?

Welcome Home,

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_*' ?

I hope that this helps,
Robin

How to debug problems like this
steps:

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.

3 Likes

Hi,

this should work...

find /path/to/files -type f -name "ABC_DEFGH*" -mtime +20

or

cd /path/to/files
find . -type f -name "ABC_DEFGH*" -mtime +20

Please bear in mind that it is looking for case and pattern before the wildcard.

also:
-mtime -20 = any file modified within last 20 days
-mtime +20 = any file modified older than the last 20 days

dakelly@TEST_SERVER: /export/home/dakelly/files> find . -type f -name "fr*"
./fruitsed.txt
./fruitlist.txt
./fruit2.txt
./fruit
dakelly@TEST_SERVER: /export/home/dakelly/files> touch fruit
dakelly@TEST_SERVER: /export/home/dakelly/files> find . -type f -name "fr*" -mtime -20
./fruit
dakelly@TEST_SERVER: /export/home/dakelly/files> find . -type f -name "fr*" -mtime +20
./fruitsed.txt
./fruitlist.txt
./fruit2.txt
dakelly@TEST_SERVER: /export/home/dakelly/files>
1 Like

Thank you all :wink: 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?

if I read this correct,
folder A has say 100 files, say 30 of which is ABC_DEFGH*
and also has folder B nad has 20ish files that has ABC_DEFGH*

if so, you can remove all from folder a and NOT folder B.

cd /path/to/directory
rm ABC_DEFGH*

or

rm /path/to/directory/ABC_DEFGH*

notice the lack of -r

rm -r /path/to/directory/ABC_DEFGH*

will recursively remove the files including in subfolders.

have a look at man rm

or check this out: Linux rm command help and examples

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

find . -name "ABC_DEFGH*" -mtime +15 | xargs -i rm {}

The glob /home/linux/txt/output/ABC_DEFGH* cannot filter for file age.
But you can do all with find, even limit the search depth

if cd /home/linux/txt
then 
  find . -maxdepth 1 -type f -name 'ABC_DEFGH*.*' -mtime +20 -print
fi

An old Unix find that does not know -maxdepth can emulate -maxdepth 1 with -prune

  find . \! -name . -prune -type f -name 'ABC_DEFGH*.*' -mtime +20 -print

The -print can be replaced with -delete if the find supports it, otherwise with -exec rm {} +

1 Like

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.

... is a performance boost in comparison to -exec rm {} \;
But nothing beats -delete !