我正在寻找一种递归遍历目录以查找特定文件的方法,然后停止搜索并将文件名路径传递给awk函数或类似的东西.我之前问了一个类似的问题,但是在我以外的机器上进行测试后发现locate
命令不起作用,因为不是每个人都在他们的系统上使用它.
我与locate一起使用的代码:
dir="/path/to/destination/";
mkdir "$dir";
locate -l 1 target_file.txt | \
awk -v dir="$dir" '{printf "cp \"%s\" \"%s\"\n", $1, dir}' | \
sh
Run Code Online (Sandbox Code Playgroud)
该find(1)
命令将执行此操作.要获得一行,请使用head(1)
.
dir="/path/to/destination/";
mkdir "$dir";
find /path/location -name target_file.txt |
head -n 1 |
awk -v dir="$dir" '{printf "cp \"%s\" \"%s\"\n", $1, dir}' |
sh
Run Code Online (Sandbox Code Playgroud)