Taylor Cohron
Taylor Cohron

Reputation: 3

How to make folders for individual files within a directory via bash script?

So I've got a movie collection that's dumped into a single folder (I know, bad practice in retrospect.) I want to organize things a bit so I can use Radarr to grab all the appropriate metadata, but I need all the individual files in their own folders. I created the script below to try and automate the process a bit, but I get the following error.

Script

#! /bin/bash

for f in /the/path/to/files/* ;
 do
    [[ -d $f ]] && continue
    mkdir "${f%.*}"
    mv "$f" "${f%.*}"
done

EDIT

So I've now run the script through Shellcheck.net per the suggestion of Benjamin W. It doesn't throw any errors according to the site, though I still get the same errors when I try running the command.

EDIT 2*

No errors now, but the script does nothing when executed.

Upvotes: 0

Views: 312

Answers (1)

Benjamin W.
Benjamin W.

Reputation: 52506

Assignments are evaluated only once, and not whenever the variable being assigned to is used, which I think is what your script assumes.

You could use a loop like this:

for f in /path/to/all/the/movie/files/*; do
    mkdir "${f%.*}"
    mv "$f" "${f%.*}"
done

This uses parameter expansion instead of cut to get rid of the file extension.

Upvotes: 2

Related Questions