执行find命令查找文件时出现'./proc/xxxx'不存在错误的原因与解决办法
find Errors & How to Fix Them Hey there, let's break down what's happening here and how to fix this annoying issue.
What's Causing the Errors?
Those find: './proc/XXXX': No such file or directory errors are totally expected when your find command traverses directories that include /proc. Here's the breakdown:
/procis a virtual filesystem that dynamically creates directories for every running process (each directory name matches a process ID, or PID).- When
findstarts scanning/proc, it might list a process directory (like./proc/12049), but by the time it tries to access that directory, the process has already exited. The OS deletes the proc directory for terminated processes immediately, sofindcan't locate it anymore—hence the error.
Since you're hunting for backup files, you almost certainly don't need anything from /proc anyway—it stores runtime process info, not persistent data you'd want to back up.
Fixes to Try
1. Explicitly Skip the /proc Directory
This is the cleanest solution because it stops find from even trying to traverse /proc in the first place. Modify your command like this:
find . -path ./proc -prune -o -name "$tar_file_name" -type f -print0 | xargs -0
-path ./proc -prunetellsfindto skip the./procdirectory and all its contents.- The
-o(or) ensures your original search conditions (-name "$tar_file_name" ...) still apply to all other directories. - Always wrap
$tar_file_namein double quotes to handle filenames with spaces or special characters.
2. Suppress Error Output
If you just want to hide the errors without adjusting the traversal (a quick fix, though not the most elegant), redirect error messages to /dev/null:
find . -name "$tar_file_name" -type f -print0 2>/dev/null | xargs -0
This sends all error logs to the "bit bucket" so you don't see them, though find will still attempt to scan /proc in the background.
3. Restrict to the Current Filesystem
Use the -xdev (or -mount, depending on your find version) option to make find only traverse the current filesystem. Since /proc is a separate virtual filesystem, this will automatically exclude it (along with other virtual filesystems like /sys or /dev):
find . -xdev -name "$tar_file_name" -type f -print0 | xargs -0
This is a great option if you want to avoid all non-persistent virtual filesystems in one go.
内容的提问来源于stack exchange,提问作者Heisenberg

