#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    113
    Rep Power
    11

    Copy files grabbing destination folder from file name


    Hi all...

    Below is what I am trying to do:
    1. Having the following folder with the files...
    /source_folder/dodiddone.tar.gz
    /source_folder/gowentgone.tar.gz
    /source_folder/gowentgone.log

    2. I need to copy and chown files with extension .tar.gz to another folder
    copy /source_folder/dodiddone.tar.gz /dodiddone/destiny_folder/dodiddone.tar.gz
    copy /source_folder/gowentgone.tar.gz /gowentgone/destiny_folder/gowentgone.tar.gz

    3. code
    #!/bin/sh
    SOURCE="/source_folder"
    EXTENSION="tar.gz"

    for i in "$SOURCE"/*.$EXTENSION
    do
    copy $i /(???-1-)/(???-1-).$EXTENSION
    chown /(???-1-)/(???-1-).$EXTENSION
    done

    4. my doubts!
    (???-1-) : here would be something like ${i%%.$EXTENSION} |sed 's#^.*/##' to grab the file name

    ***
    Could you please help to solve this problem?

    Many thanks!!!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481

    bash


    This works with bash. And with other shells too. Remove echo when you are satisfied the commands are correct.
    Code:
    #!/bin/sh
    SOURCE="/source_folder"
    EXTENSION="tar.gz"
    
    destinationPath=/dodiddone/destiny_folder/
    
    for i in "$SOURCE"/*.$EXTENSION
    do
      destination="$destinationPath"/"${i##*/}"
      echo copy "$i" "$destination"
      echo chown "$destination"
    done

IMN logo majestic logo threadwatch logo seochat tools logo