Inserting text in file names while copying them.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting text in file names while copying them.
# 1  
Old 06-20-2013
Linux Inserting text in file names while copying them.

I'm trying to find a Bourne shell script that will copy files from one directory using a wild card for the file name (*) and add some more characters in the middle of the file name as it is copied. As an example:

/u01/tmp-file1.xml => /u02/tmp-file1-20130620.xml
/u01/tmp-file2.xml => /u02/tmp-file2-20130620.xml
/u01/tmp-file3.xml => /u02/tmp-file3-20130620.xml
/u01/tmp-file4.xml => /u02/tmp-file4-20130620.xml

I've seen scripts where you can append characters to the end of a file or to the front of a file, but not in the middle of the file name. I was hoping for something as easy as:
Code:
cp /u01/tmp-file*.xml /u02/tmp-file*-20130620.xml

but of course no such thing exists.
# 2  
Old 06-20-2013
You could use a for loop with parameter substitution:
Code:
#!/bin/bash

DT=$( date +"%Y%m%d" )

for file in /u01/tmp*.xml
do
        fname="${file##*/}"
        fname="${fname%.xml}"
        fname="${fname}-${DT}.xml"
        echo cp $file /u02/$fname
done

Remove the highlighted echo if output looks good.

Last edited by Yoda; 06-20-2013 at 07:31 PM.. Reason: corrected destination path
# 3  
Old 06-20-2013
Code:
 
#!/bin/bash

DT=$( date +"%Y%m%d" )

for file in /u01/tmp*.xml
do
        fname="${file##*/}"
        fname="${fname%.xml}"
        fname="${fname}-${DT}.xml"
        echo cp $file /u02/$fname
done

Hello Yoda,

Could you please explain the code please it will be more useful for us.



Thanks,
R. Singh
# 4  
Old 06-20-2013
Check Parameter Expansion to better understand the syntax:
Code:
DT=$( date +"%Y%m%d" )

# for each file in /u01/tmp*.xml
for file in /u01/tmp*.xml
do
        # Remove every character before the last /
        fname="${file##*/}"
        # Remove .xml
        fname="${fname%.xml}"
        # Construct new file name
        fname="${fname}-${DT}.xml"
        echo cp $file /u02/$fname
done

This User Gave Thanks to Yoda For This Post:
# 5  
Old 06-21-2013
Looking for more generality

This is great if I know what the suffix is. I was looking for more generality by being able to replace the wild card with some text and leave the rest of the file name as is. So let's call the new function cpr (copy with replace). With the same file set:
Code:
/u01/tmp-file1.xml => /u02/tmp-file1-20130620.xml
/u01/tmp-file2.xml => /u02/tmp-file2-20130620.xml
/u01/tmp-file3.xml => /u02/tmp-file3-20130620.xml
/u01/tmp-file4.xml => /u02/tmp-file4-20130620.xml

I should be able to:
Code:
cpr /u01/tmp*.xml /u02/tmp*-20130620.xml

or in a more generalized:
Code:
cpr [dir1]string1*string2 [dir2]string1*string3

so that every matching string in parameter1 is duplicated in parameter2 on the output, but string3 is inserted in the name. So the command might be:
Code:
cpr /u01/tmp*.xml /u02/tmp*-20130620.txt

and the result would be:
Code:
/u01/tmp-file1.xml => /u02/tmp-file1-20130620.txt
/u01/tmp-file2.xml => /u02/tmp-file2-20130620.txt
/u01/tmp-file3.xml => /u02/tmp-file3-20130620.txt
/u01/tmp-file4.xml => /u02/tmp-file4-20130620.txt

After many tries at parameter expansion I can see why the cp command doesn't allow this.

Last edited by Franklin52; 06-21-2013 at 09:32 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 06-21-2013
Hi.

I've used mved at mved a few times.

I have not looked at your problem in detail to see if mved would solve it, but it might be worth a glance.

Best wishes ... cheers, drl
# 7  
Old 06-21-2013
Or switch to zsh Smilie
Code:
zsh-5.0.2[t]% autoload -U zmv
zsh-5.0.2[t]% ls -l ./u01
totale 0
-rw-r--r-- 1 Master None 0 21 giu 14.35 tmp-file1.xml
-rw-r--r-- 1 Master None 0 21 giu 14.35 tmp-file2.xml
-rw-r--r-- 1 Master None 0 21 giu 14.35 tmp-file3.xml
-rw-r--r-- 1 Master None 0 21 giu 14.35 tmp-file4.xml
zsh-5.0.2[t]% zmv -nv -- './u01/tmp(*).xml' './u02/tmp$1-20130620.txt'
mv -- ./u01/tmp-file1.xml ./u02/tmp-file1-20130620.txt
mv -- ./u01/tmp-file2.xml ./u02/tmp-file2-20130620.txt
mv -- ./u01/tmp-file3.xml ./u02/tmp-file3-20130620.txt
mv -- ./u01/tmp-file4.xml ./u02/tmp-file4-20130620.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Inserting text into a file with awk or sed

Hello, I've been trying to get a script working that fetches weather-data and converts it into an .ics file. The script works so far put I'm stuck at the point where I need to add specific static data. A thorough search through the forum did not point me into the right direction. #!/bin/bash... (3 Replies)
Discussion started by: Schubi
3 Replies

2. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

3. Shell Programming and Scripting

Inserting text into a new file

Hi all, I want to create a file and then insert some text into it. I'm trying to create a .sh script that will create a new python file from a template. Can someone tell me why this won't work, touch $1 | sed -e '1i\Some test code here' Sorry I'm quite new to all this! Just as a side... (3 Replies)
Discussion started by: ahodgson
3 Replies

4. Shell Programming and Scripting

Copying List of Files Under Different File Names ?!

Hi to all here , Excuse me for the lamer question but I need UNIX command for copying List of Files Under Different File Names ! for example I have this list: new_001.jpg new_002.jpg new_003.jpg ..... I want to copy all files that start with "new_" to the same directory but under... (7 Replies)
Discussion started by: boboweb_
7 Replies

5. UNIX for Dummies Questions & Answers

copying same file multiple times with different names

hi, I am copying a file from 1 folder to another in /bin/sh. if the file already exists there, it should get copied as filename1. again if copying next time it shouldget copied as filename2.. , filename3..so on.. The problem is i am able to get uptil filename1.. but how do i know what... (6 Replies)
Discussion started by: blackcat
6 Replies

6. UNIX for Dummies Questions & Answers

Inserting a column into a text file

I have a tab delimited text file with multiple columns (data.txt). I would like to insert a column into the text file. The column I want to insert is in a text file (column.txt). I want to insert it into the 5th column of data.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

7. Shell Programming and Scripting

inserting a string to a text file

Hello Can somebody please help me with the following script? I'm trying to create a text file with 20 blank lines and then insert a string in line 2 but nothing is printed in the itxtfile. I can create the file with 20 blank lines but when I "tell" it to print something on the second line, it... (4 Replies)
Discussion started by: goude
4 Replies

8. Shell Programming and Scripting

Inserting text and modifying the file

I am in a dire need of doing this job , please help from shell script or perl script. It will be highly appreciated. Please have a look at the following INPUT file; The first 14 rows are not of interest but I want them to be included in the output file as they are. From the row 14... (3 Replies)
Discussion started by: digipak
3 Replies

9. Shell Programming and Scripting

inserting subscriber no in text file using KSH....

:confused:Dears , I have text file I need to insert the subscriber number at position 32, and need to keep the next field at position 53 (no increasing of the record lenght), I mean I just want to replace the spaces at position 32 with subscirber number . for example A B A ... (1 Reply)
Discussion started by: atiato
1 Replies

10. UNIX for Dummies Questions & Answers

Copying file names returned from a grep command into another directory

When I do the following : grep -l "string" *, I get a list of file names returned. Is there a way to copy the files returned from the list into another directory ?. Thanks. (4 Replies)
Discussion started by: Kartheg
4 Replies
Login or Register to Ask a Question