Copy same file with different variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copy same file with different variables
# 1  
Old 12-19-2012
Copy same file with different variables

Hi All,
I have same file with like 1xyz_1 ,1xyz_2 , 2ghj_1, 2ghj_2, 4wer_1, 4wer1_2 etc with lots of file having similar type of name. I want to creat folder and copy similar file to that folder. for example folder 1xyz should contain only 1xyz_1 ,1xyz_2. Smilie

Thank you in Advance.

regards,
XXLMMN
# 2  
Old 12-19-2012
there is a file 4wer_1 and 4wer1_2. So will you be creating two different folders for these two?
# 3  
Old 12-19-2012
Sorry for my typing mistake that is 4wer_2. I am really sorry.
regards,
# 4  
Old 12-19-2012
Try sth like this..

Code:
ls *_* | while IFS="_" read a b
do
if [[ ! -d "$a" ]]
then
mkdir $a
fi
cp $a"_"$b $a
done

This User Gave Thanks to pamu For This Post:
# 5  
Old 12-19-2012
Not tested..

Code:
 
ls -1 *_* | awk -F_ '{print $1}' | sort -u | while read dir
do
 mkdir -p $dir
 cp $dir_* $dir
done

# 6  
Old 12-19-2012
Pamu,

I'd accept itkamaraj's solution. What do you think?
# 7  
Old 12-19-2012
Quote:
Originally Posted by PikK45
Pamu,

I'd accept itkamaraj's solution. What do you think?
Yes.. That will surely minimizes some time..Smilie
using sort -u and create dir and copy files at a same time.

--
PS- That's why i have written above in my command sth like this..Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change the file name and copy old file content to new file names.

Hi, I have a files in a directory as below :- ls -1 mqdepth-S1STC02 proc-mq-S1STC01 proc-mq-S1STC02 proc-mq-S1STC03 Whereever i have S1STC i need to copy them into new file with file name S2STC. expected output :- ls -1 mqdepth-S2STC02 proc-mq-S2STC01 proc-mq-S2STC02... (3 Replies)
Discussion started by: satishmallidi
3 Replies

2. Shell Programming and Scripting

Copy file and evaluate its internal variables

Hi I have been trying to figure a way to copy a file, (a template), that has internal variables. Using the values as defined for those variables in another script. So a file called x - #! /bin/bash D=aa.$X.bb And file y #! /bin/bash X=6 while read line do eval echo... (5 Replies)
Discussion started by: steadyonabix
5 Replies

3. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

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

5. Red Hat

Copy certain file types recursively while maintaining file structure on destination?

Hi guys, I have just been bothered by a fairly small issue for some time now. I am trying to search (using find -name) for some .jpg files recursively. This is a Redhat environment with bash. I get this job done though I need to copy ALL of them and put them in a separate folder BUT I also... (1 Reply)
Discussion started by: rockf1bull
1 Replies

6. UNIX for Dummies Questions & Answers

How to use sed to copy specific lines from a file using shell variables?

hello! I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines? if the input_file is something like this and if the following is the script a=2 b=4... (4 Replies)
Discussion started by: a_ba
4 Replies

7. UNIX for Dummies Questions & Answers

copy file using a variables

i am having a file name hello.txt i want to move it to some where in the destination folder suppose name is accnt i have done this things but not working , can't we assign a path to a variable #/bin/sh file =hello.txt dest ='accnt' cp $file $dest Thank & Regard Dina (3 Replies)
Discussion started by: Manabhanjan
3 Replies

8. UNIX for Dummies Questions & Answers

Copy a windows CVS file to the unix server as a svs file

I so desperately need a script to copy a windows csv file to my unix server and i know these should be at dummies but i have no bits. it is life & no job situation help please. thanks (1 Reply)
Discussion started by: zhegal
1 Replies

9. 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
Login or Register to Ask a Question