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

时间:2013-05-13 05:47:15

标签: bash backup file-extension

我在这里有一行find "$directory" -name "*.sh" -print0 | xargs -0 cp -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

1 个答案:

答案 0 :(得分:1)

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

   expr1 -o expr2
          Or; expr2 is not evaluated if expr1 is true.

所以在你的情况下,你可以这样做:

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