[Solved] weird in find -exec command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] weird in find -exec command
# 1  
Old 08-30-2012
[Solved] weird in find -exec command

i feel weird with this 2 command
Code:
find /tmp/*test* -user `whoami` -mtime +1 -type f -exec rm -f {}\;
find /tmp/*test* -user `whoami` -mtime +1 -type f -exec ls -lrt {}\;

the first one return correct which only delete those filename that consist *test* where second command it listed all the files inside the directory which including those filename that not *test*

can someone help to explain?


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-30-2012 at 06:28 AM.. Reason: code tags
# 2  
Old 08-30-2012
I dont understand like you where filename consist of *test*, but look in directories having test in their directory name... and so the output isnt so weird...
if you wanted filename as *test* you should have used the -name option...
# 3  
Old 09-02-2012
both command suppose should return same file that consist *test* in the file name right? but the ls it return all the files in the directory.
eg inside the directory there are
Code:
file1
file2
file3
test1
test2
test3

when i execute
Code:
find /tmp/*test* -user `whoami` -mtime +1 -type f -exec rm -f {}\;

test1, test2 and test3 is removed

but when i execute
Code:
find /tmp/*test* -user `whoami` -mtime +1 -type f -exec ls -lrt {}\;

it return
Code:
file1
file2
file3
test1
test2
test3
file1
file2
file3
test1
test2
test3


Last edited by radoulov; 09-02-2012 at 02:43 PM..
# 4  
Old 09-02-2012
GNU find 4.4.2 on linux works as expected:
Code:
$ find /tmp/*test* -user `whoami` -type f -exec ls -lrt '{}' \;
-rw-rw-r-- 1 lem lem 0 2012-09-02 10:53 /tmp/test1
-rw-rw-r-- 1 lem lem 0 2012-09-02 10:54 /tmp/test2
-rw-rw-r-- 1 lem lem 0 2012-09-02 10:53 /tmp/test3

Of course in the command above the ls "-rt" options are meaningless, since ls is run once for every file found. This below is good:

Code:
$ find /tmp/*test* -user `whoami` -type f -exec ls -lrt '{}' +
-rw-rw-r-- 1 lem lem 0 2012-09-02 10:53 /tmp/test3
-rw-rw-r-- 1 lem lem 0 2012-09-02 10:53 /tmp/test1
-rw-rw-r-- 1 lem lem 0 2012-09-02 10:54 /tmp/test2

--
Bye
# 5  
Old 09-02-2012
Quote:
Originally Posted by Lem
GNU find 4.4.2 on linux works as expected:
If it works as it should, it will also be deleting/listing files that do not match the pattern *test*. That pattern is expanded by the shell and its result are the operands where find will begin its searches. As vbe stated, files with that pattern need to be matched, -name '*test*' should be used.

Neither the -exec rm ... nor the -ls ... versions of the command limit themselves to the set of files that the OP thinks they do. If rm is not unlinking file1, file2, and file3, it is because of a lack of privilege, but not for a lack of trying.

Regards,
Alister
# 6  
Old 09-02-2012
Quote:
Originally Posted by alister
If it works as it should, it will also be deleting/listing files that do not match the pattern *test*. That pattern is expanded by the shell and its result are the operands where find will begin its searches.
No. That pattern it's a directory tree path rooted at each given file name , so it's not necessarily a directory inside which find will begin its searches.

Quote:
SYNOPSIS
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

DESCRIPTION
This manual page documents the GNU version of find. GNU find searches the directory tree
rooted at each given file name [...]
Of course if the OP has a file whose path is, for instance, /tmp/AtestB/johndoe, it will be matched as well (according to the other tests).

Quote:
As vbe stated, files with that pattern need to be matched, -name '*test*' should be used.
It should != it must. Smilie

Quote:
If rm is not unlinking file1, file2, and file3, it is because of a lack of privilege, but not for a lack of trying.
No, I'm sorry. Try yourself.
--
Bye
# 7  
Old 09-02-2012
Quote:
Originally Posted by Lem
That pattern it's a directory tree path rooted at each given file name , so it's not necessarily a directory inside which find will begin its searches.
Notice that post #3's -exec ls ... output contains duplicate filenames. There is nothing in that find command that prints a matching file name twice (and non-consecutively), and since a directory cannot contain two identically-named files, the shell must be matching one or more directories with that pattern (in addition to *test* files in tmp, if any).

With a directory path, the -exec predicates of those find commands will list or attempt to delete every regular file in that directory and its subdirectories, regardless of a file's name.


Quote:
Originally Posted by Lem
It should != it must. Smilie
True, but it's preferable to be precise and avoid this type of unnecessary bug.

Regards,
Alister

Last edited by alister; 09-02-2012 at 04:24 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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

Find and move command with exec

Hi all, I am trying to find files newer than a given file and them mv them to a new location. So I far I have: find . ! -newer <file_name> -exec ls -l {} \; and find . ! -newer <file_name> -exec mv /TEMP_LOCATION {} \; find is not liking this. Anyone know how to modify the last... (2 Replies)
Discussion started by: jonnyd
2 Replies

3. UNIX for Dummies Questions & Answers

What does the '\' in find -exec command

Hi, I have two scripts that remove files. One works fine and is coded find -name "syst*" -mtime +1 -exec rm {} \; The other is almost the same - only thing missing is the '\'. On that script though I keep getting: rm syst1202.file ? etc Does the \ make that difference or is it a... (3 Replies)
Discussion started by: Grueben
3 Replies

4. Shell Programming and Scripting

find command with -exec

Hi all, Please could someone help with the following command requirement. I basically need to find files NEWER than a given file and order the result on time. My attempt so far is as follows: find . -newer <file_name> -exec ls -lrt {} ;\ But I dont seem to get the right result... (12 Replies)
Discussion started by: jonnyd
12 Replies

5. Shell Programming and Scripting

find command to use multiple -exec options

Hello All, Is there a way to make exec do a couple of operations on a single input from find? For example, find . -type d -exec ls -l "{}" ";" I would like to give the result of each "ls -l" in the above to a wc. Is that possible? I want to ls -l | wc -l inside exec. How do I... (1 Reply)
Discussion started by: prasanna1157
1 Replies

6. Shell Programming and Scripting

How to get the exit code of -exec in the find command

Hi I have a little problem with the find command in a script that I'm writing. The script should check if there are some files younger than 100 seconds and then syncronise them with rsync. My find command: find -type f -cmin -100 -exec rsync -a --delete directory1/ directory2/ When I... (8 Replies)
Discussion started by: oku
8 Replies

7. Shell Programming and Scripting

find command with -exec

Hi People, I have a directory full of compressed files (.Z extention) In many of these files there is a string pattern (3800078163033) I want to find all file names which contain this string in their text. Regards, Abhishek (2 Replies)
Discussion started by: max29583
2 Replies

8. UNIX for Advanced & Expert Users

Find command with prune and exec

Hi, I'm using the following command to get a list of files on the system. find /releases -type f -exec ls -l > /home/sebarry/list.txt '{}' \; however, its searching a directory I don't want it to search so I know I have to use prune but I don't seem to be able to get prune and exec to work... (1 Reply)
Discussion started by: Sebarry
1 Replies

9. UNIX for Dummies Questions & Answers

find command exec error

Hi All, i am writing a shell script in korn shell which deletes all the files in a directory once in every 10DAYS. the directory has different format files. the script has something like this; cd /home/data/pavi echo "Please Enter the Number of Days to search for" read DAYS... (2 Replies)
Discussion started by: pavan_test
2 Replies
Login or Register to Ask a Question

Featured Tech Videos