Shell script to copy file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to copy file
# 1  
Old 02-14-2011
Shell script to copy file

Dear all,
I have a database with thousands of files with the structure of name is:
Filename_hour_year.abc
Filename_hour_year_1.abc
..............
So what I need is how to write a script that all file with contain the character "_1" will copy to "_2"
For example: file name: abc_111111_2011_1.abc will be copied to abc_111111_2011_2.abc
Thank all!!!
# 2  
Old 02-14-2011
PHP Code:
for filename in `ls *_1*`;
do
    
newname=`echo $filename | sed -e 's/_1/_2/g'`
    
mv $filename $newname
done 
# 3  
Old 02-14-2011
Thank Amitranjansahu so much,
It works perfectly...!!
# 4  
Old 02-14-2011
Quote:
Originally Posted by hainguyen1402
Thank Amitranjansahu so much,
It works perfectly...!!
I don't think so.
Code:
echo 'abc_111111_2011_1.abc' | sed -e 's/_1/_2/g'

gives as output:
Code:
abc_211111_2011_2.abc

instead of:
Code:
abc_111111_2011_2.abc

Try:
Code:
for file in *_1.abc
do
    newname=${file%_*}_2.abc
    mv $file $newname
done


Last edited by Franklin52; 02-14-2011 at 06:17 AM.. Reason: typo
# 5  
Old 03-01-2011
Sorry all about my unclear question that I gave.
My problem is copy all files file name have character "_1." to "_2." don't care about extension.
For example: I have files:
ads_daily_10_2008_1.MYD
ads_daily_10_2008_1.MYI
ads_daily_10_2008_1.frm
I need script that can copy these files to new files like:
ads_daily_10_2008_2.MYD
ads_daily_10_2008_2.MYI
ads_daily_10_2008_2.frm
Thank in advance.!

HaiNguyen
# 6  
Old 03-01-2011
Code:
 echo "ads_daily_10_2008_1.MYD
ads_daily_10_2008_1.MYI
ads_daily_10_2008_1.frm" |awk '{print "cp "$0" "gensub("_1","_2",$0)}' |sh

# 7  
Old 03-01-2011
Quote:
Originally Posted by hainguyen1402
Sorry all about my unclear question that I gave.
My problem is copy all files file name have character "_1." to "_2." don't care about extension.
For example: I have files:
ads_daily_10_2008_1.MYD
ads_daily_10_2008_1.MYI
ads_daily_10_2008_1.frm
I need script that can copy these files to new files like:
ads_daily_10_2008_2.MYD
ads_daily_10_2008_2.MYI
ads_daily_10_2008_2.frm
Thank in advance.!

HaiNguyen
What is wrong with the solution provided in post# 2? It's almost close to your requirement. With some few modifications however..
Code:
for filename in `ls *_1.*`;
do
    newname=`echo $filename | sed 's/_1\./_2./'`
    cp $filename $newname
done


Last edited by michaelrozar17; 03-01-2011 at 04:10 AM.. Reason: removed -e in sed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script which will check the target file and folder exists and copy it

Hi All, I am a beginner in this and trying to write a shell script in linux which will : 1. Ask for a file name and check if its exists. 2. If file exists only then it will ask for the new target folder, after entering target folder name it will check if it exists. 3. If target folder... (3 Replies)
Discussion started by: ashish_neekhra
3 Replies

2. UNIX for Dummies Questions & Answers

Download .xls file from mail and copy to another path using shell script

Hi I have a mail attachment coming from a mail id and evreytime with the same name in .xls format.I have to download the .xls file into a location and convert it itno .csv format and copy the .csv file to another location. (1 Reply)
Discussion started by: bikky6
1 Replies

3. Shell Programming and Scripting

Shell script to copy particular file from directories recursively

I have directory path in which there are several sub directories. In all these sub dir there will be one env.cnf file. I want to copy this env.cnf file from each sub dir's and place them in destination path by creating same filename as sub dir_env.cnf. After copying env.cnf files from source... (4 Replies)
Discussion started by: Optimus81
4 Replies

4. Shell Programming and Scripting

Shell Script Problems, Lose formatting when copy pasting from formatted file.

Hello, I'm having trouble with formatting some text via the terminal. I can get it perfectly formatted, but when I try and copy paste the text from the output file it loses it's formatting. Very frustrating! Basically I have 7 files (data data2 data3 data4 data5 data6 data7) containing a... (13 Replies)
Discussion started by: facetoe
13 Replies

5. Shell Programming and Scripting

Shell script to copy a log file if it exceeds 5000000 bytes

Hi, on unix box, under /local/home/userid/logs folder, apps generated the following files sw_warn.log sw_error.log eaijava.log if there any application specific errors, the above file will keep growing. if the file exceed 5000000 bytes, I would like to have a shell script which do... (6 Replies)
Discussion started by: lookinginfo
6 Replies

6. Shell Programming and Scripting

Shell script for searching a record,copy to a file and then delete it

Hi, I have a requirement in hand: I have a file with millions of records say file 1.I have another file, say file 2 which has 2000 records in it. The requirement is to read file2 , and remove the read record from file 1 and move i to a seperate file, file 3. For eg: Read file 2, get the... (5 Replies)
Discussion started by: kumara2010
5 Replies

7. Shell Programming and Scripting

Solaris KSH shell script to copy all lines from one file to another

Hello, more of a windows wscript guy. However I took a new position that requires me to support some solaris servers. So... issue is that I need to copy all lines from a file to a temporary file and then copy them back into the original file starting at line 1. Reason I need to do this is... (5 Replies)
Discussion started by: ZigZaggin
5 Replies

8. Shell Programming and Scripting

Shell Script - Copy File at intervals

Hi, I want to copy some files from a Folder say, /usr/X at random intervals to another location. Basically, new files will be dumped at random intervals to location /usr/X and I have to copy those new files to some other location (after copying, I cannot delete those files from source... (2 Replies)
Discussion started by: angshuman_ag
2 Replies

9. Shell Programming and Scripting

Help with a shell script to modify one line and copy the next 9 to same file

Hi everyone, the problem is quite simple, yet I can't find an easy solution using awk. I need to search for a string in $3, then if I find this string, copy the line,modify $3, and copy the next 9 lines to the same file. My problem is in the copying of the lines... Finding and modifying... (5 Replies)
Discussion started by: Teroc
5 Replies

10. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies
Login or Register to Ask a Question