1

This question already has an answer here:

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

marked as duplicate by Christopher, Rui F Ribeiro, G-Man, Thomas, Jeff Schaller Dec 1 '18 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 '18 at 23:34
0

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
1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.