No Result
View All Result
CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home Linux

Linux find -exec rm -r report No such file or directory

by npn
June 25, 2019
in Linux
Reading Time: 3 mins read
0
0
SHARES
2.2k
VIEWS
Share on FacebookShare on Twitter

Contents

  • 1 System environment Ubuntu 16.04.3 LTS
  • 2 Scene simulation:
  • 3 There are many solutions, and only the common methods are listed below:
    • 3.1 1. Use find’s -maxdepth OPTIONS:
    • 3.2 2. Use find’s -prune ACTIONS:
    • 3.3 3. Use + (plus sign) as the terminator of the find command without using it; (semicolon)
Rate this post

System environment Ubuntu 16.04.3 LTS

When writing a batch of docker image scripts, first copy the code directory to the corresponding directory where the image is created, then traverse the image directory to execute the build, make the image, and after the image build is completed, delete the code directory and delete the code directory. Find -exec combination command, but reported:

No such file or directory

Scene simulation:

zzfdeMacBook-Air:temp zzf$ mkdir testaaa
zzfdeMacBook-Air:temp zzf$ ls | grep testaaa
testaaa
zzfdeMacBook-Air:temp zzf$ find . -type d -name "testaaa" -exec rm -r {} \;
find: ./testaaa: No such file or directory

Find the testaaa directory again and find that the testaaa directory has indeed been deleted. But why would you report: No such file or directory?

zzfdeMacBook-Air:temp zzf$ ls testaaa
ls: testaaa: No such file or directory

After checking the information of find, it is found that the default is recursive lookup. When executing the above combined command to delete the directory, it will traverse the testaaa directory. The actual execution process is as follows:

  1. Query all directories under the current directory
  2. Perform pattern matching ‘testaaa’, match successfully? success.
  3. Command after executing exec: rm -r testaaa
  4. Find tries to enter the testaaa/ directory, find the directory or file, and execute the command after exec
  5. Find does not find the testaaa directory, returns ENOENT (No such file or directory)

There are many solutions, and only the common methods are listed below:

1. Use find’s -maxdepth OPTIONS:

-maxdepth levels: Specifies the maximum directory depth for the actions of tests and actions, which can only be non-negative integers. It can be easily understood as the depth of directory search, but it is not. The current path directory has a level of 1, so if you specify -maxdepth 0 you won't get any results.

zzfdeMacBook-Air:temp zzf$ ls -d testaaa
testaaa
zzfdeMacBook-Air:temp zzf$
zzfdeMacBook-Air:temp zzf$ find . -maxdepth 1 -type d -name "testaaa" -exec rm -r {} \;
zzfdeMacBook-Air:temp zzf$ echo $?
0
zzfdeMacBook-Air:temp zzf$ ls -d testaaa
ls: testaaa: No such file or directory

2. Use find’s -prune ACTIONS:

-prune: Do not enter the directory (tell find, do not look for subdirectories or files in the directory to be deleted), so it can be used to ignore directories, but will not ignore normal files. When -depth is not given, it always returns true. If -depth is given, it returns false directly, so -delete (implicit -depth) is not used with -prune.

ADVERTISEMENT
zzfdeMacBook-Air:temp zzf$ ls -d testaaa
testaaa
zzfdeMacBook-Air:temp zzf$ find . -type d -name "testaaa" -prune -exec rm -r {} \;
zzfdeMacBook-Air:temp zzf$ echo $?
0
zzfdeMacBook-Air:temp zzf$ ls -d testaaa
ls: testaaa: No such file or directory

3. Use + (plus sign) as the terminator of the find command without using it; (semicolon)

+ and ; difference:

; is the find traversal once to execute the command after the exec, and + will split the file or directory found in the batch, the batch runs the command, so it will not return an error, and when the content is too much, + will reduce the CPU usage.

zzfdeMacBook-Air:temp zzf$ ls -d testaaa
testaaa
zzfdeMacBook-Air:temp zzf$ find . -type d -name "testaaa" -exec rm -r {} +
zzfdeMacBook-Air:temp zzf$ echo $?
0
zzfdeMacBook-Air:temp zzf$ ls -d testaaa
ls: testaaa: No such file or directory

Of course you can also use | (pipe) + xargs :

find . -type d -name "testaaa" | xargs rm -r

But when testaaa does not exist, executing this combination command will return a non-zero status code, which is not in line with my scenario.

Tags: BashLinuxLinux findNo such file or directory
ShareTweetShare
Previous Post

Disable browser ‘Back’ button with Javascript

Next Post

RPC load balancing strategy

npn

Related Posts

Linux

Ubuntu 22.04 versus 20.04 comparison

April 26, 2022
37.7k
Linux

Linux’s last command

April 26, 2022
635
Linux

What is the location of the MySQL databases?

April 24, 2022
598
Linux

How to find the Linux file creation date?

April 24, 2022
772
Javascript

Configuring VS Code for Node/JavaScript Development

August 2, 2021
1.3k
Linux

How to create a self-signed SSL certificate for Apache on Ubuntu 16.04

July 18, 2021
1k
Next Post

RPC load balancing strategy

Discussion about this post

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
Exit mobile version