Visit Our UNIX and Linux User Community


Copy files help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files help needed
# 1  
Old 02-26-2008
Copy files help needed

Guys,
I am new to scripting. I need help in the following

I have some files like

ls -1
file_5001_1.payor.txt
file_5001_1.addr.txt

I basically need to copy to another files with same format except replacing 5001 with 5006.

output
ls -1
file_5001_1.payor.txt
file_5001_1.addr.txt
file_5006_1.payor.txt
file_5006_1.addr.txt

Please help!
jak
# 2  
Old 02-26-2008
Here's one way:

Code:
for i in *.txt
do
    cp ${i} `echo ${i} | sed 's/5001/5006/g'`
done

# 3  
Old 02-26-2008
Thanks for your help! I will try it out

jak
# 4  
Old 02-26-2008
An alternative solution, this time using just the shell, without the help of external programs (except by the cp program, of course):

PHP Code:
for file in *5001*; do cp $file ${file/01/06};done 
the key here is the ${//} shell's parameter expansion operator:

extracted from GNU Bash man page:

Quote:
${parameter/pattern/string}
${parameter//pattern/string}
The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is
replaced with string. In the first form, only the first match is replaced. The second form causes all matches of pattern to be replaced with string.
If pattern begins with #, it must match at the beginning of the expanded value of parameter. If pattern begins with %, it must match at the end of the
expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is @ or *, the
substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable sub-
scripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
this operator is available on the Korn Shell to.

Previous Thread | Next Thread
Test Your Knowledge in Computers #853
Difficulty: Medium
AngularJS most notable feature is the two-way data binding.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy files from one drive to another, keeping most recently modified files

Hi all, I am a bit of a beginner with shell scripting.. What I want to do is merge two drives, for example moving all data from X to Y. If a file in X doesn't exist in Y, it will be moved there. If a file in X also exists in Y, the most recently modified file will be moved to (or kept) in... (5 Replies)
Discussion started by: apocolapse
5 Replies

2. Red Hat

Unable to copy files due to many files in directory

I have directory that has some billion file inside , i tried copy some files for specific date but it's always did not respond for long time and did not give any result.. i tried everything with find command and also with xargs.. even this command find . -mtime -2 -print | xargs ls -d did not... (2 Replies)
Discussion started by: before4
2 Replies

3. Shell Programming and Scripting

how to copy files followed by list of names of all the files in /etc?

....... (2 Replies)
Discussion started by: pcbuilder
2 Replies

4. Solaris

How to safely copy full filesystems with large files (10Gb files)

Hello everyone. Need some help copying a filesystem. The situation is this: I have an oracle DB mounted on /u01 and need to copy it to /u02. /u01 is 500 Gb and /u02 is 300 Gb. The size used on /u01 is 187 Gb. This is running on solaris 9 and both filesystems are UFS. I have tried to do it using:... (14 Replies)
Discussion started by: dragonov7
14 Replies

5. Solaris

Solaris 6.5 MOD disk copy help needed

Hello, we are running Irix 6.5 on our octane/sgi computers - these computers come with an external Sony MOD drive attached via a scsi cable. We have backed info to 2.3 gig MOD disks over the years and woule like to duplicate the MOD's. I believe there are 3 ways to do this: add a second... (1 Reply)
Discussion started by: drew_holm
1 Replies

6. UNIX for Dummies Questions & Answers

Help needed to backup files.

Hi guys , I m writing a script which will backup a particular folder and its content to a different location. this script needs to be run every weekend. But my problem is how would i apply logic such that the previous backup folder is only deleted if and only if the current backup is... (1 Reply)
Discussion started by: pinga123
1 Replies

7. Shell Programming and Scripting

Help needed for deleting the files.

Hi guys i m trying to delete some files from one of the share which is mounted on my machine. but whenever i do that it says access denied. I m logged in as a root for my machine. below is the output for those two file. mount command for mounting the shares. mount... (1 Reply)
Discussion started by: pinga123
1 Replies

8. Shell Programming and Scripting

help needed in grep and copy

need help in a command. My requirement is that i m grepping the filename based on a pattern in .tar.gz file and then copying the file into some dir below is what exactly i want to achieve step 1: grep -l '30017A6800022D1A' CurrentCollectorMeterReadBackup20081007.tar.gz step 2: file found... (2 Replies)
Discussion started by: ali560045
2 Replies

9. UNIX for Dummies Questions & Answers

Help needed in deleting the files

Hi, I wanted to check whether any files are available under the path /usr/app/logs/. If the files are available under this path then i have to delete all the files except the files system.log, system.log.1, system.log.2 and sub folders under this path Can any one please help me on this? ... (2 Replies)
Discussion started by: Sheethal
2 Replies

10. UNIX for Dummies Questions & Answers

Help needed in Listing Files

I need to list files in alphabetical-group and each such group listed in increasing order of timestamp. Example : If I list files in a directory ( ls -lrt ) it shows like below. c3 b4 b3 a4 a3 c2 b2 c1 b1 a2 a1 What I need is c3 c2 (4 Replies)
Discussion started by: coolbhai
4 Replies

Featured Tech Videos