簡體   English   中英

Bash備份多個目錄中的指定文件擴展名

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM