Search code examples
linuxcommand

Sending all files into folder with the same name


Let's say we have tens of files in one folder with no pattern. I want to send whole files into the folder with the same name.

Current file structure:

 test1234.js
 new-york.php
 foo.cs

My aim:

 test1234/test1234.js
 new-york/new-york.php
 foo/foo.cs

Is it possible to do this with one single command?


Solution

  • It depends what you consider a single command. It can be done with a simple loop:

    for FILE in *; do
        if [ -f "$FILE" ]; then 
            mkdir -p "${FILE%.*}"
            mv "$FILE" "${FILE%.*}"
        fi
    done