簡體   English   中英

找到文件列表,創建一個目錄並在linux中復制這些文件

[英]find list of files, make a directory and copy those files in linux

例如,我想從不同的目錄中復制“file-to-copied -txt”

/home/user1/file-to-be-copied.txt
/home/user2/file-to-be-copied.txt
/home/user3/file-to-be-copied.txt

然后根據用戶帳戶創建一個新目錄

/home/user4/user1/
/home/user4/user2/
/home/user4/user3/

然后將“file-to-copied -txt”復制到新創建的目錄中

/home/user4/user1/file-to-be-copied.txt
/home/user4/user2/file-to-be-copied.txt
/home/user4/user3/file-to-be-copied.txt

我所知道的是它應該使用bash腳本來完成,但我不知道如何。 這就是我走的路

find /home . "file-to-be-copied.txt" | xargs -i mkdir ... cp {} ...

沒有找到必要的,更是如此:沒有xargs(這幾乎總是多余的find,因為find有-exec)。

cd /home
cp --parents user?/file-to-be-copied.txt user4
for f in $(/usr/bin/find '/home' -name 'file-to-be-copied.txt'); do
  tmpname=${f%/*}
  dirname=${tmpname##*/}
  /bin/mkdir -p $dirname && /bin/cp -p $f $dirname
done

這是我使用的代碼。


    for f in $(find '/home' -name 'file-to-be-copied.txt')
      do
        tmpname=${f%/*}
        dirname=${tmpname##*/}
        mkdir -p /home/user4/$dirname && /bin/cp -p $f /home/user4/$dirname

        echo $f copied to /home/user4/$dirname
      done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM