我正在尝试将文件复制到相同目录但带有前缀。我正在使用xargs来做到这一点。注意:我无法使用,cd
因为它会破坏我的管道中的构建。这就是我所拥有的
root@gotoadmins-OU:~# ls
snap test
root@gotoadmins-OU:~# ls test/
me.war
root@gotoadmins-OU:~# ls test | xargs -I {} cp {} latest-{} test/
cp: cannot stat 'me.war': No such file or directory
cp: cannot stat 'latest-me.war': No such file or directory
如果我正确理解了这个问题,您只想将一个子目录中的所有文件复制到同一子目录中,但是前缀为“ latest-”。
find test -type f -execdir bash -c 'cp "$1" latest-"$(basename "$1")"' "$(which bash)" "{}" \;
$(which bash)
可以用单词bash
或其他任何东西代替。它只是一个占位符。
正如@KamilCuk所说,也可以选择使用subshell。如果将命令放在括号内(例如(cd test; for FILE in *; do cp "$FILE" latest-"$FILE"; done)
),则这些命令在子shell中运行,因此环境(包括您的工作目录)不受影响。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句