解决/usr/bin/find: Argument list too long错误,导出文件路径至Excel
Hey there, I’ve run into this exact headache before when dealing with massive directory structures—let’s get you sorted out.
The /usr/bin/find: Argument list too long error pops up because your shell is hitting the system’s ARG_MAX limit when trying to expand too many files at once. Your small folder tests worked because the number of files stayed under that threshold, but with hundreds of thousands of files, we need to work around shell expansion entirely.
Here are a few reliable solutions:
1. Nested find (most straightforward)
Instead of relying on shell globbing, use find to first locate all your Parent directories, then run a second find inside each one to grab the file paths. This avoids any argument list limits because we’re processing each Parent directory individually:
find /Tabletop -type d -name "Parent" -exec find {} -type f \; > parent_files.txt
- The first
findscans/Tabletopto find every subdirectory namedParent. -exec find {} -type f \;runs a newfindin each matchingParentdirectory to list all files (-type f).- All paths get saved to
parent_files.txt, which you can easily import into Excel.
2. Single find with path matching (more efficient)
If you want to avoid spawning multiple find processes, you can match the file paths directly in one command. This works by targeting files that live inside any Parent subdirectory:
find /Tabletop -path "*/Parent/*" -type f ! -path "*/Parent/*/Parent/*" > parent_files.txt
-path "*/Parent/*"matches any file whose path includes/Parent/.! -path "*/Parent/*/Parent/*"excludes files that are inside a nestedParentdirectory (remove this part if you want those included too).
3. Handling special characters (if needed)
If your files have weird characters like newlines or spaces, use find’s -print0 to output paths separated by null characters, then convert them to a format Excel can handle:
find /Tabletop -type d -name "Parent" -exec find {} -type f -print0 \; | tr '\0' '\n' > parent_files.txt
The tr command replaces nulls with newlines, making the file safe for Excel import.
Importing to Excel
Once you have parent_files.txt:
- Open Excel and go to Data > Get Data > From File > From Text/CSV.
- Select your
parent_files.txtfile. - In the import window, set the delimiter to Line breaks and load the data—each file path will be its own row.
Why your xargs attempt might have failed
Chances are you were using xargs with a command that still relied on shell expansion (like echo). The key is to let find handle the traversal entirely, since it doesn’t depend on the shell’s argument list limits. If you did want to use xargs, the correct syntax would be:
find /Tabletop -type d -name "Parent" | xargs -I {} find {} -type f > parent_files.txt
But the nested find method above is simpler and more reliable for this use case.
内容的提问来源于stack exchange,提问作者James Trory

