我正在搜索特定目录和子目录以查找新文件,我想复制这些文件。我正在使用这个:
find /home/foo/hint/ -type f -mtime -2 -exec cp '{}' ~/new/ \;
它正在成功复制文件,但有些文件在/home/foo/hint/
. 我想将文件及其基本目录复制到~/new/
目录中。
test@serv> find /home/foo/hint/ -type f -mtime -2 -exec ls '{}' \;
/home/foo/hint/do/pass/file.txt
/home/foo/hint/fit/file.txt
test@serv>
~/new/
复制后应该是这样的:
test@serv> ls -R ~/new/
/home/test/new/pass/:
file.txt
/home/test/new/fit/:
file.txt
test@serv>
平台:Solaris 10。
由于您不能使用 rsync 或花哨的 GNU 选项,因此您需要使用 shell 自行滚动。
该find
命令允许您在 .bashrc 中运行完整的 shell -exec
,因此最好使用单行来处理名称。
如果我理解正确,您只希望将父目录而不是完整的树复制到目标。以下可能会做:
#!/usr/bin/env bash
findopts=(
-type f
-mtime -2
-exec bash -c 'd="${0%/*}"; d="${d##*/}"; mkdir -p "$1/$d"; cp -v "$0" "$1/$d/"' {} ./new \;
)
find /home/foo/hint/ "${findopts[@]}"
结果:
$ find ./hint -type f -print
./hint/foo/slurm/file.txt
./hint/foo/file.txt
./hint/bar/file.txt
$ ./doit
./hint/foo/slurm/file.txt -> ./new/slurm/file.txt
./hint/foo/file.txt -> ./new/foo/file.txt
./hint/bar/file.txt -> ./new/bar/file.txt
我已将选项find
放入 bash 数组中,以便于阅读和管理。该-exec
选项的脚本仍然有点笨拙,因此这里细分了它对每个文件的作用。请记住,在这种格式中,选项从零开始编号,{}
变成$0
和目标目录变成$1
...
d="${0%/*}" # Store the source directory in a variable, then
d="${d##*/}" # strip everything up to the last slash, leaving the parent.
mkdir -p "$1/$d" # create the target directory if it doesn't already exist,
cp "$0" "$1/$d/" # then copy the file to it.
我用于cp -v
详细输出,如上面的“结果”所示,但 IIRC 也不支持 Solaris,可以安全地忽略它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句