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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy specific file (different but same name) as folder name
# 1  
Old 01-20-2015
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:

Code:
From /005_0/1000/005.xxx -> /copy/005_1000.xxx
From /005_0/1001/005.xxx -> /copy/005_1001.xxx
From /005_0/1002/005.xxx -> /copy/005_1002.xxx
From /005_0/1003/005.xxx -> /copy/005_1003.xxx
From /005_1/1004/005.xxx -> /copy/005_1004.xxx
From /005_1/1005/005.xxx -> /copy/005_1005.xxx
From /006_0/1006/006.xxx -> /copy/006_1006.xxx
From /006_1/1007/006.xxx -> /copy/006_1007.xxx
From /006_1/1008/006.xxx -> /copy/006_1008.xxx
...

Can someone give me the command to type under RedHat (csh and bash) please? - Many thanks Smilie

Last edited by Don Cragun; 01-20-2015 at 05:00 AM.. Reason: Add CODE tags.
# 2  
Old 01-20-2015
Hello wappor,

Could you please try following and let me know if this helps.

Code:
cat test19.ksh
SOURCE=$1
DESTINATION=$2
awk  -vsource=`echo $SOURCE` -vdestination=`echo $DESTINATION` --re-interval 'BEGIN{match(source,/\/[0-9]{4}\//);A=substr(source,RSTART+1,RLENGTH-2);match(source,/\/[0-9]+\./);B=substr(source,RSTART+1,RLENGTH-2);match(source,/\..*/);C=substr(source,RSTART,RLENGTH);match(destination,/\/[a-zA-Z]+\//);D=substr(destination,RSTART,RLENGTH);print "cp " source " " D B"_" A C}'

We can run it as follows by providing 2 arguments first as source file path and second is destination file path as follows.
Code:
./test19.ksh /005_0/1000/005.xxx  /copy/
cp /005_0/1000/005.xxx /copy/005_1000.xxx

If you are happy with results above you can run following script then which will copy the files as follows.
Code:
cat test19.ksh
SOURCE=$1
DESTINATION=$2
awk  -vsource=`echo $SOURCE` -vdestination=`echo $DESTINATION` --re-interval 'BEGIN{match(source,/\/[0-9]{4}\//);A=substr(source,RSTART+1,RLENGTH-2);match(source,/\/[0-9]+\./);B=substr(source,RSTART+1,RLENGTH-2);match(source,/\..*/);C=substr(source,RSTART,RLENGTH);match(destination,/\/[a-zA-Z]+\//);D=substr(destination,RSTART,RLENGTH);print "cp " source " " D B"_" A C}' | sh

NOTE: Also if you are happy with results then you can read all the files which you want to copy to a destination directory and pass it to script then.


Thanks,
R. Singh
# 3  
Old 01-20-2015
Thanks for your help but unfortunately I have the following with my awk:

Code:
awk: not an option : --re-interval

Smilie

Last edited by Don Cragun; 01-20-2015 at 05:02 AM.. Reason: Add CODE tags again.
# 4  
Old 01-20-2015
Hello wappor,

Kindly use code tags for commands in your posts as per forum rules. Could you please try following and let me know.
Code:
cat test19.ksh
SOURCE=$1
DESTINATION=$2
awk  -vsource=`echo $SOURCE` -vdestination=`echo $DESTINATION` 'BEGIN{match(source,/\/[0-9]+\//);A=substr(source,RSTART+1,RLENGTH-2);match(source,/\/[0-9]+\./);B=substr(source,RSTART+1,RLENGTH-2);match(source,/\..*/);C=substr(source,RSTART,RLENGTH);match(destination,/\/[a-zA-Z]+\//);D=substr(destination,RSTART,RLENGTH);print "cp " source " " D B"_" A C}'

After running the script we will get following results.
Code:
./test19.ksh /005_0/1000/005.xxx  /copy/
cp /005_0/1000/005.xxx /copy/005_1000.xxx


Thanks,
R. Singh
# 5  
Old 01-20-2015
RedHat

Thanks for that again and Sorry about the code tags - I just registered and as everyone, I don't read the terms before clicking "I agree" Smilie

The 'cp ' doesn't appear in the command and could you please tell me more about RSTART and RLENGTH please... What does it take when coded?

Many thanks
# 6  
Old 01-20-2015
Hello wappor,

Could you please let me know what error you are getting while executing it?
Also RSTART and RLENGTH meaning is as follows.
Quote:
RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)
RLENGTH The length of the string matched by match(); -1 if no match.
Thanks,
R. Singh

Last edited by RavinderSingh13; 01-20-2015 at 05:13 AM..
# 7  
Old 01-20-2015
If I'm right, you want to take the second directory in the path and use that as part of the file name in the common target directory.

If you use find to get the list of files and assuming that you can limit the search in some way, (e.g. define source_dirs as a space separated list of directories to search, define name_pattern as an expression, e.g. "*.xxx") you can do the following (in ksh and perhaps bash):-
Code:
for FQfile in $(find $source_dirs -type f -name "$name_pattern")
do
   dirsplit="${FQfile#/*/}"               # Remove first directory from fully qualified file name
   second_dir="${dirsplit%%/*}            # Extract just the next directory from the above
   file_name="${FQfile##*/}"              # Get just file file name part
   f1="${file_name%%.*}"                  # Get the first part of the file name (split at the full-stop)
   f2="${file_name##*.}"                  # Get the last part of the file name (split at the full-stop)
   print "cp -p $FQfile /copy/${f1}_${second_dir}.${f2}"
done

This may run marginally slower than the awk, but hopefully it shows you what is happening and the logic behind it. I'm using "variable substitution" to slice up the fully qualified file name to get the useful parts and then build the output line at the end.

You haven't said how you generate the list of files, so I've gone with a find command. If you have a file with them all listed, you could just change the loop to be:-
Code:
while read FQfile
do
   dirsplit=.......
   :
   :
done < input_file

I hope that this helps and that you can follow the logic.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy one file from a server to a local folder

Hi, Is there a way I can copy a file from a server to a local folder (i.e. My Documents)? can it be done by scp? I tried this but it just rename the file as the folder it has to be transferred at. scp -r name@some_server:/home/user/file.txt 'somehere\home\home_dir' Thanks. (4 Replies)
Discussion started by: erin00
4 Replies

2. Shell Programming and Scripting

Need help in writitng a script to rename file name and copy to other folder

Hi All, My requirement is as follows: A file (say abc) will be having list of the .txt file names. I need to read this abc file line by line and rename the .txt file names inside it and move them to other folder/path. Eg: abc ------- file1.txt file2.txt file3.txt Output (should... (1 Reply)
Discussion started by: pavan.yadalla
1 Replies

3. Programming

how to copy downloaded file into my source file folder (putty/unix)

I need to "Ensure that when you download libchat.a from the VLE you have copied it to the same folder on ius as your source files. You then refer to the library (and the libraries it needs) with: gcc -o outputfile sourcefile.c -L. -lchat -lsocket -lnsl" But I have no idea what this means! (I... (2 Replies)
Discussion started by: fakuse
2 Replies

4. UNIX for Dummies Questions & Answers

copy files grabbing destination folder from file name

Hi all... Below is what I am trying to do: 1. Having the following folder with the files... /source_folder/dodiddone.tar.gz /source_folder/gowentgone.tar.gz /source_folder/gowentgone.log 2. I need to copy and chown files with extension .tar.gz to another folder copy... (1 Reply)
Discussion started by: pedroz
1 Replies

5. UNIX for Dummies Questions & Answers

Copy the latest (last file) in given folder

#!/bin/bash for i in {1..1536..1} do #find /home/test/Desktop/up111/workplace/Malware/$i/logs for a in /home/test/Desktop/up111/workplace/Malware/$i/logs/* do #max=a for b in /home/test/Desktop/up111/workplace/Malware/$i/logs/* do ... (4 Replies)
Discussion started by: upvan111
4 Replies

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

7. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

8. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

9. UNIX for Dummies Questions & Answers

Copy the latest file from a folder

Hi, I have a problem. I have some text files in a folder. The names can be like: emp_20080307053015.dat emp_20080306053015.dat emp_20080305053015.dat emp_20080304053015.dat The date format appended is like yyyymmdd and timestamp. What i need is i have to copy the latest file every... (3 Replies)
Discussion started by: Aswarth
3 Replies

10. Shell Programming and Scripting

How to copy file and locate in new folder?

Hi All, Please advise me how to make a copy of file from a list and store in one particular location? For example , I have aaa.txt which contains as below, But, those *usg files might be randomly store in different location.... > cat aaa.txt adc.usg dfdjkf.usg ugjfk.usg And I want... (3 Replies)
Discussion started by: cedrichiu
3 Replies
Login or Register to Ask a Question