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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers UNIX script to find existences of files with a pattern in name
# 1  
Old 03-04-2014
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 anything if no file is found.

Thanks.
# 2  
Old 03-04-2014
Hi, what have you tried so far?
# 3  
Old 03-04-2014
Here is what I have done.
Code:
#Script starts
#!/bin/ksh
SOURCE_DIR=/home/infa_usr/bjoe
ARCHINVE_DIR=${SOURCE_DIR}/Archive
for FILENAME in `ls $SOURCE_DIR/Bank\_????????.txt`
do
        echo "Archving File $FILENAME"
        mv $FILENAME $ARCHINVE_DIR
done
#Script ends

Code:
/home/infa_usr/bjoe->ls -l Bank*
-rw-r--r--    1 infa_usr informat          0 Mar  4 12:44 Bank_20110102.txt
-rw-r--r--    1 infa_usr informat          0 Mar  4 12:44 Bank_20140101.txt
/home/infa_usr/bjoe->./test.ksh
Archving File /home/infa_usr/bjoe/Bank_20110102.txt
Archving File /home/infa_usr/bjoe/Bank_20140101.txt
/home/infa_usr/bjoe->./test.ksh
/home/infa_usr/bjoe/Bank_????????.txt not found

If the files are found then scripts runs fine, but if there are no files matching pattern, then I get the "not found" message. Is there a way to handle this so that this message is not displayed on the terminal.

Thanks.

Last edited by Scrutinizer; 03-04-2014 at 02:01 PM.. Reason: code tags
# 4  
Old 03-04-2014
Try:
Code:
cd "$SOURCE_DIR"
for FILENAME in Bank_????????.txt
do
  if [ -f "$FILENAME" ]; then
    # mv operations
  fi
done

# 5  
Old 03-04-2014
Scrutinizer,
The code you provided bellow does work perfectly. Thanks for the code.

Can we extend it further that we pass the pattern "Bank\_????????.txt" (or any generic pattern) from command line. I tried to change
Bank_????????.txt
to
$1
or
"$1"
and then passed the argument as Bank_????????.txt to the script, but that does not work.

Thanks.
# 6  
Old 03-04-2014
Yes you could try:
Code:
pattern=$1
cd "$SOURCE_DIR"
for FILENAME in $pattern
do
  if [ -f "$FILENAME" ]; then
    # mv operations
  fi
done

And call with:
Code:
./script.sh "Bank_????????.txt"

The quotes around the pattern are important.


--or--
Code:
cd "$SOURCE_DIR"
for FILENAME
do
  if [ -f "$FILENAME" ]; then
    # mv operations
  fi
done

And call with:
Code:
./script.sh Bank_????????.txt


Last edited by Scrutinizer; 03-04-2014 at 05:20 PM..
# 7  
Old 03-04-2014
Code:
#Script starts
#!/bin/ksh
SOURCE_DIR=/home/infa_usr/bjoe
ARCHINVE_DIR=${SOURCE_DIR}/Archive
.
.
.

Note that the shebang should be the very first line in your script.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

2. UNIX for Beginners Questions & Answers

How to zip csv files having specific pattern in a directory using UNIX shell script?

I have files in a Linux directory . Some of the file is listed below -rw-rw-r--. 1 roots roots 0 Dec 23 02:17 zzz_123_00000_A_1.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_121_00000_A_2.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_124_00000_A_3.csv drwxrwxr-x. 2 roots roots 6 Dec 23... (4 Replies)
Discussion started by: Balraj
4 Replies

3. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question