LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-14-2009, 02:39 AM   #1
mynameisthomas
LQ Newbie
 
Registered: Feb 2008
Distribution: Fedroa 10
Posts: 19

Rep: Reputation: 0
find +exec with rm gives unnecessary error


Hi,

I am using 'find' command with 'rm' to delete some folders. Though it deletes them, but at the same time it gives an error.

Command I use is

Code:
find . -name 'artrafoaeep2.part?' -exec rm -r '{}' \;
Though it deletes those folders but it also gives

Code:
find: `./artrafoaeep2.part1': No such file or directory
Here is the detailed listing of my directory.

Code:
[Thomas@dhcppc1 Nero]$ ls
artrafoaeep2.part1      artrafoaeep2.part2.rar  artrafoaeep2.part4      artrafoaeep2.part5.rar  artrafoaeep2.part7      artrafoaeep2.part8.rar
artrafoaeep2.part1.rar  artrafoaeep2.part3      artrafoaeep2.part4.rar  artrafoaeep2.part6      artrafoaeep2.part7.rar
artrafoaeep2.part2      artrafoaeep2.part3.rar  artrafoaeep2.part5      artrafoaeep2.part6.rar  artrafoaeep2.part8
[Thomas@dhcppc1 Nero]$
I just want to delete the folders (those that doesn't have .rar extension)
So I used

Code:
[Thomas@dhcppc1 Nero]$ find . -name 'artrafoaeep2.part?' -exec rm -r '{}' \;
But this gives me

Code:
[Thomas@dhcppc1 Nero]$ find . -name 'artrafoaeep2.part?' -exec rm -r '{}' \;
find: `./artrafoaeep2.part1': No such file or directory
find: `./artrafoaeep2.part2': No such file or directory
find: `./artrafoaeep2.part3': No such file or directory
find: `./artrafoaeep2.part4': No such file or directory
find: `./artrafoaeep2.part5': No such file or directory
find: `./artrafoaeep2.part6': No such file or directory
find: `./artrafoaeep2.part7': No such file or directory
find: `./artrafoaeep2.part8': No such file or directory
[Thomas@dhcppc1 Nero]$
Surprisingly, all the folders have been deleted, as I wanted :

Code:
[Thomas@dhcppc1 Nero]$ ls
artrafoaeep2.part1.rar  artrafoaeep2.part3.rar  artrafoaeep2.part5.rar  artrafoaeep2.part7.rar
artrafoaeep2.part2.rar  artrafoaeep2.part4.rar  artrafoaeep2.part6.rar  artrafoaeep2.part8.rar
[Thomas@dhcppc1 Nero]$
Now what I don't understand is why I get the error 'No such file or directory'. How should I avoid this error?

Any help will be really appreciated.
 
Old 08-14-2009, 03:37 AM   #2
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 328

Rep: Reputation: 42
Use double quotes around the file name:
Code:
find . -name 'artrafoaeep2.part?'
replace with:
Code:
find . -name "artrafoaeep2.part?"
I've only recently learned a little about using find, but I know that I've never personally seen the '' used with the {}.
You should replace
Code:
-exec rm -r '{}' \;
with
Code:
-exec rm -r {} \;
as far as I know. This could also help:
http://www.oracle.com/technology/pub...lish-find.html

Last edited by joeBuffer; 08-14-2009 at 04:06 AM.
 
Old 08-14-2009, 10:15 AM   #3
mynameisthomas
LQ Newbie
 
Registered: Feb 2008
Distribution: Fedroa 10
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks joeBuffer for replying to my question.

I tried your proposed solution but unfortunately it didn't work.

Here is my experience :

Code:
[Thomas@dhcppc1 Rapid]$ ls
number0  number1  number2  number3  number4  number5  number6  number7  number8  number9  p.sh
[Thomas@dhcppc1 Rapid]$ find . -name "number?" -exec rm -r {} \;
find: `./number1': No such file or directory
find: `./number9': No such file or directory
find: `./number8': No such file or directory
find: `./number5': No such file or directory
find: `./number7': No such file or directory
find: `./number2': No such file or directory
find: `./number0': No such file or directory
find: `./number3': No such file or directory
find: `./number4': No such file or directory
find: `./number6': No such file or directory
[Thomas@dhcppc1 Rapid]$ ls
p.sh
[Thomas@dhcppc1 Rapid]$
So how can I avoid this error? Any idea?
 
Old 08-14-2009, 10:20 AM   #4
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 328

Rep: Reputation: 42
I really have no idea. When using find, it just won't display anything if it doesn't find what you're searching for. It's always been that way, as far as I know. I'm sure someone will be able to help more ...
 
Old 08-14-2009, 10:59 AM   #5
mynameisthomas
LQ Newbie
 
Registered: Feb 2008
Distribution: Fedroa 10
Posts: 19

Original Poster
Rep: Reputation: 0
Well, I got different command combination to get the same affect, but without any error message.

Code:
[Thomas@dhcppc1 Rapid]$ ls
number0  number1  number2  number3  number4  number5  number6  number7  number8  number9  p.sh
[Thomas@dhcppc1 Rapid]$ rm -r ` find . -name 'number?' `
[Thomas@dhcppc1 Rapid]$ ls
p.sh
[Thomas@dhcppc1 Rapid]$
Here I have used ` (back quotes) before find, not to be confused with single quotes '.

Though, this new command combination works flawlessly, but I will still appreciate if someone explains the problem with find+exec version of the command which I posted in the original post.

Thanks.
 
Old 08-17-2009, 06:33 AM   #6
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by mynameisthomas View Post
Well, I got different command combination to get the same affect, but without any error message.

Code:
[Thomas@dhcppc1 Rapid]$ ls
number0  number1  number2  number3  number4  number5  number6  number7  number8  number9  p.sh
[Thomas@dhcppc1 Rapid]$ rm -r ` find . -name 'number?' `
[Thomas@dhcppc1 Rapid]$ ls
p.sh
[Thomas@dhcppc1 Rapid]$
Here I have used ` (back quotes) before find, not to be confused with single quotes '.

Though, this new command combination works flawlessly, but I will still appreciate if someone explains the problem with find+exec version of the command which I posted in the original post.

Thanks.
only explanation i have is that this error is most likely local to you only

you see if i run that command anywhere on a machine, it just returns silently as it did not find anything.

Code:
[root@rolinux log]# find . -maxdepth 1 -name  '*.?'
./maillog.2
./spooler.1
./boot.log.2
./secure.2
./cron.3
./rpmpkgs.2
./maillog.3
./cron.4
./spooler.2
./messages.2
./messages.1
./secure.3
./rpmpkgs.1
./spooler.4
./secure.4
./cron.1
./maillog.4
./boot.log.3
./messages.4
./boot.log.4
./secure.1
./spooler.3
./messages.3
./boot.log.1
./cron.2
./rpmpkgs.3
./rpmpkgs.4
./maillog.1
here

Code:
[root@rolinux log]# find . -name "number?" -exec rm -r {} \;
[root@rolinux log]#
finds nothing.

but if i run your command but just replace 'number?' with regexp matching

Code:
find . -maxdepth 1 -name  '*.?' -exec rm -r {} \;
[root@rolinux log]# find . -maxdepth 1 -name  '*.?'
[root@rolinux log]# ls
acpid            atop      clamav      cups     httpd    maillog     ppp      rsnapshot         secure          tor         Xorg.0.log.old
anaconda.log     audit     conman      dmesg    iptraf   messages    prelink  sa                setroubleshoot  vtund       yum.log
anaconda.syslog  boot.log  conman.old  faillog  lastlog  mysqld.log  privoxy  samba             spooler         wtmp
anaconda.xlog    btmp      cron        gdm      mail     pm          rpmpkgs  scrollkeeper.log  tallylog        Xorg.0.log
[root@rolinux log]#
everything i searched for is removed silently.
even if there are no such files, i am presented with a silent return.

this is why i think the issue you had is a local one rather then a general one with the command being used.
 
Old 08-17-2009, 06:56 AM   #7
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by centosboy View Post
only explanation i have is that this error is most likely local to you only
Might just be the small diffrence but *.? sure is not asdf.part?.

Code:
[test@test test]# find . -name 'afro?' -exec rm -r {} \;
find: ./afro4: No such file or directory
find: ./afro6: No such file or directory
find: ./afro2: No such file or directory
find: ./afro7: No such file or directory
find: ./afro9: No such file or directory
find: ./afro1: No such file or directory
find: ./afro5: No such file or directory
find: ./afro3: No such file or directory
find: ./afro8: No such file or directory
[test@test test]# ls -lh
total 44K
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro10
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro10.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro11
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro11.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro12
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro12.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro13
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro13.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro14
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro14.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro15
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro15.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro16
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro16.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro17
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro17.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro18
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro18.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro19
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro19.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro1.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro20
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro20.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro2.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro3.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro4.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro5.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro6.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro7.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro8.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro9.rar
Same on RHEL 5. Steps to repdroduce

Code:
for i in $(seq 1 9); do mkdir ./test$i; done
find ./ -name 'test?' -exec ls -ld {} \;
find ./ -name 'test?' -exec rm -r {} \;
Also I don't have an explanation
 
Old 08-17-2009, 07:03 AM   #8
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by zhjim View Post
Might just be the small diffrence but *.? sure is not asdf.part?.

Code:
[test@test test]# find . -name 'afro?' -exec rm -r {} \;
find: ./afro4: No such file or directory
find: ./afro6: No such file or directory
find: ./afro2: No such file or directory
find: ./afro7: No such file or directory
find: ./afro9: No such file or directory
find: ./afro1: No such file or directory
find: ./afro5: No such file or directory
find: ./afro3: No such file or directory
find: ./afro8: No such file or directory
[test@test test]# ls -lh
total 44K
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro10
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro10.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro11
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro11.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro12
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro12.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro13
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro13.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro14
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro14.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro15
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro15.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro16
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro16.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro17
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro17.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro18
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro18.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro19
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro19.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro1.rar
drwxr-xr-x 2 root root 4.0K Aug 17 13:48 afro20
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro20.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro2.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro3.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro4.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro5.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro6.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro7.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro8.rar
-rw-r--r-- 1 root root    0 Aug 17 13:48 afro9.rar
Same on RHEL 5. Steps to repdroduce

Code:
for i in $(seq 1 9); do mkdir ./test$i; done
find ./ -name 'test?' -exec ls -ld {} \;
find ./ -name 'test?' -exec rm -r {} \;
Also I don't have an explanation
ok...so it is not a local error then....

but i cant reproduce....
but i will keep trying tho...
 
Old 08-17-2009, 07:15 AM   #9
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Looks kinda like a race condition to me.
find is deleting the folders before the loop can access the folders.


try running with this (-delete) flag


Code:
find . -name 'filename?' -delete
its in newer versions of findutils.

Code:
[root@cuba log]# rpm -qi findutils
Name        : findutils                    Relocations: (not relocatable)
Version     : 4.2.27                            Vendor: CentOS
Release     : 4.1                           Build Date: Sun 07 Jan 2007 12:30:45 GMT
Install Date: Fri 09 May 2008 17:39:08 BST      Build Host: builder1.centos.org
Group       : Applications/File             Source RPM: findutils-4.2.27-4.1.src.rpm
Size        : 677405                           License: GPL
Signature   : DSA/SHA1, Wed 04 Apr 2007 01:21:41 BST, Key ID a8a447dce8562897
URL         : http://www.gnu.org/software/findutils/
Summary     : The GNU versions of find utilities (find and xargs).

Last edited by centosboy; 08-17-2009 at 07:19 AM.
 
Old 08-17-2009, 07:29 AM   #10
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by centosboy View Post
try running with this (-delete) flag
-delete is a happy fellow. No errors and all folders deleted.

We'll see maybe someone can enlighten us.
 
Old 08-17-2009, 07:48 AM   #11
:::
Member
 
Registered: Aug 2009
Distribution: slackware 12.2
Posts: 51

Rep: Reputation: 17
find is working fine. i can reproduce the "error" but it is human error. think about it, what is find doing (as opposed to what you think find does). it took me a while to figure it out .

setup:
Code:
$ ls
dir.1          dir.2          dir.3          dir.4
dir.1.tar.bz2  dir.2.tar.bz2  dir.3.tar.bz2  dir.4.tar.bz2
i wanted to rm all the dir (not the tar balls). i did:
Code:
$ find . -name 'dir.?' -exec rm -r '{}' \;
find: ./dir.1: No such file or directory
find: ./dir.3: No such file or directory
find: ./dir.4: No such file or directory
find: ./dir.2: No such file or directory
what does find do?
1. find takes (not necessarily) the first entry in the ./ directory. this would be e.g. dir.1/
2. it compares it to the pattern 'dir.?'. does it match? yes.
3. find executes "rm -r dir.1".
4. find tries to enter dir.1/ to find the pattern within the directory. it doesn't know anything about the exec command.
5. it doesn't find dir.1/ anymore. returns ENOENT (look at the strace output)

the same is true for the other dirs.

proof:
if you use:
Code:
$ find . -name 'dir.?' -exec rm -r '{}' +
instead, find first tries to find all occurences of the pattern and builds up the exec command occurence for occurence. after it travelled all the subdirs it will execute the command:
Code:
rm -r dir.1/ dir.2/ dir.3/ dir.4/
but be careful! if ./dir.1 containes another 'dir.?' you'll again get an error message saying:
Code:
rm: cannot remove `./dir.1/dir.1': No such file or directory
but this time it's rm complaining.

bottomline: don't use -exec. apart from such subtle trap lines it's unsecure. better use -execdir (see manpage). even better and easier, use xargs.

Code:
$ find . -name 'pattern' | xargs rm -r
cheers :::
 
Old 08-17-2009, 08:51 AM   #12
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Nice one. Another point for the computer not making any errors. The biggest error is 10inch in front of the screen

Thanks for laying this out.
 
Old 08-19-2009, 02:53 PM   #13
mynameisthomas
LQ Newbie
 
Registered: Feb 2008
Distribution: Fedroa 10
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ::: View Post
what does find do?
1. find takes (not necessarily) the first entry in the ./ directory. this would be e.g. dir.1/
2. it compares it to the pattern 'dir.?'. does it match? yes.
3. find executes "rm -r dir.1".
4. find tries to enter dir.1/ to find the pattern within the directory. it doesn't know anything about the exec command.
5. it doesn't find dir.1/ anymore. returns ENOENT (look at the strace output)
Thanks :::, that was a fantastic explanation. With the help of your post I tried find with maxdepth option and didn't get any error :

Code:
[Thomas@dhcppc0 temp]$ ls
number0  number1  number2  number3  number4  number5  number6  number7  number8  number9  p.sh
[Thomas@dhcppc0 temp]$ find . -maxdepth 1 -iname 'number?' -exec rm -r '{}' \;
[Thomas@dhcppc0 temp]$ ls
p.sh
[Thomas@dhcppc0 temp]$
Quote:
Originally Posted by ::: View Post
bottomline: don't use -exec. apart from such subtle trap lines it's unsecure. better use -execdir (see manpage). even better and easier, use xargs.

Code:
$ find . -name 'pattern' | xargs rm -r
cheers :::
Thanks for this advice too, I will try to learn these new commands too.

Thanks to all the members who participated in this thread. I really appreciate the help offered by all you guys.

Regards
 
Old 08-19-2009, 03:13 PM   #14
:::
Member
 
Registered: Aug 2009
Distribution: slackware 12.2
Posts: 51

Rep: Reputation: 17
Quote:
Originally Posted by mynameisthomas View Post
With the help of your post I tried find with maxdepth option and didn't get any error :
maxdepth is another good option
Quote:
Originally Posted by mynameisthomas View Post
Thanks for this advice too, I will try to learn these new commands too.
i sometime get myself baffled about these commands and i'm still trying to learn how to use them correctly (http://www.linuxquestions.org/questi...mad-cd-748671/).
cheers :::
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find with -exec argument - not giving proper output..how to find... hinetvenkat Linux - Server 4 01-25-2010 06:19 AM
find -exec syntax: bug in find? mfcarroll Programming 5 06-21-2007 07:13 PM
find -exec help dc144 Linux - Newbie 3 10-12-2006 02:14 AM
find -exec question eantoranz Linux - General 1 06-26-2004 10:57 AM
find -exec cricbk Linux - Newbie 4 01-05-2004 07:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration