简体   繁体   中英

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 that backs up everything ending in .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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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