How to copy specific files when you don't know the file name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy specific files when you don't know the file name?
# 1  
Old 12-10-2010
How to copy specific files when you don't know the file name?

I hope this isn't as silly as it sounds from the title of the thread.

I have software that outputs files where the name starts with a real number followed by underscore as a prefix to an input file name. These will list in the directory with the file with the smallest real number prefix as the second file in the directory (there is another file that starts with 00).

Example directory,
Code:
$ ls
00_f0_reference_S5_v36.20.1_2.txt
38.01_E300_41.36_E200_3_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
38.74_E800_44.24_E1500_1_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
41.22_E100_43.75_E300_0_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
44.06_E1400_56.04_E200_2_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
51.91_E2200_49.86_E900_4_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt

I need a way to copy just the second and third file from a bash script, but, of course, I don't know exactly what the file names will be since they will be different every time the software runs.

In this case I need to identify the files,
Code:
38.01_E300_41.36_E200_3_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
38.74_E800_44.24_E1500_1_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt

and copy them to a different directory.

I suppose there is a way to read in all the file names and grab the real number into a variable, sort the variables, and then I would know the number that all the file names start with in order of the real, but I really don't know how to do that sort of thing. If there is a simple way to create a list (like [ ] I think) and then just be able to copy the first three files in the list, that would work fine. I can always delete the 00_ file, even though that is not a very slick solution.

Thanks for the help,

LMHmedchem

Last edited by radoulov; 12-10-2010 at 12:49 PM.. Reason: Code tags, please!
# 2  
Old 12-10-2010
Is it as easy as:

Code:
root@ms:/tmp/r>ls -la
total 96K
drwxr-x---   2 root system 4.0K 10 Dec 2010 10:39 .
drwxrwxrwt  29 bin  bin     84K 10 Dec 2010 10:40 ..
-rw-r-----   1 root system    0 10 Dec 2010 10:38 00_f0_reference_S5_v36.20.1_2.txt
-rw-r-----   1 root system    0 10 Dec 2010 10:38 38.01_E300_41.36_E200_3_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
-rw-r-----   1 root system    0 10 Dec 2010 10:38 38.74_E800_44.24_E1500_1_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
-rw-r-----   1 root system    0 10 Dec 2010 10:39 41.22_E100_43.75_E300_0_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
-rw-r-----   1 root system    0 10 Dec 2010 10:39 44.06_E1400_56.04_E200_2_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
-rw-r-----   1 root system    0 10 Dec 2010 10:39 51.91_E2200_49.86_E900_4_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
root@ms:/tmp/r>ls -1 | head -3 | tail -2
38.01_E300_41.36_E200_3_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
38.74_E800_44.24_E1500_1_ri_OA_f0_S5e_v36-1_ON_0.25lr_36.20.1.in.txt
root@ms:/tmp/r>

You're looking for the two lowest numbered files right?
# 3  
Old 12-10-2010
See if this works:
Code:
(set -- *; cp -p "$2" "$3" /path/to/newdir)

# 4  
Old 12-12-2010
Thanks for the replies, I will give both of these a try. I can't believe I haven't been able to get back to this before now.

LMHmedchem
# 5  
Old 04-18-2011
Quote:
Originally Posted by Scrutinizer
See if this works:
Code:
(set -- *; cp -p "$2" "$3" /path/to/newdir)

This works just fine.

Now I need to read in the file names and capture some of the file name to a bash variable.

In the filename,
41.15_E2400_49.62_E200_2_ri_OA_f0_S4-A_37.20.1_1_ON_0.25lr_37.20.1.out.txt

I need to grab the E2400, E200, and the value 2 (_2_). I will start looking this up, but if someone knows off hand what to do, it would save allot of time. This kind of looks like a job for a tokenizer, if "_" can be used as the delimiter.

---------- Post updated 04-18-11 at 12:16 AM ---------- Previous update was 04-17-11 at 08:14 PM ----------

I have this,
Code:
for INFILE in `ls *ri.txt`
do
   EPOCH=`echo $INFILE | awk 'BEGIN {FS="_"} {print $2}' | sed 's/E//g'`
   RNDINI=`echo $INFILE | awk 'BEGIN {FS="_"} {print $5}'`
   EPOCH1=$(( EPOCH - 100 ))
   echo $EPOCH
   echo $EPOCH1
   echo $RNDINI
done

which read the file names in a directory,
41.15_E2400_49.62_E200_2_ri.txt
42.38_E1600_52.12_E200_12_ri.txt

grabs the values I need and assigns them to some shell vars.
2
2400
2300

12
1600
1500

I don't know if the sed command there is the best way to get rid of the "E" or not. I also don't know if both vars can be assigned from a single awk command. I would be interested in knowing the perl version of what is above.

LMHmedchem
# 6  
Old 04-18-2011
If the file names have the same format then you can use cut or awk

Code:
echo "41.15_E2400_49.62_E200_2_ri.txt" | cut -d"_" -f2 |  tr -cd '[[:digit:]]'   #gives you 2400
echo "41.15_E2400_49.62_E200_2_ri.txt" | cut -d"_" -f4 | tr -cd '[[:digit:]]'   #gives you 200
echo "41.15_E2400_49.62_E200_2_ri.txt" | cut -d"_" -f5                             #gives you 2

regards,
Ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy Specific Files Recursively

Is it possible to only copy selected files+its directories when you are copying recursively? find /OriginalFolder/* -type -d \{ -mtime 1 -o -mtime 2 \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \; -print From the above line, I want to only copy *txt and *ini files from /OriginalFolder/* ... (4 Replies)
Discussion started by: apacheLinux
4 Replies

2. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

3. Shell Programming and Scripting

Rsync to copy specific subfolders and files to new directory

RootFolderI: RootFolderI/FolderA/Subfolder1/Subsub1/JPG1.jpg -> want this jpg RootFolderI/FolderA/Subfolder2/Subsub1/JPG2.jpg -> want this jpg RootFolderI/FolderA/Subfolder2/Subsub2/JPG3.jpg . . . RootFolderI/FolderB/Subfolder1/Subsub1/JPG4.jpg -> want this jpg ... (1 Reply)
Discussion started by: blocnt
1 Replies

4. Shell Programming and Scripting

Copy specific file (different but same name) as folder name

I have to copy a particular file present in a main folder having part of the file-name present in many sub-folders to a new destination preserving the name of the source "part of the main folder" and previous file-name of the output file: Example: From /005_0/1000/005.xxx ->... (7 Replies)
Discussion started by: wappor
7 Replies

5. Shell Programming and Scripting

How to copy a directory without specific files?

Hi I need to copy a huge directory with thousands of files onto another directory but without *.WMV files (and without *.wmv - perhaps we need to use *.). Pls advise how can I do that. Thanks (17 Replies)
Discussion started by: reddyr
17 Replies

6. Shell Programming and Scripting

copy specific files and count them - not as easy as it seems!

Hi all: Here's my dilemma: to identify files of a specific type, copy them to a new location while preserving the original file attributes (date, time, full path, etc), and at the same time capture the count of the number of files identified as a variable for later reporting. Here's where I... (9 Replies)
Discussion started by: Yamaha Evans
9 Replies

7. Shell Programming and Scripting

How to copy specific file.txt in specific folder?

hye there... i have a problem to copy file in specific folder that will change the name according to host,time(%m%s) and date(%Y%M%D) example folder name: host_20100531.154101801 this folder name will always change... but i just want to copy the AAA.txt and BBB.txt file.. really need... (17 Replies)
Discussion started by: annetote
17 Replies

8. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies

9. Shell Programming and Scripting

Recursively copy only specific files from a directory tree

Hi I am a shell-script newbie and am looking to synchronize certain files in two directory structures. Both these directory-trees are in CVS and so I dont want the CVS directory to be copied over. I want only .sh and .pl files in each subdirectory under these directory trees to be... (3 Replies)
Discussion started by: sharpsharkrocks
3 Replies

10. Solaris

To copy the files newer than specific date

Dear all, Can you help me in copying files newer than speciifc date Thanks in advance, Rajesh (3 Replies)
Discussion started by: RAJESHKANNA
3 Replies
Login or Register to Ask a Question