1

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.

2 Answers 2

3

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.

2

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
3
  • 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, 2013 at 6:02
  • Do you have space characters in your path ? Escape them '\ ' or use double quote, find "$Path_Backup_Dir"
    – Quentin
    Dec 15, 2013 at 20:57
  • no there is no space character it's like /backup/dump/database/11-12-2013.sql for example.
    – Tarun
    Dec 16, 2013 at 5:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .