finding and moving files based on the last three numerical characters in the filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers finding and moving files based on the last three numerical characters in the filename
# 1  
Old 05-04-2011
finding and moving files based on the last three numerical characters in the filename

Hi, I have a series of files (upwards of 500) the filename format is as follows
CC10-1234P1999.WGS84.p190, all in one directory.
Now the last three numeric characters, in this case 999, can be anything from 001 to 999.
I need to move some of them to a seperate directory, the ones I need to move will be within a defined range, eg I would want to move everything from the file ending in 005 to the file ending in 155.
Any thoughts ?
# 2  
Old 05-05-2011
Best guess would be to use find for this, and matching regex.

Something like :
Code:
find $FINDDIR -name 'CC10-1234P1[01][0-5][0-5].WGS84.p190' -type f | xargs cp -t $DESTDIR

This User Gave Thanks to Peasant For This Post:
# 3  
Old 05-05-2011
For sure there are some more elegant way to do it but it should do the work
Code:
#!/bin/ksh
SOURCEDIR=...
DESTDIR=...
for i in $(seq 1 999 | xargs printf "%03d\n")
do
    f="CC10-1234P1$i.WGS84.p190"
    [[ -f "$SOURCEDIR/$f" ]] && cp -p $SOURCEDIR/$f $DESTDIR/$f
done

This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 05-05-2011
Quote:
Originally Posted by Peasant
Best guess would be to use find for this, and matching regex.

Something like :
Code:
find $FINDDIR -name 'CC10-1234P1[01][0-5][0-5].WGS84.p190' -type f | xargs cp -t $DESTDIR

That one rung a few bells so with a little redesign I came up with (transferring 001-132 this time)

find ./ \( -name "*[0][0-9][0-9].WGS84.p190" -o -name "*[1][3][0-2].WGS84.p190" -o -name "*[1][0-2][0-9].WGS84.p190" \) -exec scp -v {} $DESTDIRp190/ \;


That works a treat, thanks guys, much appreciated
# 5  
Old 05-05-2011
For a one shot move, the find is nice.

Now, if your range change frequently, i may become tedious to re-tweak the pattern matching with expression like those you used (and this also may lead to error or if some case of matching are forgotten).
The code i proposed is easier to adapt if the ranges are frequently changing.
# 6  
Old 05-05-2011
Youre right ctsgnb, its a task I'll have to do quite often so I think something a litle easier to manipulate is required, I'll try using what you suggest or perhaps (if I can get my head working again...) a quick script where I can input the range I want and translate that to the find string. Thanks for the help though mate, much appreciated
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding files with newlines in filename

I want to use grep to find files that have newlines in the filename. For example, I have a directory where I create three files: $ touch file1 $ touch "file 2" $ touch "file > with > newlines" $ find . ./file 2 ./file1 ./file?with?newlinesI now want to pipe the find output into grep and... (4 Replies)
Discussion started by: Ralph
4 Replies

2. UNIX for Dummies Questions & Answers

Finding filename based on filecontent

Hi, I have been trying , to find the filename based on some pattern present inside the file My command is as follows: filename=`grep -l 'Pattern' path/*.txt ` Its strange that it works some times, but doesn't print anything some times . But my if test -f $filename is passing all the... (2 Replies)
Discussion started by: Prashanth19
2 Replies

3. Shell Programming and Scripting

Finding files with wc -l results = 1 then moving the files to another folder

Hi guys can you please help me with a script to find files with one row/1 line of content then move the file to another directory my script below runs but nothing happens to the files....Alternatively Ca I get a script to find the *.csv files with "wc -1" results = 1 then create a list of those... (5 Replies)
Discussion started by: Dj Moi
5 Replies

4. UNIX for Dummies Questions & Answers

Finding files with one row then moving them

Hi guys can you please help me with a script to find files with one row the move the file to another directory the files are in /optima/prd/optdir/ZTE_BSS/Combiner/ZTE_2G/out/BSC_PS_Basic_Meas and I want to move them to /optima/prd/optdir/ZTE_BSS/Loader/error/ZTE_2G/BSC_PS_Basic_Meas ... (5 Replies)
Discussion started by: Dj Moi
5 Replies

5. Shell Programming and Scripting

Finding File Names Ending In 3 Random Numerical Characters

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190 each of this files is in a directory named for the file but excluding the extension. Now the last three numeric characters, in this case 999, can be anything from 001 to 999, I need to... (3 Replies)
Discussion started by: roche.j.mike
3 Replies

6. UNIX for Dummies Questions & Answers

moving/copying files in a numerical order

Hi I am newbie to unix scripting, but i have enough knowledge to understand. I have a specific questions like, I use to collect like 3500 files per experiment, each one named like data_001.img.. data_002.img data_003.img .... data_3500.img I would like to move every 12 files in the 3500... (3 Replies)
Discussion started by: wpat
3 Replies

7. Shell Programming and Scripting

Finding files with filename format

hi all, i'm trying to find out how to show files having a particular format. i.e. files o570345.out o5703451.out XX_570345_1.RTF so when i search for files using ls *570345* it shows all three files but actually i don't like to see the second file o5703451.out because 5703451 is... (6 Replies)
Discussion started by: adshocker
6 Replies

8. UNIX for Dummies Questions & Answers

Moving files out of multiple directories and renaming them in numerical order

Hi, I have 500 directories each with multiple data files inside them. The names are sort of random. For example, one directory has files named e_1.dat, e_5.dat, e_8.dat, etc. I need to move the files to a single directory and rename them all in numerical order, from 1.dat to 1000(or some... (1 Reply)
Discussion started by: renthead720
1 Replies

9. Shell Programming and Scripting

moving files with spaces in filename from one directory to another

Hello, When I run following script #!/bin/bash cd ~/directory1 mv `ls -trF | grep -v / | tail -10 ` ~/directory2 works fine with filenames not having any space but runs into issues with filenames that have spaces tried with $file variable still doesnot work. Can someone help me (4 Replies)
Discussion started by: asakhare
4 Replies

10. UNIX for Dummies Questions & Answers

Moving files by splitting the path embedded in the filename

Hello All. I am having a directory /tmp/rahul which contains many files in the format @#home@#rahul@#programs@#script.pl where /home/rahul/programs is the directory where the script.pl file is to be placed. I have many files in this format. What i want is a script which read these... (7 Replies)
Discussion started by: rahulrathod
7 Replies
Login or Register to Ask a Question