Renaming the files with prepending parent directory name [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












Need help in mass renaming files that prepending their parent directory name to them without using rename command.



e.g.



/tmp/2017-09-22/cyber.gz
/tmp/2017-09-23/cyber.gz
/tmp/2017-09-24/cyber.tar


Also renamed files has to be copy in /tmp/archive without impacting above original files.



Looks like below



/tmp/archive/2017-09-22_cyber.gz
/tmp/archive/2017-09-23_cyber.gz
/tmp/archive/2017-09-24_cyber.tar









share|improve this question















closed as unclear what you're asking by Scott, Stephen Rauch, Anthon, Romeo Ninov, Anthony Geoghegan Oct 4 '17 at 8:39


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


















    up vote
    0
    down vote

    favorite












    Need help in mass renaming files that prepending their parent directory name to them without using rename command.



    e.g.



    /tmp/2017-09-22/cyber.gz
    /tmp/2017-09-23/cyber.gz
    /tmp/2017-09-24/cyber.tar


    Also renamed files has to be copy in /tmp/archive without impacting above original files.



    Looks like below



    /tmp/archive/2017-09-22_cyber.gz
    /tmp/archive/2017-09-23_cyber.gz
    /tmp/archive/2017-09-24_cyber.tar









    share|improve this question















    closed as unclear what you're asking by Scott, Stephen Rauch, Anthon, Romeo Ninov, Anthony Geoghegan Oct 4 '17 at 8:39


    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Need help in mass renaming files that prepending their parent directory name to them without using rename command.



      e.g.



      /tmp/2017-09-22/cyber.gz
      /tmp/2017-09-23/cyber.gz
      /tmp/2017-09-24/cyber.tar


      Also renamed files has to be copy in /tmp/archive without impacting above original files.



      Looks like below



      /tmp/archive/2017-09-22_cyber.gz
      /tmp/archive/2017-09-23_cyber.gz
      /tmp/archive/2017-09-24_cyber.tar









      share|improve this question















      Need help in mass renaming files that prepending their parent directory name to them without using rename command.



      e.g.



      /tmp/2017-09-22/cyber.gz
      /tmp/2017-09-23/cyber.gz
      /tmp/2017-09-24/cyber.tar


      Also renamed files has to be copy in /tmp/archive without impacting above original files.



      Looks like below



      /tmp/archive/2017-09-22_cyber.gz
      /tmp/archive/2017-09-23_cyber.gz
      /tmp/archive/2017-09-24_cyber.tar






      bash rename






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 9 '17 at 5:26









      αғsнιη

      15.6k92563




      15.6k92563










      asked Oct 4 '17 at 0:22









      Nirmal

      42




      42




      closed as unclear what you're asking by Scott, Stephen Rauch, Anthon, Romeo Ninov, Anthony Geoghegan Oct 4 '17 at 8:39


      Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






      closed as unclear what you're asking by Scott, Stephen Rauch, Anthon, Romeo Ninov, Anthony Geoghegan Oct 4 '17 at 8:39


      Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.



          find * -path "archive" -prune -o -type f -exec 
          bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;


          • -path "archive" -prune is excluding archive directory itself.


          • $1///_ is replacing Slash / with Underscore _. The 1 is point to the find_bash first parameter which is file path passing by .


          (Replace echo with cp to copy or mv to move the files)



          Directory structure:



          .
          ├── 2017-09-22
          │   └── cyber.gz
          ├── 2017-09-23
          │   └── cyber.gz
          ├── 2017-09-24
          │   └── cyber.tar
          └── archive


          After executing the command:



          .
          ├── 2017-09-22
          │   └── cyber.gz
          ├── 2017-09-23
          │   └── cyber.gz
          ├── 2017-09-24
          │   └── cyber.tar
          └── archive
          ├── 2017-09-22_cyber.gz
          ├── 2017-09-23_cyber.gz
          └── 2017-09-24_cyber.tar





          share|improve this answer





























            up vote
            1
            down vote













            Using a short shell script:



            #!/bin/sh

            archive='/tmp/archive'

            mkdir -p "$archive"

            for name in /tmp/????-??-??/*; do
            dirdate=$name%/* # dirdate=$(dirname "$name")
            dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")

            newname="$archive/$dirdate_$name##*/"

            if [ ! -e "$newname" ]; then
            echo cp "$name" "$newname"
            fi
            done


            This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.



            If the new name does not exist inside the archive directory, the file is copied there.



            Remove echo from the above script to actually perform the copying.



            For the given example, the script will perform the following operations:



            cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
            cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
            cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar





            share|improve this answer





























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.



              find * -path "archive" -prune -o -type f -exec 
              bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;


              • -path "archive" -prune is excluding archive directory itself.


              • $1///_ is replacing Slash / with Underscore _. The 1 is point to the find_bash first parameter which is file path passing by .


              (Replace echo with cp to copy or mv to move the files)



              Directory structure:



              .
              ├── 2017-09-22
              │   └── cyber.gz
              ├── 2017-09-23
              │   └── cyber.gz
              ├── 2017-09-24
              │   └── cyber.tar
              └── archive


              After executing the command:



              .
              ├── 2017-09-22
              │   └── cyber.gz
              ├── 2017-09-23
              │   └── cyber.gz
              ├── 2017-09-24
              │   └── cyber.tar
              └── archive
              ├── 2017-09-22_cyber.gz
              ├── 2017-09-23_cyber.gz
              └── 2017-09-24_cyber.tar





              share|improve this answer


























                up vote
                1
                down vote













                If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.



                find * -path "archive" -prune -o -type f -exec 
                bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;


                • -path "archive" -prune is excluding archive directory itself.


                • $1///_ is replacing Slash / with Underscore _. The 1 is point to the find_bash first parameter which is file path passing by .


                (Replace echo with cp to copy or mv to move the files)



                Directory structure:



                .
                ├── 2017-09-22
                │   └── cyber.gz
                ├── 2017-09-23
                │   └── cyber.gz
                ├── 2017-09-24
                │   └── cyber.tar
                └── archive


                After executing the command:



                .
                ├── 2017-09-22
                │   └── cyber.gz
                ├── 2017-09-23
                │   └── cyber.gz
                ├── 2017-09-24
                │   └── cyber.tar
                └── archive
                ├── 2017-09-22_cyber.gz
                ├── 2017-09-23_cyber.gz
                └── 2017-09-24_cyber.tar





                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.



                  find * -path "archive" -prune -o -type f -exec 
                  bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;


                  • -path "archive" -prune is excluding archive directory itself.


                  • $1///_ is replacing Slash / with Underscore _. The 1 is point to the find_bash first parameter which is file path passing by .


                  (Replace echo with cp to copy or mv to move the files)



                  Directory structure:



                  .
                  ├── 2017-09-22
                  │   └── cyber.gz
                  ├── 2017-09-23
                  │   └── cyber.gz
                  ├── 2017-09-24
                  │   └── cyber.tar
                  └── archive


                  After executing the command:



                  .
                  ├── 2017-09-22
                  │   └── cyber.gz
                  ├── 2017-09-23
                  │   └── cyber.gz
                  ├── 2017-09-24
                  │   └── cyber.tar
                  └── archive
                  ├── 2017-09-22_cyber.gz
                  ├── 2017-09-23_cyber.gz
                  └── 2017-09-24_cyber.tar





                  share|improve this answer














                  If not using rename here is how you can do with Shell (Bash, ksh, ksh93, mksh, zsh) Pattern substitution expansion.



                  find * -path "archive" -prune -o -type f -exec 
                  bash -c 'echo "$1" "archive/$1///_"' find_bash '' ;


                  • -path "archive" -prune is excluding archive directory itself.


                  • $1///_ is replacing Slash / with Underscore _. The 1 is point to the find_bash first parameter which is file path passing by .


                  (Replace echo with cp to copy or mv to move the files)



                  Directory structure:



                  .
                  ├── 2017-09-22
                  │   └── cyber.gz
                  ├── 2017-09-23
                  │   └── cyber.gz
                  ├── 2017-09-24
                  │   └── cyber.tar
                  └── archive


                  After executing the command:



                  .
                  ├── 2017-09-22
                  │   └── cyber.gz
                  ├── 2017-09-23
                  │   └── cyber.gz
                  ├── 2017-09-24
                  │   └── cyber.tar
                  └── archive
                  ├── 2017-09-22_cyber.gz
                  ├── 2017-09-23_cyber.gz
                  └── 2017-09-24_cyber.tar






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 4 '17 at 6:38

























                  answered Oct 4 '17 at 5:47









                  αғsнιη

                  15.6k92563




                  15.6k92563






















                      up vote
                      1
                      down vote













                      Using a short shell script:



                      #!/bin/sh

                      archive='/tmp/archive'

                      mkdir -p "$archive"

                      for name in /tmp/????-??-??/*; do
                      dirdate=$name%/* # dirdate=$(dirname "$name")
                      dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")

                      newname="$archive/$dirdate_$name##*/"

                      if [ ! -e "$newname" ]; then
                      echo cp "$name" "$newname"
                      fi
                      done


                      This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.



                      If the new name does not exist inside the archive directory, the file is copied there.



                      Remove echo from the above script to actually perform the copying.



                      For the given example, the script will perform the following operations:



                      cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
                      cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
                      cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar





                      share|improve this answer


























                        up vote
                        1
                        down vote













                        Using a short shell script:



                        #!/bin/sh

                        archive='/tmp/archive'

                        mkdir -p "$archive"

                        for name in /tmp/????-??-??/*; do
                        dirdate=$name%/* # dirdate=$(dirname "$name")
                        dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")

                        newname="$archive/$dirdate_$name##*/"

                        if [ ! -e "$newname" ]; then
                        echo cp "$name" "$newname"
                        fi
                        done


                        This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.



                        If the new name does not exist inside the archive directory, the file is copied there.



                        Remove echo from the above script to actually perform the copying.



                        For the given example, the script will perform the following operations:



                        cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
                        cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
                        cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar





                        share|improve this answer
























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Using a short shell script:



                          #!/bin/sh

                          archive='/tmp/archive'

                          mkdir -p "$archive"

                          for name in /tmp/????-??-??/*; do
                          dirdate=$name%/* # dirdate=$(dirname "$name")
                          dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")

                          newname="$archive/$dirdate_$name##*/"

                          if [ ! -e "$newname" ]; then
                          echo cp "$name" "$newname"
                          fi
                          done


                          This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.



                          If the new name does not exist inside the archive directory, the file is copied there.



                          Remove echo from the above script to actually perform the copying.



                          For the given example, the script will perform the following operations:



                          cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
                          cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
                          cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar





                          share|improve this answer














                          Using a short shell script:



                          #!/bin/sh

                          archive='/tmp/archive'

                          mkdir -p "$archive"

                          for name in /tmp/????-??-??/*; do
                          dirdate=$name%/* # dirdate=$(dirname "$name")
                          dirdate=$dirdate##*/ # dirdate=$(basename "$dirdate")

                          newname="$archive/$dirdate_$name##*/"

                          if [ ! -e "$newname" ]; then
                          echo cp "$name" "$newname"
                          fi
                          done


                          This picks out dirdate as the basename of the directories that have dates as their names, and creates a new name for each file inside using this.



                          If the new name does not exist inside the archive directory, the file is copied there.



                          Remove echo from the above script to actually perform the copying.



                          For the given example, the script will perform the following operations:



                          cp /tmp/2017-09-22/cyber.gz /tmp/archive/2017-09-22_cyber.gz
                          cp /tmp/2017-09-23/cyber.gz /tmp/archive/2017-09-23_cyber.gz
                          cp /tmp/2017-09-24/cyber.tar /tmp/archive/2017-09-24_cyber.tar






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Oct 9 '17 at 5:39

























                          answered Oct 4 '17 at 6:34









                          Kusalananda

                          105k14209326




                          105k14209326