Find the answer to your Linux question:
Results 1 to 8 of 8
I have a single directory of pairs of files, with the pairs sharing a string as the beginning of the filename: SF1-27F1492R-clone01_T3_A18_001.ab1 SF1-27F1492R-clone01_T7_A20_002.ab1 SF1-27F1492R-clone02_T3_A19_003.ab1 SF1-27F1492R-clone02_T7_A21_004.ab1 ...etc I need to create ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Location
    Tokyo
    Posts
    5

    Moving files to directories based on part of filename

    I have a single directory of pairs of files, with the pairs sharing a string as the beginning of the filename:

    SF1-27F1492R-clone01_T3_A18_001.ab1
    SF1-27F1492R-clone01_T7_A20_002.ab1
    SF1-27F1492R-clone02_T3_A19_003.ab1
    SF1-27F1492R-clone02_T7_A21_004.ab1
    ...etc

    I need to create a subdirectory for each pair then move the pair into the subdirectory.

    I accomplished the first step using:

    $find /foo -name '*T3*' -exec mkdir '{}.wrk' \;

    I can use a regex to designate the pair and associate the directory, but how do I use regex in a path as the output of a move command?

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Could you please explain with some example --step by step ,if possible
    - 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
    -------------------

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Location
    Tokyo
    Posts
    5
    Quote Originally Posted by Lakshmipathi View Post
    Could you please explain with some example --step by step ,if possible
    I'll detail what I need first, then how I achieved it.

    1. Create directories (with a subdirectory structure from a template directory) based on the first common part of two filenames. So from this list:

    SF1-27F1492R-clone01_T3_A18_001.ab1
    SF1-27F1492R-clone01_T7_A20_002.ab1
    SF1-27F1492R-clone02_T3_A22_003.ab1
    SF1-27F1492R-clone02_T7_A24_004.ab1
    SF1-27F1492R-clone03_T3_A26_005.ab1
    SF1-27F1492R-clone03_T7_A28_006.ab1

    Create the directories:

    SF1-27F1492R-clone01
    SF1-27F1492R-clone02
    SF1-27F1492R-clone03

    2. Move each pair of files into the corresponding directory (within a subdirectory).

    So
    SF1-27F1492R-clone01_T3_A18_001.ab1
    SF1-27F1492R-clone01_T7_A20_002.ab1

    are both moved into

    SF1-27F1492R-clone01/edit_dir/

    etc.

  4. #4
    Just Joined!
    Join Date
    Jul 2010
    Location
    Tokyo
    Posts
    5
    This is how I achieved it (with my logic)

    1. $ rename 's/\_T3\w{8}/-T3/' *
    (I don't need the end of the filename)

    2. $ find /home/foo/assembly -type f -name '*T3*' -exec cp -a /home/foo/template '{}.wrk' \;
    (To make one directory for each pair of files)

    3. $find /home/foo/assembly -type f -name '*T3*' -exec mv '{}' '{}.wrk/edit_dir/' \;
    (Move one file from each pair into the directory)

    4. $ rename 's/\_T7\w{8}/-T7/' *
    (Same logic as 1.)

    5. $ rename 's/-T3/-T7/' *
    (Renames the directory to match the remaining file)

    6. $ find /home/foo/assembly -type f -name '*T7*' -exec mv '{}' '{}.wrk/edit_dir/' \;
    (Moves the remaining file of the pair into the directory)

    7. $ rename 's/-T7.ab1.wrk//' *
    (Renames the directory to be the prefix of the two files)

    The outcome:

    $ ls SF1-27F1492R-clone01/edit_dir/
    SF1-27F1492R-clone01-T3.ab1 SF1-27F1492R-clone01-T7.ab1

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Code:
    ls  | cut -f1 -d'T' > uniq.txt
    uniq uniq.txt  > create_dir.txt
    while read line; do mkdir $line; done < create_dir.txt
    This should give you directories like
    SF1-27F1492R-clone01_
    SF1-27F1492R-clone02_
    SF1-27F1492R-clone03_

    then do
    Code:
    while read line; do mv $line*.ab1 $line; done < create_dir.txt
    to move files into their directories.

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

  6. #6
    Just Joined!
    Join Date
    Jul 2010
    Location
    Tokyo
    Posts
    5
    My method works, but it felt clumsy because I had to manipulate the name of the directory to match the name of the first file, then add a suffix to the directory, then manipulate the directory name again for the second file.

    Basically, I couldn't use find to mv both files together because find -exec {} didn't allow me to use part of the filename (in this case SF1-27F1492R-clone01) as the output of the mv command.

    In essence, is there an alternative to:

    $find /foo -type f * -exec mv '{}' '{}.suffix/edit_dir' \;

    that will allow me to move the files to a directory with a smaller name than the filenames, but with a common prefix ie move files foo1.suffix foo2.suffix foo3.suffix to directory foo.

  7. #7
    Just Joined!
    Join Date
    Jul 2010
    Location
    Tokyo
    Posts
    5
    Thanks, with a few changes (to cp a template directory instead of mkdir and then mv files into a subdirectory) your method works great.

  8. #8
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    In essence, is there an alternative to:

    $find /foo -type f * -exec mv '{}' '{}.suffix/edit_dir' \;
    As of now , I can't think of an alternative ! I think about it and get back here
    - 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
  •  
...