简体   繁体   English

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

[英]Bash backup specified file extensions in multiple directories

I have this line here find "$directory" -name "*.sh" -print0 | xargs -0 cp -t ~/bckup 我在这里find "$directory" -name "*.sh" -print0 | xargs -0 cp -t ~/bckup find "$directory" -name "*.sh" -print0 | xargs -0 cp -t ~/bckup that backs up everything ending in .sh. find "$directory" -name "*.sh" -print0 | xargs -0 cp -t ~/bckup备份以.sh结尾的所有内容。 How would I do that for multiple file extensions? 对于多个文件扩展名,我该怎么办? I've tried different combinations of quotes, parentheses and $. 我尝试了引号,括号和$的不同组合。 None of them worked =\\ 他们都没有工作= \\

I would also like to back up certain file extensions into different folders and I'm not sure how to search a file name for a specific extension. 我也想将某些文件扩展名备份到不同的文件夹中,但不确定如何在文件名中搜索特定扩展名。

Here is my whole code just in case: 这是我的整个代码,以防万一:

#!/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

One option might be to use the builtin logical or (from find man page): 一种选择是使用内置逻辑或(从查找手册页):

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

So in your case you can do: 因此,您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM