Unix环境下如何tail find结果文件?自定义函数无输出排查
Hey there! Let's work through your two issues one by one—no worries, this is super common for folks new to bash scripting and cluster workflows.
1. Fixing the Custom Function That Only Outputs >
First off, that > you're seeing is a clear sign your function has a syntax or logic misstep. Chances are, you accidentally wrote something like this in your .bashrc:
function monitor() { > $1 }
That line > $1 doesn't read the file—it tells bash to redirect empty output to the file you specified. Since there's no input to redirect, bash drops you into a secondary prompt (the >) waiting for you to type something.
Here's a corrected, flexible function that does exactly what you want—we'll set it up for real-time monitoring (perfect for running cluster tasks), with options to adjust if you just need to view existing content:
# Add this to your .bashrc (functions work fine outside the alias section too) function task_monitor() { # Check if you provided a filename if [ $# -eq 0 ]; then echo "Oops! Usage: task_monitor <output-file>" return 1 fi # Pick the right command for your needs: tail -f "$1" # Real-time tracking (great for ongoing tasks) # OR tail "$1" # Just show the last 10 lines # OR cat "$1" # Show the entire file }
After adding this, run source ~/.bashrc to reload your config, then test it with task_monitor dm.o1234—you should see the file content as expected.
2. Running tail on Files Found with find
There are a few clean, reliable ways to do this, depending on your use case:
Option 1: Use find's built-in -exec (best for special filenames)
This is the safest method, especially if your filenames might have spaces or special characters:
# View last 10 lines of all dm.o* files in current directory/subdirectories find . -name "dm.o*" -type f -exec tail {} +
{}gets replaced with each filefindlocates- Using
+instead of;passes all files to a singletailcommand (so you get clear filename headers in the output) - Swap
tailwithtail -n 20if you want more than 10 lines
Option 2: Use xargs (quick for standard filenames)
If you're sure your filenames don't have spaces, this works great:
find . -name "dm.o*" -type f | xargs tail
For filenames with spaces, use the safe null-terminator version:
find . -name "dm.o*" -type f -print0 | xargs -0 tail
Option 3: Real-time monitor all matching files
If you want to track updates to all dm.o* files at once (like multiple running tasks):
find . -name "dm.o*" -type f -exec tail -f {} +
tail -f will watch each file and print new content as it's added—handy for keeping an eye on multiple jobs at once.
内容的提问来源于stack exchange,提问作者benamoussa

