![](/img/trans.png)
[英]Bash script to copy a directory from multiple subdirectories with directory structure
[英]bash script linux - use directory as user input parameter and copy all the subdirectories to /tmp/ folder with the same name as the input directory
我想创建一个名为package.sh
的脚本,它应该:
/home/eddy
创建目录/tmp/eddy
。 /tmp
的相应目录中 我是bash脚本的新手,所以我想得到一些帮助。 非常感谢
以及为什么不将该目录/home/eddy
复制到/tmp
? 如果使用rsync
进行复制以便过滤所需的文件,则可以使用一些--exclude
标志。
那样的话呢:
#!/bin/bash
cp -r `realpath $1` /tmp
这将作为第一个参数给出的目录复制到/ tmp。
但正如你所说,你只需要复制* .txt和* .sh文件,这样就可以了
#!/bin/bash
cp `find $1 -name "*.txt" | xargs realpath` /tmp
cp `find $1 -name "*.sh" | xargs realpath` /tmp
但是这不会像你想要的那样重新创建目录结构,所以你需要cpio
#!/bin/bash
find $1 -regextype posix-awk -regex "(.*\.txt|.*\.sh)" | cpio -pdv /tmp
要包含.sh文件必须设置可执行标志的条件(如果未设置则跳过副本),那么我们必须使用两行:
#!/bin/bash
find $1 -name "*.txt" | cpio -pdv /tmp
find $1 -perm /u=x,g=x,o=x -name "*.sh" | cpio -pdv /tmp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.