Bash备份多个目录中指定的文件扩展名

Bash备份多个目录中指定的文件扩展名,bash,backup,file-extension,Bash,Backup,File Extension,我这里有一行find“$directory”-name“*.sh”-print0 | xargs-0cp-t~/bckup,它备份以.sh结尾的所有内容。对于多个文件扩展名,我该怎么做?我尝试了引号、括号和$的不同组合。他们都不工作=\ 我还想将某些文件扩展名备份到不同的文件夹中,但我不确定如何在文件名中搜索特定的扩展名 以下是我的全部代码,以防万一: #!/bin/bash collect() { find "$directory" -name "*.(sh|c)" -print0 | x

我这里有一行
find“$directory”-name“*.sh”-print0 | xargs-0cp-t~/bckup
,它备份以.sh结尾的所有内容。对于多个文件扩展名,我该怎么做?我尝试了引号、括号和$的不同组合。他们都不工作=\

我还想将某些文件扩展名备份到不同的文件夹中,但我不确定如何在文件名中搜索特定的扩展名

以下是我的全部代码,以防万一:

#!/bin/bash


collect()
{
find "$directory" -name "*.(sh|c)" -print0 | xargs -0 cp -t ~/bckup #xargs handles files names with spaces. Also gives error of "cp: will not overwrite just-created" even if file didn't exist previously
}

echo "Starting log"

timelimit=10
echo "Please enter the directory that you would like to collect.
If no input in 10 secs, default of /home will be selected"

read -t $timelimit directory

if [ ! -z "$directory" ] #if directory doesn't have a length of 0
then
echo -e "\nYou want to copy $directory." #-e is so the \n will work and it won't show up as part of the string
else
directory=/home/
echo "Time's up. Backup will be in $directory"
fi

if [ ! -d ~/bckup ]
then
echo "Directory does not exist, creating now"
mkdir ~/bckup
fi 

collect
echo "Finished collecting"

exit 0

一个选项可能是使用内置逻辑or(从查找手册页):

因此,在您的情况下,您可以:

find "$directory" -name '*.c' -o -name '*.sh'

哦,好吧,如果我想做的不仅仅是两个扩展,我会继续添加
-o'*.txt'-o'*.jpg'
等吗?没错。您还可以使用逻辑and(或否定)更具体,并通过使用括号强制优先级(但不要忘记从bash中转义它们)。你需要添加-name“.c”-o-name“.sh”-o-name“*…jpg”等。也修复了原来的帖子。啊,好吧,这就是它不起作用的原因。我还刚刚意识到,我可以为每组不同的文件扩展名更改文件夹,然后将它们添加到文件夹中。谢谢现在我有另一个问题。由于某些原因,当存在类似名称的文件扩展名时,它只复制最后一个文件扩展名。例如,
“.c”-o-name”.cc”-o-name“*.cxx”
将只复制.cxx。为什么呢?
find "$directory" -name '*.c' -o -name '*.sh'