Linux find command returns nothing


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Linux find command returns nothing
# 1  
Old 10-13-2017
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.

Code:
find . -name 'ABC_DEFGH*.*'  -mtime +20 | more

but when I run the following code it shows the list of all files:

Code:
find . -name 'ABC_DEFGH*.*'

Why -mtime doesn't work?
# 2  
Old 10-13-2017
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
# 3  
Old 10-13-2017
How to debug problems like this
steps:
Code:
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:
Code:
$ 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.

Last edited by jim mcnamara; 10-13-2017 at 10:10 AM..
These 3 Users Gave Thanks to jim mcnamara For This Post:
# 4  
Old 10-13-2017
Hi,

this should work...

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

or
Code:
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

Code:
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>


Last edited by dakelly; 10-13-2017 at 11:11 AM..
This User Gave Thanks to dakelly For This Post:
# 5  
Old 10-16-2017
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?
# 6  
Old 10-16-2017
Quote:
Originally Posted by Home
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?
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.

Code:
cd /path/to/directory
rm ABC_DEFGH*

or
Code:
rm /path/to/directory/ABC_DEFGH*

notice the lack of -r

Code:
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
# 7  
Old 10-16-2017
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:

Code:
#!/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?

Last edited by Home; 10-16-2017 at 08:55 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

rm -rf ab returns find: `./ab': No such file or directory

Hi Gurus. This is driving me a bit batty. I now if must be a simple matter but I cant find anything that references it. I have a housekeeping script that searches for some huge dump directories then removes them using rm -rf. find ./ -name 'ab' -exec rm -rf {} \; This works but always... (7 Replies)
Discussion started by: rinser
7 Replies

3. Linux

help in find command in linux

I am trying to find pictures which contains a specific word in the file name. For example any .JPG files that contains "lm" at the beginning or at the middle or at the end of the file name. find / -iname "*.jpg" | ...what should go after the pipe? regards, Moaathe (2 Replies)
Discussion started by: kidwai
2 Replies

4. Shell Programming and Scripting

find/grep returns no matches

Hi all! I've faced with very unintelligible error using find/grep like this: root@v29221:~# find /var/www/igor/data/www/lestnitsa.ru | grep u28507I get nothing as a result, but: root@v29221:~# grep u28507 /var/www/igor/data/www/lestnitsa.ru/_var.inc $db_name = 'u28507';... (2 Replies)
Discussion started by: ulrith
2 Replies

5. UNIX for Dummies Questions & Answers

echo statement when find returns null

Hi, How do you echo something once when a find statement returns null results? This is when using mutiple locations and mutiple arguments. The below find command the inner loop of a nested for loop where the outter loop holds the $args and the inner loop holds the locations. find... (2 Replies)
Discussion started by: tchoruma
2 Replies

6. Shell Programming and Scripting

How to check if a command returns nothing

Hi, I want to write a script that runs a command (at -l) and writes the output to a file. If the command (at -l) command returns no value (is empty/null) then write a message to the file in place of the command output. My problem is around trapping the empty returned command value and replacing... (2 Replies)
Discussion started by: Alvescot
2 Replies

7. UNIX for Dummies Questions & Answers

ascii FTP from Linux to Linux adding carriage returns

Hi, I've got an issue with a shell script that FTP's a file from one Linux server to another Linux server. My script runs on a Linux server and creates an output file (from a database call), and then FTP's this file to another Linux server. The problem is that, even though the output file... (0 Replies)
Discussion started by: roysterdoyster
0 Replies

8. UNIX for Dummies Questions & Answers

find command returns files with spaces, mv won't work...

Hi guys. I am trying, to move files found with the find command... Script runs fine, until it reaches a file that contains spaces... Here is what i wrote up quickly. ROOTDIR=/apps/data SEARCH=$(find /data/HDTMPRestore/home/tmq/ -type f -print | grep Mods/Input/bck | cut -c19-) for i... (1 Reply)
Discussion started by: Stephan
1 Replies

9. Shell Programming and Scripting

FIND returns different results in script

When I execute this line at the command prompt I get a different answer than when I run it in a script? Any ideas on how to resolve? I'm trying to find all files/dir in a directory except files that start with the word file. Once I get this command to work, I will add the "delete" part to the... (6 Replies)
Discussion started by: blt123
6 Replies

10. UNIX for Dummies Questions & Answers

cant find command that returns blank line

This is my list of sed commands: can anyone tell me where im going wrong. The script works on a file called data which contains 6 student id's and there answers for 6 questions. !/bin/sh sed -e 's/*//g' \ #replace * with nothing -e s/ /X/g' \ #replacing empty space with X -e... (2 Replies)
Discussion started by: jeffersno1
2 Replies
Login or Register to Ask a Question