Skip to content

Instantly share code, notes, and snippets.

@githubcom13
Last active August 17, 2021 22:34
Show Gist options
  • Select an option

  • Save githubcom13/90d8004c5427da86524a7b09c79ab95c to your computer and use it in GitHub Desktop.

Select an option

Save githubcom13/90d8004c5427da86524a7b09c79ab95c to your computer and use it in GitHub Desktop.
Create new folder for each file - Linux/MacOs

Create a folder based on the filename, but exclude the extension, then move the original item with the same name into the folder.

before:

Folder
|- File1.ext
|- File2.ext
|- File3.ext
|- File4.ext

After:

Folder 
|- File1 *"This is a folder"*
   |- File1.ext *"This is the file"*
|- File2
   |- File2.ext
|- File3
   |- File3.ext<br>
|- File4<br>
   |- File4.ext<br>

Open a terminal window in the folder containing the files and type the following command:

for FILE in *; do FOLDER=$(echo $FILE | sed 's/\.[A-Za-z0-9]*$//'); mkdir "$FOLDER"; mv "$FILE" "$FOLDER" ; done

Note: it will fail creating the folder for the files without extension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment