Cmd Line help

Status
Not open for further replies.

George51

Contributor
Joined
Feb 4, 2014
Messages
126
Going round in circles on this one.

I have a folder "Movies"

Within it I have lots of different movies in the format "[movie title][year].*"
I also have some within their own folders ie. "[movie title][year]/[movie title][year].*"

I have been trying for hours to come up with either a cmd line or a small script to automate moving the 1000 odd files into their own folders. So far I've been unsuccessful, result in various failed 'for' loops
Save me having to create folders manual and them move the files individually. Any help would be greatly appreciated

Cheers
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
CAVEAT: untested

Code:
MOVIE_PATH="/mnt/pool/movies"
find "$MOVIE_PATH" -maxdepth 1 -type f -print | while read MOVIE_FILE_PATH
do
	MOVIE_FILE="$(basename "$MOVIE_FILE_PATH")"
	MOVIE_NAME="${MOVIE_FILE%.*}"
	mkdir "$MOVIE_PATH/$MOVIE_NAME"
	mv -n "$MOVIE_FILE_PATH" "$MOVIE_PATH/$MOVIE_NAME/"
done

The find command will search for only files that are directly in the given MOVIE_PATH. That will exclude folders and any movies that have already been placed inside folders.
The output (one file per line) is piped in to the while loop.
'basename' strips off the MOVIE_PATH component.
'${MOVIE_FILE%.*}' removes the last extension. This is probably the trickiest part because you don't want to damage a movie title if it contains a '.' and it's unlikely that you have any movie files with multiple extensions (.tar.gz for example).
'mkdir' creates a new directory with the movie name under the base path.
'mv -n' moves the file, but bails if there's an existing file.

Again, this hasn't been tested. It might be a good idea to just 'echo' the mkdir and mv lines. Then you can check that you aren't moving any files to incorrect locations before you run the script for real.
 
Status
Not open for further replies.
Top