Script to find only files that contain pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to find only files that contain pattern
# 1  
Old 08-21-2015
Script to find only files that contain pattern

Actually I googled it and found != *Frequent* works!
So now I have zero questions
Thanks for reading, anyway!

Actually I got this working see below. I just now have one question:

How do you use a test condition (such as with Frequent) that takes whitespace into account
for example, if the file is Frequent List Home Items?
At this point the condition breaks up the file

Code:
MYDIR="/local/mnt/Lists"
eval cd $MYDIR for i in  `ls -d */` ; do (cd $i && for file in *; do [[ -d 2015 ]] ||  mkdir 2015; cd "2015"; [[ -d 01-January ]] ||  mkdir 01-January; cd ".."; dir=$(echo "$i"|sed 's/\/$//g');if [[ -f "$file" ]] && [[ "$file" != M-* && "$file" != Frequent ]]; then echo mv "$file" /netapp/bank/vol0/vertices/Move\ Lists/$dir/extras; fi;done);done

Code:
MYDIR="/local/mnt/Lists"
eval cd $MYDIR
for i in  `ls -d */` ; do (cd $i; echo $i && for file in *; do
[[ -d 2014 ]] || mkdir 2014
cd "2014"
[[ -d 01-January ]] || mkdir 01-January
[[ -d 02-February ]] || mkdir 02-February
[[ -d 03-March ]] || mkdir 03-March
[[ -d 04-April ]] || mkdir 04-April
[[ -d 05-May ]] || mkdir 05-May
[[ -d 06-June ]] || mkdir 06-June
[[ -d 07-July ]] || mkdir 07-July
[[ -d 08-August ]] || mkdir 08-August
[[ -d 09-September ]] || mkdir 09-September
[[ -d 10-October ]] || mkdir 10-October
[[ -d 11-November ]] || mkdir 11-November
[[ -d 12-December ]] || mkdir 12-December
cd ".."
dir=$(echo "$i"|sed 's/\/$//g')
if  [[ "$file" != *20[1-2][0-9]* && "$file" != M-* ]];
then echo mv "$file" /tmp/additions;
fi
done);done

It works just fine - the only problem is that it will try to move all the directories as well as files. I've tried the following to stop this but it hasn't worked:

Code:
dir=$(echo "$i"|sed 's/\/$//g')
if  [[  -f "$file" ]] && [[ "$file" != *20[1-2][0-9]* && "$file" != M-* ]];
then mv "$file" /local/mnt/Lists/$dir

What I get with this is nothing is moved..

If I do not use
Code:
[[ -f $file ]]

then directories are picked up, if I do use it, then nothing is moved.

Does anyone have any ideas?

I think I will try the find command.

Last edited by newbie2010; 08-21-2015 at 11:40 AM..
# 2  
Old 08-21-2015
You are actually excluding anything with a number between 2010-2029.
Code:
..."$file" != *20[1-2][0-9]* ...

hth
# 3  
Old 08-21-2015
What, at the
Code:
 [[ $file !=

point do you intend $file to contain?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX script to find existences of files with a pattern in name

Hi My requirement is as follows. In a particular source directory say /home/Source, I need to search for filename with the following pattern. Bank_YYYYMMDD.txt and if the files with patterns are found them move them to archive directory /home/Source/Archive. The script should not do... (8 Replies)
Discussion started by: b.joe
8 Replies

2. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

3. Shell Programming and Scripting

Help me to find files in a shell script with any matching pattern

Hi friends.. I have many dirs in my working directory. Every dir have thousands of files (.jsp, .java, .xml..., etc). So I am working with an script to find every file recursively within those directories and subdirectories ending with .jsp or .java which contains inside of it, the the pattern... (3 Replies)
Discussion started by: hnux
3 Replies

4. Shell Programming and Scripting

How to find files in a pattern directory?

Hi, I have more than 1000 directories under one directory (lets says under /home/). Sub directories are like A1 to A100,B1 to B100 etc.. Here my problem is I need to find the files older than 10 days in the directories which starts with A*. I tried some thing like this which is not... (2 Replies)
Discussion started by: shhrikanth
2 Replies

5. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

6. Shell Programming and Scripting

To find the files with pattern and display

Hi all, Im looking for a script(bash,perl) to find the files which has content with ORA (Mostly will be from log file)if we find those content,need to send a mail alert to the team members with the files which are matched. This script should run daily so that it should search for the files... (1 Reply)
Discussion started by: preetha_83
1 Replies

7. UNIX for Dummies Questions & Answers

find files NOT matching name pattern

Hi, I have following files in my directory: /TESTDONTDEL> ls -alt total 14 drwxr-xr-x 2 oracle dba 1024 May 15 06:30 . -rw-r--r-- 1 oracle dba 40 May 15 06:30 exception.txt -rw-r--r-- 1 oracle dba 19 May 15 06:22 ful_1234_test1.txt -rw-r--r-- 1... (2 Replies)
Discussion started by: sagarparadkar
2 Replies

8. UNIX for Dummies Questions & Answers

Find files matching a pattern

Hi, I am writing a BASH shell script. I would like to count all the files in the CURRENT directory matching a specific pattern. Could someone suggest the best/simplest way to do this. I have thought of these solutions (for simplicity the pattern is all files starting with A): ls -1 *A | wc -l... (5 Replies)
Discussion started by: msb65
5 Replies

9. Shell Programming and Scripting

How to find files which has more than one occurance of pattern

Hello Everyone, Please help me in finding out the solution. The problem is .. lets say i have 600 files in a directory. All 600 files are shell script files. Now i need to find out the files which contains a pattern "SHELL" more than once. No matter how the pattern occurs , it can be in... (10 Replies)
Discussion started by: Prahlad
10 Replies

10. UNIX for Dummies Questions & Answers

find pattern in FILES and replace it ??

Hi How can I looking for a pattern found in more than one file and replace it with anther pattern this what I was used: find . -name "account.adrs" -depth -follow -exec grep -l "Email = ;" {} \; this print the files name -which is account.adrs- and its path -which is deferent for each... (4 Replies)
Discussion started by: tamer
4 Replies
Login or Register to Ask a Question