简体   繁体   English

找到文件列表,创建一个目录并在linux中复制这些文件

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

For example, I want to copy the "file-to-be-copied.txt" from different directories 例如,我想从不同的目录中复制“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

Then create a new directory based on the user account 然后根据用户帐户创建一个新目录

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

Then copy the "file-to-be-copied.txt" to the new created directories 然后将“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

All I know is that it should be done using bash scripting but I don't know how. 我所知道的是它应该使用bash脚本来完成,但我不知道如何。 This is as far as I go 这就是我走的路

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

No find necessary, and more so: no xargs (which is almost always superfluous with find, since find has -exec). 没有找到必要的,更是如此:没有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

This is the code I used. 这是我使用的代码。


    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