Results 1 to 10 of 10
Hi all,
Here is very interest problem,
i got a file named under dir1
/dir1/a_b_std_e_f.jpg
i want to copy this file to another dir say dir2
/dir2/a1_b1_std_e1_f1.jpg
in the same ...
- 08-06-2007 #1
[SOLVED] How to rename a file servel times and cp to a directory?
Hi all,
Here is very interest problem,
i got a file named under dir1
/dir1/a_b_std_e_f.jpg
i want to copy this file to another dir say dir2
/dir2/a1_b1_std_e1_f1.jpg
in the same manner,
i need to say 10000 file under dir2. named like
ed_we_std_as_add.jpg
rd_ww_std_aa_ss.jpg
need dir2 10000 files with difference file names but same middle name std
How to do this??
Thanks
(the name can change but the important thing will you have that middle part of file name as std)
how to write a script for it.?- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 08-06-2007 #2Just Joined!
- Join Date
- Jan 2007
- Posts
- 90
i guess this one will be helpful (you can improvise and put it in a script )
echo a_b_std_e_f.jpg| sed 's/\(.\)_\(.*\)/\1_ins\2/g'
the \(.\) matches the fist character
and then an _
and then any list of characters
so the output will be a_insb_std_e_f.jpg
(the single quote ' is important )
you can keep adding the matches like
to select 2 matches u can write
\(.\)_\(.\)_\(.*\)
and you can run it through a for loop and keep the for loop variable (say f)
as
sed s/\(.\)_\(.\)_\(.*\)/\1$f\2$f\3/g
which will give what you want
I think this will help you
reference : read on regular expressions, sed or awk
- 08-06-2007 #3
hey bhaslinux..that's really cool.
echo a_b_std_e_f.jpg| sed 's/\(.\)_\(.*\)/\1_ins\2/g'
gives me
a_insb_std_e_f.jpg
and
echo a_b_std_e_f.jpg| sed 's/\(.\)_\(.*\)/\1_ina\2/g'
gives me
a_inab_std_e_f.jpg
i'm not good with shell script. How to use this part to generate say 10 files using script
sed s/\(.\)_\(.\)_\(.*\)/\1$f\2$f\3/g
Thanks.- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 08-06-2007 #4Just Joined!
- Join Date
- Aug 2007
- Posts
- 33
- 08-06-2007 #5Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Perhaps it's better to understand how it works if you do this in 3 steps:
1. Replace "_" with "1_":
2. Replace the "." with "1.":Code:sed 's/_/1_/g'
3. Replace the "std1" with "std":Code:sed 's!\.!1\.!'
Code:sed 's/std1/std/'
To copy the files from /dir1 to /dir2 with a script:Code:$ file="a_b_std_e_f.jpg" $ echo $file|sed -e 's/_/1_/g' -e 's!\.!1\.!' -e 's/std1/std/' $ a1_b1_std_e1_f1.jpg
RegardsCode:#!/usr/bash cd /dir1 for $file in *.jpg do dest="/dir2/"$(echo "$file"|sed -e 's/_/1_/g' -e 's!\.!1\.!' -e 's/std1/std/') cp "$file" "$dest" done
- 08-07-2007 #6
Hi guyz thanks a18u14 and franklin ...
when i ran step by step it works
...when tried to put the code in script it says
`$file': not a valid identifier
Code:#! /usr/bash for $file in *.jpg do dest="/home/oss/dir2/"$(echo "$file"|sed -e 's/_/1_/g' -e 's!\.!1\.!' -e 's/std1/std/') cp "$file" "$dest" done
Last edited by devils casper; 08-07-2007 at 06:42 AM. Reason: Please use code tags.
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 08-07-2007 #7Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Sorry, a typo...
must be:Code:for $file in *.jpg
RegardsCode:for file in *.jpg
- 08-07-2007 #8
when i try,
It said,Code:for file in *.jpg
cp: cannot stat `*.jpg': No such file or directory
so i put single quites
even now it says ,Code:for file in '*.jpg'
cp: cannot stat `*.jpg': No such file or directory
finally i tried ,
it saysCode:for file in .
cp: omitting directory `.'
what to do now?- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 08-07-2007 #9Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Ok, I'm awaking after a couple cups of coffee:
RegardsCode:for file in $(ls *.jpg)
- 08-07-2007 #10
Thanks it works perfectly .
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------



