[英]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
I want to create a script called package.sh
which should: 我想创建一个名为
package.sh
的脚本,它应该:
/home/eddy
a directory /tmp/eddy
is created. /home/eddy
创建目录/tmp/eddy
。 /tmp
/tmp
的相应目录中 I am new to bash script so I would like to get some help. 我是bash脚本的新手,所以我想得到一些帮助。 Thanks so much
非常感谢
well why not just copy that directory /home/eddy
to /tmp
? 以及为什么不将该目录
/home/eddy
复制到/tmp
? you can use some --exclude
flags if you use rsync
for copying in order to filter the files you need. 如果使用
rsync
进行复制以便过滤所需的文件,则可以使用一些--exclude
标志。
Something like this then: 那样的话呢:
#!/bin/bash
cp -r `realpath $1` /tmp
this copies the dir given as the first argument, to /tmp. 这将作为第一个参数给出的目录复制到/ tmp。
But as you say you only want *.txt and *.sh files copied so this should work instead 但正如你所说,你只需要复制* .txt和* .sh文件,这样就可以了
#!/bin/bash
cp `find $1 -name "*.txt" | xargs realpath` /tmp
cp `find $1 -name "*.sh" | xargs realpath` /tmp
But this doesn't recreate the directory structure like you want so you need cpio for that 但是这不会像你想要的那样重新创建目录结构,所以你需要cpio
#!/bin/bash
find $1 -regextype posix-awk -regex "(.*\.txt|.*\.sh)" | cpio -pdv /tmp
To include the criteria that the .sh files have to have the executable flag set (skip the copy if it is not set) then we have to use two lines: 要包含.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.