Find the answer to your Linux question:
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 ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Thumbs up [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
    -------------------

  2. #2
    Just 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

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

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

  4. #4
    Just Joined!
    Join Date
    Aug 2007
    Posts
    33
    Quote Originally Posted by Lakshmipathi View Post
    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.?
    try this one

    #to create the files, u can do ur own R&D with it
    for list in `seq 10`;do touch $list-std-1.jpg;done
    cp -rv * /path/to/dir2

    u can put this in the script & run it

  5. #5
    Linux 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_":

    Code:
    sed 's/_/1_/g'
    2. Replace the "." with "1.":

    Code:
    sed 's!\.!1\.!'
    3. Replace the "std1" with "std":

    Code:
    sed 's/std1/std/'
    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
    To copy the files from /dir1 to /dir2 with a script:

    Code:
    #!/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
    Regards

  6. #6
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    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
    -------------------

  7. #7
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Sorry, a typo...

    Code:
    for $file in *.jpg
    must be:

    Code:
    for file in *.jpg
    Regards

  8. #8
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    when i try,
    Code:
    for file in *.jpg
    It said,
    cp: cannot stat `*.jpg': No such file or directory
    so i put single quites
    Code:
    for file in '*.jpg'
    even now it says ,
    cp: cannot stat `*.jpg': No such file or directory
    finally i tried ,
    Code:
    for file in .
    it says
    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
    -------------------

  9. #9
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Ok, I'm awaking after a couple cups of coffee:

    Code:
    for file in $(ls *.jpg)
    Regards

  10. #10
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...