Files
如何從另一個目錄一步複製並添加前綴到文件名?
我想將多個文件從一個目錄複製並重命名到另一個目錄。特別是,我想要這樣的東西:
/tmp/tmp.XXX/aaa.original.txt /tmp/tmp.XXX/bb5.original.txt /tmp/tmp.XXX/x2x.original.txt
複製到
/root/hello/dump-aaa.txt /root/hello/dump-bb5.txt /root/hello/dump-x2x.txt
我已經嘗試過一些這樣的東西,但它們不起作用:
cp /tmp/tmp.XXX/*.original.txt /root/hello/*.txt
find /tmp/tmp.XXX/ -name '*.original.txt' | xargs -i cp /root/hello/dump-{}.txt
for f in /tmp/tmp.XXX/*.original.txt; do cp -- "$f" "/root/hello/dump-$f.txt"; done
通常上述程式碼會導致錯誤:
cp: cannot create regular file '/root/hello/dump-/tmp/tmp.XXX/aaa.original.txt.txt': No such file or directory
**
bash
**解決方案:for f in /tmp/tmp.XXX/*.original.txt; do bn="${f##*/}" # extracting file basename cp "$f" "/root/hello/dump-${bn%%.*}.txt" done