Solved Create folders for files, then move files into folders.

September 3, 2016 at 21:38:02
Specs: Linux
How do I make new folders with the name of all the files in a folder without the file extension in the folder name, then move the files in to there corresponding folder in Fedora 24 Linux?

Example:
File0.mp4
File1.mp4

Folders created:
File0
File1

Then move files in to folders.


See More: Create folders for files, then move files into folders.

Report •

#1
September 4, 2016 at 05:15:02
Not sure if this is what you want, but here goes:
mkdir File0 File1
mv File0.mp4 File0
mv File1.mp4 File1

Don C


Report •

#2
September 4, 2016 at 10:32:00
Thanks for the reply dcutl002.

I was hoping to automate it more, as I have a few hundred files to move to individual folders, and the file names all vary drastically.


Report •

#3
September 4, 2016 at 15:51:31
✔ Best Answer
This bash script is one way .....

#!/bin/bash

# change to your directory name
yourdir="/export/home/nails/s1"
cd $yourdir

# find files ONLY IN THE CURRENT DIRECTORY
# then foreach file remove the extension and make the directory from 
# that name. finally move the file to that directory
find . ! -name .  -prune -type f| while read myfile
do
   noext=${myfile%.*} # remove the extension
   mkdir -p $noext    # make the directory if it doesn't exist
   mv $myfile $noext  # move the file to the directory
done

message edited by nails


Report •
Related Solutions


Ask Question