1

I have a list of files in a folder:

./file1.ext
./file2.ext2
./file3.ext

I want to move all these files in their own new folder with their related name, like:

./file1/file1.ext
./file2/file2.ext2
./file3/file3.ext

Is there a way to do so in very few commands?

1 Answers1

0

You can use a loop to do so :

for f in *; do
    [ ! -d "${f%.*}" ] && mkdir "${f%.*}/"
    mv "$f" "${f%.*}/"
done

This will create and move everything in your current directory.

${f%.*} take the trailing extension. I also use it to verify if the directory already exist in case you have multiple file like file1.ext and file1.ext1.

In case you have a file1 this script will not work for any file begining with file1

Marius_Couet
  • 1,095