Sunday, May 5, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1602  / 1 Year ago, mon, april 24, 2023, 12:43:48

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.


More From » command-line

 Answers
4

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.


[#28162] Wednesday, April 26, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
uestred

Total Points: 464
Total Questions: 104
Total Answers: 112

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
uestred questions
;