Mv several file to directory with same basenameChange to the last modified subdirectory of the current directoryDifference between files in directorycannot move Directory not emptyTemporary directory per processHow to move files from certain directories to newly created directories based on their extension?Move file only if another file with different suffix existsFind latest folder/file version and remove - no time stamp dataNeed to loop through folder and move files to different directory?Recursively rename all the files without changing their extensions?How to rename a file to have the same name and extension as another file in same directory

Fencing style for blades that can attack from a distance

How do I create uniquely male characters?

Email Account under attack (really) - anything I can do?

Test whether all array elements are factors of a number

Can I ask the recruiters in my resume to put the reason why I am rejected?

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

Did Shadowfax go to Valinor?

What typically incentivizes a professor to change jobs to a lower ranking university?

LaTeX closing $ signs makes cursor jump

What is the word for reserving something for yourself before others do?

What defenses are there against being summoned by the Gate spell?

In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?

Languages that we cannot (dis)prove to be Context-Free

How to format long polynomial?

Dragon forelimb placement

Today is the Center

Is it legal for company to use my work email to pretend I still work there?

Why Is Death Allowed In the Matrix?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

Why dont electromagnetic waves interact with each other?

"You are your self first supporter", a more proper way to say it

Show that if two triangles built on parallel lines, with equal bases have the same perimeter only if they are congruent.

Is it possible to do 50 km distance without any previous training?

TGV timetables / schedules?



Mv several file to directory with same basename


Change to the last modified subdirectory of the current directoryDifference between files in directorycannot move Directory not emptyTemporary directory per processHow to move files from certain directories to newly created directories based on their extension?Move file only if another file with different suffix existsFind latest folder/file version and remove - no time stamp dataNeed to loop through folder and move files to different directory?Recursively rename all the files without changing their extensions?How to rename a file to have the same name and extension as another file in same directory






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I have several files with different extensions and directories with the same basename, like this example.



FileA.txt
FileA.html
FileA.directory/

FileB.txt
FileB.html
FileB.directory/

FileC.txt
FileC.html
FileC.directory/


And i would like to create a for-loop for which i can move all files to their corresponding directory based on their basename. Resulting in the following



FileA.directory/FileA.txt
FileA.directory/FileA.html

FileB.directory/FileB.txt
FileB.directory/FileB.html

FileC.directory/FileC.txt
FileC.directory/FileC.html


I've tried to find several suggestion which are kind of similar to my problem on this page, like this example. But i can't find a completely similar question.



for dir in .*/
do
for f in "$dir"*
do
base=$f#$dir
mv "$base.*" "$dir"
done
done


However I can't get it to work.










share|improve this question






























    -1















    I have several files with different extensions and directories with the same basename, like this example.



    FileA.txt
    FileA.html
    FileA.directory/

    FileB.txt
    FileB.html
    FileB.directory/

    FileC.txt
    FileC.html
    FileC.directory/


    And i would like to create a for-loop for which i can move all files to their corresponding directory based on their basename. Resulting in the following



    FileA.directory/FileA.txt
    FileA.directory/FileA.html

    FileB.directory/FileB.txt
    FileB.directory/FileB.html

    FileC.directory/FileC.txt
    FileC.directory/FileC.html


    I've tried to find several suggestion which are kind of similar to my problem on this page, like this example. But i can't find a completely similar question.



    for dir in .*/
    do
    for f in "$dir"*
    do
    base=$f#$dir
    mv "$base.*" "$dir"
    done
    done


    However I can't get it to work.










    share|improve this question


























      -1












      -1








      -1








      I have several files with different extensions and directories with the same basename, like this example.



      FileA.txt
      FileA.html
      FileA.directory/

      FileB.txt
      FileB.html
      FileB.directory/

      FileC.txt
      FileC.html
      FileC.directory/


      And i would like to create a for-loop for which i can move all files to their corresponding directory based on their basename. Resulting in the following



      FileA.directory/FileA.txt
      FileA.directory/FileA.html

      FileB.directory/FileB.txt
      FileB.directory/FileB.html

      FileC.directory/FileC.txt
      FileC.directory/FileC.html


      I've tried to find several suggestion which are kind of similar to my problem on this page, like this example. But i can't find a completely similar question.



      for dir in .*/
      do
      for f in "$dir"*
      do
      base=$f#$dir
      mv "$base.*" "$dir"
      done
      done


      However I can't get it to work.










      share|improve this question
















      I have several files with different extensions and directories with the same basename, like this example.



      FileA.txt
      FileA.html
      FileA.directory/

      FileB.txt
      FileB.html
      FileB.directory/

      FileC.txt
      FileC.html
      FileC.directory/


      And i would like to create a for-loop for which i can move all files to their corresponding directory based on their basename. Resulting in the following



      FileA.directory/FileA.txt
      FileA.directory/FileA.html

      FileB.directory/FileB.txt
      FileB.directory/FileB.html

      FileC.directory/FileC.txt
      FileC.directory/FileC.html


      I've tried to find several suggestion which are kind of similar to my problem on this page, like this example. But i can't find a completely similar question.



      for dir in .*/
      do
      for f in "$dir"*
      do
      base=$f#$dir
      mv "$base.*" "$dir"
      done
      done


      However I can't get it to work.







      bash directory mv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      Rui F Ribeiro

      41.9k1483142




      41.9k1483142










      asked Mar 11 at 11:06









      RAHenriksenRAHenriksen

      6




      6




















          3 Answers
          3






          active

          oldest

          votes


















          1














          Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:



          topdir=.

          for dirname in "$topdir"/*/; do
          prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
          prefix=$prefix%%.* # FileC.directory --> FileC

          for filename in "$topdir/$prefix".*; do
          if [ ! -d "$filename" ]; then
          mv -i "$filename" "$dirname"
          fi
          done
          done


          The outer loop iterates over all directories in the directory $topdir (here set to ., the current directory). The $prefix will be the base name of the directory name, with the bit after the first dot removed.



          Once the prefix has been computed, non-directories (files) in the same $topdir directory that share the same prefix are moved to the directory.






          share|improve this answer
































            0














            dirs=$(ls | grep ".d")
            for d in $dirs
            do
            filename=$(echo $d | awk -F"." 'print $1')
            ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
            done


            Maybe too verbose, but it works






            share|improve this answer


















            • 1





              Note that .d also matches strings like bad since . matches any character.

              – Kusalananda
              Mar 11 at 11:52






            • 1





              You should escape it "."

              – Prvt_Yadv
              Mar 11 at 11:53











            • @Prvt_Yadv It's not enough. That may still match FileA.draft.txt, even though the given list in the question does not contain such a filename.

              – Kusalananda
              Mar 11 at 12:18











            • Related: Why *not* parse `ls` (and what do to instead)?

              – Kusalananda
              Mar 11 at 12:49


















            0














            Assuming we know that all files have the filename suffixes .html or .txt and that all directories have the suffix .directory, you can use:



            for i in *.txt *.html
            do
            mv "$i" "$i%%.*.directory/$i"
            done


            Run this in same directory where you are having files.
            It will remove trailing .txt and .html from file names and then will move files to destination directory.






            share|improve this answer

























              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505607%2fmv-several-file-to-directory-with-same-basename%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:



              topdir=.

              for dirname in "$topdir"/*/; do
              prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
              prefix=$prefix%%.* # FileC.directory --> FileC

              for filename in "$topdir/$prefix".*; do
              if [ ! -d "$filename" ]; then
              mv -i "$filename" "$dirname"
              fi
              done
              done


              The outer loop iterates over all directories in the directory $topdir (here set to ., the current directory). The $prefix will be the base name of the directory name, with the bit after the first dot removed.



              Once the prefix has been computed, non-directories (files) in the same $topdir directory that share the same prefix are moved to the directory.






              share|improve this answer





























                1














                Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:



                topdir=.

                for dirname in "$topdir"/*/; do
                prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
                prefix=$prefix%%.* # FileC.directory --> FileC

                for filename in "$topdir/$prefix".*; do
                if [ ! -d "$filename" ]; then
                mv -i "$filename" "$dirname"
                fi
                done
                done


                The outer loop iterates over all directories in the directory $topdir (here set to ., the current directory). The $prefix will be the base name of the directory name, with the bit after the first dot removed.



                Once the prefix has been computed, non-directories (files) in the same $topdir directory that share the same prefix are moved to the directory.






                share|improve this answer



























                  1












                  1








                  1







                  Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:



                  topdir=.

                  for dirname in "$topdir"/*/; do
                  prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
                  prefix=$prefix%%.* # FileC.directory --> FileC

                  for filename in "$topdir/$prefix".*; do
                  if [ ! -d "$filename" ]; then
                  mv -i "$filename" "$dirname"
                  fi
                  done
                  done


                  The outer loop iterates over all directories in the directory $topdir (here set to ., the current directory). The $prefix will be the base name of the directory name, with the bit after the first dot removed.



                  Once the prefix has been computed, non-directories (files) in the same $topdir directory that share the same prefix are moved to the directory.






                  share|improve this answer















                  Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:



                  topdir=.

                  for dirname in "$topdir"/*/; do
                  prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
                  prefix=$prefix%%.* # FileC.directory --> FileC

                  for filename in "$topdir/$prefix".*; do
                  if [ ! -d "$filename" ]; then
                  mv -i "$filename" "$dirname"
                  fi
                  done
                  done


                  The outer loop iterates over all directories in the directory $topdir (here set to ., the current directory). The $prefix will be the base name of the directory name, with the bit after the first dot removed.



                  Once the prefix has been computed, non-directories (files) in the same $topdir directory that share the same prefix are moved to the directory.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 11 at 12:03

























                  answered Mar 11 at 11:46









                  KusalanandaKusalananda

                  140k17261435




                  140k17261435























                      0














                      dirs=$(ls | grep ".d")
                      for d in $dirs
                      do
                      filename=$(echo $d | awk -F"." 'print $1')
                      ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
                      done


                      Maybe too verbose, but it works






                      share|improve this answer


















                      • 1





                        Note that .d also matches strings like bad since . matches any character.

                        – Kusalananda
                        Mar 11 at 11:52






                      • 1





                        You should escape it "."

                        – Prvt_Yadv
                        Mar 11 at 11:53











                      • @Prvt_Yadv It's not enough. That may still match FileA.draft.txt, even though the given list in the question does not contain such a filename.

                        – Kusalananda
                        Mar 11 at 12:18











                      • Related: Why *not* parse `ls` (and what do to instead)?

                        – Kusalananda
                        Mar 11 at 12:49















                      0














                      dirs=$(ls | grep ".d")
                      for d in $dirs
                      do
                      filename=$(echo $d | awk -F"." 'print $1')
                      ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
                      done


                      Maybe too verbose, but it works






                      share|improve this answer


















                      • 1





                        Note that .d also matches strings like bad since . matches any character.

                        – Kusalananda
                        Mar 11 at 11:52






                      • 1





                        You should escape it "."

                        – Prvt_Yadv
                        Mar 11 at 11:53











                      • @Prvt_Yadv It's not enough. That may still match FileA.draft.txt, even though the given list in the question does not contain such a filename.

                        – Kusalananda
                        Mar 11 at 12:18











                      • Related: Why *not* parse `ls` (and what do to instead)?

                        – Kusalananda
                        Mar 11 at 12:49













                      0












                      0








                      0







                      dirs=$(ls | grep ".d")
                      for d in $dirs
                      do
                      filename=$(echo $d | awk -F"." 'print $1')
                      ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
                      done


                      Maybe too verbose, but it works






                      share|improve this answer













                      dirs=$(ls | grep ".d")
                      for d in $dirs
                      do
                      filename=$(echo $d | awk -F"." 'print $1')
                      ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
                      done


                      Maybe too verbose, but it works







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 11 at 11:45









                      dgandgan

                      13919




                      13919







                      • 1





                        Note that .d also matches strings like bad since . matches any character.

                        – Kusalananda
                        Mar 11 at 11:52






                      • 1





                        You should escape it "."

                        – Prvt_Yadv
                        Mar 11 at 11:53











                      • @Prvt_Yadv It's not enough. That may still match FileA.draft.txt, even though the given list in the question does not contain such a filename.

                        – Kusalananda
                        Mar 11 at 12:18











                      • Related: Why *not* parse `ls` (and what do to instead)?

                        – Kusalananda
                        Mar 11 at 12:49












                      • 1





                        Note that .d also matches strings like bad since . matches any character.

                        – Kusalananda
                        Mar 11 at 11:52






                      • 1





                        You should escape it "."

                        – Prvt_Yadv
                        Mar 11 at 11:53











                      • @Prvt_Yadv It's not enough. That may still match FileA.draft.txt, even though the given list in the question does not contain such a filename.

                        – Kusalananda
                        Mar 11 at 12:18











                      • Related: Why *not* parse `ls` (and what do to instead)?

                        – Kusalananda
                        Mar 11 at 12:49







                      1




                      1





                      Note that .d also matches strings like bad since . matches any character.

                      – Kusalananda
                      Mar 11 at 11:52





                      Note that .d also matches strings like bad since . matches any character.

                      – Kusalananda
                      Mar 11 at 11:52




                      1




                      1





                      You should escape it "."

                      – Prvt_Yadv
                      Mar 11 at 11:53





                      You should escape it "."

                      – Prvt_Yadv
                      Mar 11 at 11:53













                      @Prvt_Yadv It's not enough. That may still match FileA.draft.txt, even though the given list in the question does not contain such a filename.

                      – Kusalananda
                      Mar 11 at 12:18





                      @Prvt_Yadv It's not enough. That may still match FileA.draft.txt, even though the given list in the question does not contain such a filename.

                      – Kusalananda
                      Mar 11 at 12:18













                      Related: Why *not* parse `ls` (and what do to instead)?

                      – Kusalananda
                      Mar 11 at 12:49





                      Related: Why *not* parse `ls` (and what do to instead)?

                      – Kusalananda
                      Mar 11 at 12:49











                      0














                      Assuming we know that all files have the filename suffixes .html or .txt and that all directories have the suffix .directory, you can use:



                      for i in *.txt *.html
                      do
                      mv "$i" "$i%%.*.directory/$i"
                      done


                      Run this in same directory where you are having files.
                      It will remove trailing .txt and .html from file names and then will move files to destination directory.






                      share|improve this answer





























                        0














                        Assuming we know that all files have the filename suffixes .html or .txt and that all directories have the suffix .directory, you can use:



                        for i in *.txt *.html
                        do
                        mv "$i" "$i%%.*.directory/$i"
                        done


                        Run this in same directory where you are having files.
                        It will remove trailing .txt and .html from file names and then will move files to destination directory.






                        share|improve this answer



























                          0












                          0








                          0







                          Assuming we know that all files have the filename suffixes .html or .txt and that all directories have the suffix .directory, you can use:



                          for i in *.txt *.html
                          do
                          mv "$i" "$i%%.*.directory/$i"
                          done


                          Run this in same directory where you are having files.
                          It will remove trailing .txt and .html from file names and then will move files to destination directory.






                          share|improve this answer















                          Assuming we know that all files have the filename suffixes .html or .txt and that all directories have the suffix .directory, you can use:



                          for i in *.txt *.html
                          do
                          mv "$i" "$i%%.*.directory/$i"
                          done


                          Run this in same directory where you are having files.
                          It will remove trailing .txt and .html from file names and then will move files to destination directory.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 11 at 12:22









                          Kusalananda

                          140k17261435




                          140k17261435










                          answered Mar 11 at 11:41









                          Prvt_YadvPrvt_Yadv

                          3,17131330




                          3,17131330



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505607%2fmv-several-file-to-directory-with-same-basename%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                              Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                              Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.