[英]Bash backup specified file extensions in multiple directories
我在这里find "$directory" -name "*.sh" -print0 | xargs -0 cp -t ~/bckup
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
一种选择是使用内置逻辑或(从查找手册页):
expr1 -o expr2
Or; expr2 is not evaluated if expr1 is true.
因此,您可以执行以下操作:
find "$directory" -name '*.c' -o -name '*.sh'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.