Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a shell script code which find .sql.gz files and moves them:

find $Dir -type f -mtime $Time -name \*sql.gz | while read file
do
    echo "Earlier $file will be moved to different folder." >> $Path_Log_File
    mv -f $file $Path_Folder
done

Now I have few more files with the extension .sql so how can I modify my above code so it includes this file extension as well. Thanks.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

find offers an "or" (-o) operator:

find $Path_Backup_Dir -type f -mtime $Time \( -name \*sql.gz -o -name \*.sql \)

The default operator between expressions is "and" (-a). Since "and" has higher priority than "or", we need to use parens to group the two "-name" expressions together to get the right logic. Because shells typically treat parens specially, I have escaped them with a backslash to protect them.

Alternatively, you can use a regular expression:

find $Path_Backup_Dir -type f -mtime $Time -regex '.*\.(sql|sql.gz)'

You should also note that your find/read command pipeline won't respond well to files with special names (leading space trailing spaces, newlines, etc.). See, for example, this wiki for ways to hand that issue.

share|improve this answer

Simply use the "-or" operator :

#Find and Move backup files older than the time constraint defined
find $Path_Backup_Dir -type f -mtime $Time \( -name *sql.gz -or -name *sql \) | while read file
do
    echo "Earlier Backup $file will be moved to archive folder." >> $Path_Log_File
    mv -f $file $Path_Mysql_Archive
done

You can do all with a single command:

find $Path_Backup_Dir -type f \( -name *.sql -or -name *.sql.gz \) -exec mv -f -v {} /tmp/test2 \; >> $Path_Log_File
share|improve this answer
    
It gives error: find: paths must precede expression: 01-09-2013.tar.gz Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression] –  Tarun Dec 12 '13 at 6:02
    
Do you have space characters in your path ? Escape them '\ ' or use double quote, find "$Path_Backup_Dir" –  Quentin Dec 15 '13 at 20:57
    
no there is no space character it's like /backup/dump/database/11-12-2013.sql for example. –  Tarun Dec 16 '13 at 5:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.