简体   繁体   English

bash脚本linux - 使用目录作为用户输入参数,并将所有子目录复制到/ tmp / folder,其名称与输入目录相同

[英]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的脚本,它应该:

  • Use directory as input parameter (can be relative or absolute pathname) 使用目录作为输入参数(可以是相对路径名或绝对路径名)
  • Recursively identify all sub directories of the input directory and recreate this structure in /tmp/. 递归地标识输入目录的所有子目录,并在/ tmp /中重新创建此结构。 For example: for an input parameter /home/eddy a directory /tmp/eddy is created. 例如:对于输入参数/home/eddy创建目录/tmp/eddy
  • All the text files and script files below the input directory should be copied to the corresponding directory in /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.

相关问题 Bash 脚本,用于从具有目录结构的多个子目录中复制目录 - Bash script to copy a directory from multiple subdirectories with directory structure Bash - 获取用户输入的目录 - Bash - Getting the directory of user input 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux Bash - 列出所有子目录中的程序,目录名在文件之前 - Bash - listing programs in all subdirectories with directory name before file 重命名Linux目录和子目录中的所有文件 - Rename All Files in directory and subdirectories Linux 删除linux中目录的所有子目录中的特定文件 - Delete particular files in all subdirectories of a directory in linux 将目录权限应用于Linux中的所有新子目录 - Apply directory permissions to all new subdirectories in linux Linux中当前目录下所有子目录添加后缀 - Add suffix to all subdirectories in current directory in Linux 创建 Linux Bash 脚本来复制文件和创建目录 - create Linux Bash Script to copy files and create directory 如何在Linux中以名称Bouvet-Island.png递归复制同一目录中的所有文件Norway.png - How to copy all files Norway.png in the same directory recursively with the name Bouvet-Island.png in Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM