LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-11-2021, 01:48 AM   #1
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,782

Rep: Reputation: 107Reputation: 107
find, unexpected message


Hya

I cannot figure out why I am getting this error. dir2del is deleted, as I intend.
Code:
>mkdir dir2del
>find . -type d -name dir2del -print
./dir2del
>find . -type d -name dir2del -exec rm {} \;
rm: cannot remove './dir2del': Is a directory
>find . -type d -name dir2del -exec rm -r {} \;
find: ‘./dir2del’: No such file or directory
I do not think I should get "No such...". I understand that -exec clause is interpreted as "rm -r dir2del", thus "rm dir2del" results in error.

rm is aliased to "rm -i", but I think it is irrelevant. This also happens when dir2del contains file/dir.

So far, I am unable to locate reason(s) in "man rm", "info rm"...

Does any expert know why?
 
Old 10-11-2021, 02:00 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,967

Rep: Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722
in the first rm command there is no option -r, therefore rm will report: cannot rm dir
in the second rm command you used -r which means recursive delete, so it will remove dir2del and everything inside. After that find cannot continue, because the currently processed (found) dir disappeared. find would continue to go inside that dir, but lost.
 
Old 10-11-2021, 02:18 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,854
Blog Entries: 3

Rep: Reputation: 3040Reputation: 3040Reputation: 3040Reputation: 3040Reputation: 3040Reputation: 3040Reputation: 3040Reputation: 3040Reputation: 3040Reputation: 3040Reputation: 3040
Also, you can use the -delete option instead of -exec. It can be used to delete an empty directory, not just files. Otherwise, if you are going to use -exec you need the rmdir utility.
 
Old 10-11-2021, 03:30 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,628

Rep: Reputation: Disabled
Also, see discussion of directory options in the GNU findutils manual, particularly, the option -depth. BTW, -delete implies -depth, you don't have to specify it in that case.
 
Old 10-11-2021, 03:42 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,814

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
Quote:
BTW, -delete implies -depth
So true, and IMHO a wrong design!
Because it does not allow
Code:
find . -type d -path "./exludedir" -prune -o -type f -delete
One must use -exec rm here
 
Old 10-11-2021, 07:01 PM   #6
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,782

Original Poster
Rep: Reputation: 107Reputation: 107
Thanks for comments.

Yes, -d or rmdir works. My understanding is:
1. find script finds directory
2. rm -r is evoked (spawned?) then
3. find script finds next target

With "find -exec command {} +", command is evoked after find script finds all targets. In this situation (non sequential execution), it can happen.

So, when things go sequentially, unless directory structure is complex, such as

dir2del/dir2del

and dir2del (level2) is deleted with rm -r dir2del (level1)

such a situation won't happen.

In the case at post #1, directory structure is simple and "-exec rm -r {} ;", none of above applies.

cheers
 
Old 10-12-2021, 03:25 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,967

Rep: Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722
sorry, do you have a question now?
 
Old 10-12-2021, 08:20 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,814

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
rm -r is recursive, you should not run it in a recursive find.
The following lets find run on level 1 only i.e. within the start directory:
Code:
find . -mindepth 1 -maxdepth 1 -type d -name dir2del -exec rm -r {} \;
This is okay, but why not simply
Code:
\rm -rf dir2del/
?
The -f is silent if dir2del does not exist.
The trailing / ensures it is a directory.
 
Old 10-12-2021, 07:30 PM   #9
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,782

Original Poster
Rep: Reputation: 107Reputation: 107
Hya

Still, I do not understand the reason.
So, my next guess:
dir2del is found, then been deleted together with its content. (so far, no error)
nextObject is found ... (no error)

If find remembers der2del, and need to come back, then goto nextObject (cannot come back dir2del, which makes sense for that message. Why need to find the object which is already found?). But in this scenario, I somewhat worry whether or not some file/dir could be missed.


Yes, 'rm -rf dir2del/' works. But, reason for this thread is that "to remove __pycache__/*pyc". dir2del sample in #1 is the most simplified version. I think I need to use "rm -r {} ;"

cheers

Last edited by kaz2100; 10-12-2021 at 07:47 PM.
 
Old 10-13-2021, 01:06 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,967

Rep: Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722
you can safely delete the whole __pychace__ dir.
find is a process, rm is another one. When you exec any command which will alter the actual directory (remove/rename it) you definitely will confuse find (because that dir was already red and stored in the memory of find). find will not re-analyze the underlying directory structure, it just assumes the user won't put a halter round his own neck.
 
Old 10-13-2021, 06:24 PM   #11
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,782

Original Poster
Rep: Reputation: 107Reputation: 107
Hya

Now, I no longer have a question.

So, my best guess is the situation is something like
Code:
>mkdir ghostDir
>cd ghostDir
>rm -r ../ghostDir/
rm: remove directory '../ghostDir/'? y
>ls
ls: cannot open directory '.': Stale file handle
>pwd
pwd: failed to stat '.': Stale file handle
>
As long as no side effect (un-sought dir/file, orphan inode...), I have no problem.
cheers

Last edited by kaz2100; 10-13-2021 at 06:37 PM.
 
Old 10-13-2021, 07:07 PM   #12
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,782

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
A program will never do what you wish but what was implemented!
So, in this case:
A program did what I wished, but implemented to complain.
 
Old 10-14-2021, 12:48 AM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,967

Rep: Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722Reputation: 5722
Quote:
Originally Posted by kaz2100 View Post
So, in this case:
A program did what I wished, but implemented to complain.
Not really. Complaining means it is not the designed way, something went wrong. But anyway you could achieve your goal.

https://cheezburger.com/4215647488/hard-driving-it-in
 
  


Reply

Tags
exec, find, interactive mode, rm


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Command line execution error: unexpected EOF while looking for matching `"' bash: -c: line 25: syntax error: unexpected end of file maheshreddy690 Linux - Newbie 1 12-25-2018 01:13 PM
[SOLVED] "Unexpected End of File" message on LXTerminal while trying to unzip a .tar.gz file pioneermtb Debian 2 12-22-2012 07:55 PM
Unexpected message Huamin Linux - Software 5 11-20-2012 12:49 AM
Unexpected RCODE message Md.Abul Quashem Linux - Networking 2 08-22-2009 11:15 PM
GETTING "Xlib: unexpected async reply (sequence 0x9541): ERROR MESSAGE dipk_23 Programming 1 04-23-2007 06:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:40 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