Make script to create subdirectories based on file name and move matching files into it [duplicate]

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











up vote
1
down vote

favorite
1













This question already has an answer here:



  • Move files by reading their file names to different directories

    3 answers



A folder has a huge number of files; each pair of files has the same name but different extensions (.obj and .mtl). I want to make a folder for each pair of files with the same name, then move each pair of files into its matching folder.
File names always have letters; some also have underscores and numbers.



What I need to do is:

A) Read a file name

B) Make a subdirectory with the same name as the preceding file

C) Move the pair of files matching the filename into the matching subdirectory

D) Repeat for the next file



Example:

Files: Big_Column1.obj , Big_Column1.mtl

To be moved into subdirectory: Big_Column1










share|improve this question















marked as duplicate by Christopher, Rui F Ribeiro, G-Man, Thomas, Jeff Schaller Dec 1 at 12:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Can you add an example of the file names? Are they alphanumeric with a single dot at the extension and no other characters?
    – ivanivan
    Nov 30 at 23:34














up vote
1
down vote

favorite
1













This question already has an answer here:



  • Move files by reading their file names to different directories

    3 answers



A folder has a huge number of files; each pair of files has the same name but different extensions (.obj and .mtl). I want to make a folder for each pair of files with the same name, then move each pair of files into its matching folder.
File names always have letters; some also have underscores and numbers.



What I need to do is:

A) Read a file name

B) Make a subdirectory with the same name as the preceding file

C) Move the pair of files matching the filename into the matching subdirectory

D) Repeat for the next file



Example:

Files: Big_Column1.obj , Big_Column1.mtl

To be moved into subdirectory: Big_Column1










share|improve this question















marked as duplicate by Christopher, Rui F Ribeiro, G-Man, Thomas, Jeff Schaller Dec 1 at 12:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Can you add an example of the file names? Are they alphanumeric with a single dot at the extension and no other characters?
    – ivanivan
    Nov 30 at 23:34












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1






This question already has an answer here:



  • Move files by reading their file names to different directories

    3 answers



A folder has a huge number of files; each pair of files has the same name but different extensions (.obj and .mtl). I want to make a folder for each pair of files with the same name, then move each pair of files into its matching folder.
File names always have letters; some also have underscores and numbers.



What I need to do is:

A) Read a file name

B) Make a subdirectory with the same name as the preceding file

C) Move the pair of files matching the filename into the matching subdirectory

D) Repeat for the next file



Example:

Files: Big_Column1.obj , Big_Column1.mtl

To be moved into subdirectory: Big_Column1










share|improve this question
















This question already has an answer here:



  • Move files by reading their file names to different directories

    3 answers



A folder has a huge number of files; each pair of files has the same name but different extensions (.obj and .mtl). I want to make a folder for each pair of files with the same name, then move each pair of files into its matching folder.
File names always have letters; some also have underscores and numbers.



What I need to do is:

A) Read a file name

B) Make a subdirectory with the same name as the preceding file

C) Move the pair of files matching the filename into the matching subdirectory

D) Repeat for the next file



Example:

Files: Big_Column1.obj , Big_Column1.mtl

To be moved into subdirectory: Big_Column1





This question already has an answer here:



  • Move files by reading their file names to different directories

    3 answers







shell-script files rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 at 23:43

























asked Nov 30 at 22:23









Ninjabdou

84




84




marked as duplicate by Christopher, Rui F Ribeiro, G-Man, Thomas, Jeff Schaller Dec 1 at 12:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Christopher, Rui F Ribeiro, G-Man, Thomas, Jeff Schaller Dec 1 at 12:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Can you add an example of the file names? Are they alphanumeric with a single dot at the extension and no other characters?
    – ivanivan
    Nov 30 at 23:34
















  • Can you add an example of the file names? Are they alphanumeric with a single dot at the extension and no other characters?
    – ivanivan
    Nov 30 at 23:34















Can you add an example of the file names? Are they alphanumeric with a single dot at the extension and no other characters?
– ivanivan
Nov 30 at 23:34




Can you add an example of the file names? Are they alphanumeric with a single dot at the extension and no other characters?
– ivanivan
Nov 30 at 23:34










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Here's a quick one-liner to accomplish this. We simply iterate over all files in the current directory using shell globbing (assuming they are the ones you want), create a directory with the name of the file before the period using cut to modify the name (mkdir -p not erroring if the directory already exists), and move the file there. There's not really much to it.



for file in *; do dir=$(echo $file | cut -d. -f1); mkdir -p $dir; mv $file $dir; done





share|improve this answer



























    up vote
    1
    down vote













    Tested and worked fine



    Finding out the all filname and segregating the filenames with same name but with different extensions andsorting out and creating directory and move the files to directory of same name



    In below example Big_Column1,Big_Column2 directories are created



    for i in `ls -ltrh directorypath| awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done

    Before

    praveen_linux_example pravee]# ll
    total 0
    -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
    -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
    -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
    -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj


    command:n_linux_example pravee]# for i in `ls -ltrh | awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done


    after



    After executing below is the output of ls -ltr

    drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column1
    -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
    -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
    drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column2
    -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
    -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj





    share|improve this answer



























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      Here's a quick one-liner to accomplish this. We simply iterate over all files in the current directory using shell globbing (assuming they are the ones you want), create a directory with the name of the file before the period using cut to modify the name (mkdir -p not erroring if the directory already exists), and move the file there. There's not really much to it.



      for file in *; do dir=$(echo $file | cut -d. -f1); mkdir -p $dir; mv $file $dir; done





      share|improve this answer
























        up vote
        0
        down vote



        accepted










        Here's a quick one-liner to accomplish this. We simply iterate over all files in the current directory using shell globbing (assuming they are the ones you want), create a directory with the name of the file before the period using cut to modify the name (mkdir -p not erroring if the directory already exists), and move the file there. There's not really much to it.



        for file in *; do dir=$(echo $file | cut -d. -f1); mkdir -p $dir; mv $file $dir; done





        share|improve this answer






















          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Here's a quick one-liner to accomplish this. We simply iterate over all files in the current directory using shell globbing (assuming they are the ones you want), create a directory with the name of the file before the period using cut to modify the name (mkdir -p not erroring if the directory already exists), and move the file there. There's not really much to it.



          for file in *; do dir=$(echo $file | cut -d. -f1); mkdir -p $dir; mv $file $dir; done





          share|improve this answer












          Here's a quick one-liner to accomplish this. We simply iterate over all files in the current directory using shell globbing (assuming they are the ones you want), create a directory with the name of the file before the period using cut to modify the name (mkdir -p not erroring if the directory already exists), and move the file there. There's not really much to it.



          for file in *; do dir=$(echo $file | cut -d. -f1); mkdir -p $dir; mv $file $dir; done






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 1 at 2:06









          multithr3at3d

          42028




          42028






















              up vote
              1
              down vote













              Tested and worked fine



              Finding out the all filname and segregating the filenames with same name but with different extensions andsorting out and creating directory and move the files to directory of same name



              In below example Big_Column1,Big_Column2 directories are created



              for i in `ls -ltrh directorypath| awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done

              Before

              praveen_linux_example pravee]# ll
              total 0
              -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
              -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
              -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
              -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj


              command:n_linux_example pravee]# for i in `ls -ltrh | awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done


              after



              After executing below is the output of ls -ltr

              drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column1
              -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
              -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
              drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column2
              -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
              -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj





              share|improve this answer
























                up vote
                1
                down vote













                Tested and worked fine



                Finding out the all filname and segregating the filenames with same name but with different extensions andsorting out and creating directory and move the files to directory of same name



                In below example Big_Column1,Big_Column2 directories are created



                for i in `ls -ltrh directorypath| awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done

                Before

                praveen_linux_example pravee]# ll
                total 0
                -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
                -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
                -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
                -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj


                command:n_linux_example pravee]# for i in `ls -ltrh | awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done


                after



                After executing below is the output of ls -ltr

                drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column1
                -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
                -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
                drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column2
                -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
                -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Tested and worked fine



                  Finding out the all filname and segregating the filenames with same name but with different extensions andsorting out and creating directory and move the files to directory of same name



                  In below example Big_Column1,Big_Column2 directories are created



                  for i in `ls -ltrh directorypath| awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done

                  Before

                  praveen_linux_example pravee]# ll
                  total 0
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj


                  command:n_linux_example pravee]# for i in `ls -ltrh | awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done


                  after



                  After executing below is the output of ls -ltr

                  drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column1
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
                  drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column2
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj





                  share|improve this answer












                  Tested and worked fine



                  Finding out the all filname and segregating the filenames with same name but with different extensions andsorting out and creating directory and move the files to directory of same name



                  In below example Big_Column1,Big_Column2 directories are created



                  for i in `ls -ltrh directorypath| awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done

                  Before

                  praveen_linux_example pravee]# ll
                  total 0
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj


                  command:n_linux_example pravee]# for i in `ls -ltrh | awk 'print $NF'| awk -F "." 'print $1'| sort| uniq`; do mkdir $i; yes|cp $i* $i; done


                  after



                  After executing below is the output of ls -ltr

                  drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column1
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.mtl
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column1.obj
                  drwxr-xr-x. 2 root root 4096 Nov 30 22:39 Big_Column2
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.mtl
                  -rw-r--r--. 1 root root 0 Nov 30 22:38 Big_Column2.obj






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 1 at 6:47









                  Praveen Kumar BS

                  1,162138




                  1,162138












                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay