noobs script for file and folder management via bash

glkshankar

New Member
Credits
44
First of all, I was trying to find the right forum to ask my noob question and after several searches found good reviews about this place. Now let me come to point, I have around 1000 media files in a folder and want to create a folder with a suffix of the year of release and then move the media file into that directory. Currently, all my media files are with various extensions eg: mkv,mp4,srt,wmv etc and all the file names have spaces in them as below. And also I have a text file which has the folder name with 'file name (YYYY)'. which I can create all the folders easily using "xargs -I {} mkdir -p "{}" < folders.txt".
But now the question is how to move the file into the folder?

File Names:
'Angel Has Fallen.mkv'
'Apocalypse Now.mp4'
Aquaman.mkv
Arrival.mkv
Arrival.srt
Avatar.sub


Folder name is going to be

'Angel Has Fallen (2019)
'Apocalypse Now (1979)
Aquaman (2018)
Arrival (2016)
Avatar (2009)


Tried this one but no luck

for i in *
do
file_name="${i%.*}"
folder_name=${file_name::-6}
mv $file_name $folder_name
done;


Any suggestions or help is greatly appreciated..

Cheers
 


Tolkem

Well-Known Member
Credits
1,109
First of all, I was trying to find the right forum to ask my noob question and after several searches found good reviews about this place. Now let me come to point, I have around 1000 media files in a folder and want to create a folder with a suffix of the year of release and then move the media file into that directory. Currently, all my media files are with various extensions eg: mkv,mp4,srt,wmv etc and all the file names have spaces in them as below. And also I have a text file which has the folder name with 'file name (YYYY)'. which I can create all the folders easily using "xargs -I {} mkdir -p "{}" < folders.txt".
But now the question is how to move the file into the folder?

File Names:
'Angel Has Fallen.mkv'
'Apocalypse Now.mp4'
Aquaman.mkv
Arrival.mkv
Arrival.srt
Avatar.sub


Folder name is going to be

'Angel Has Fallen (2019)
'Apocalypse Now (1979)
Aquaman (2018)
Arrival (2016)
Avatar (2009)


Tried this one but no luck

for i in *
do
file_name="${i%.*}"
folder_name=${file_name::-6}
mv $file_name $folder_name
done;


Any suggestions or help is greatly appreciated..

Cheers
I also have a large collection of multimedia files and once time ago wanted to do the same, so I came up with this which did the job, I didn't do the whole thing by myself tho, I adapted it from a script I found somewhere on the web, can't remember where exactly now but it worked. This will do some of that you want to. First, you have to rename your files so they have no spaces i.e Angel_has_fallen.mkv instead of Angel has fallen.mkv, otherwise you'll end up with several folders, like F1Angel, F2has, F3fallen. At least I couldn't figure it out by then and still can't lol However, it occurs to me now one could add some code to handle the renaming part as well. The script will create folders according to the file name and move them into those. Here's the code:
Code:
#!/bin/bash
#Create folders and move files by name into them accordingly
ls $file | cut -c 1-21 | sort -u | xargs mkdir -p $file &> /dev/null
for dir in */
do for file in "${dir%/}"?*
do
[ -f "$file" ] && mv -v "$file" "$dir"
done
done
Hope this helps! :)

EDIT: Just googled and found a way to handle the renaming part;
Code:
for oldname in *; do newname=`echo $oldname | sed -e 's/ //g'`; mv "$oldname" "$newname";
Run that before the script to rename all the files at once. Just tried and it works :)
 
Last edited:

glkshankar

New Member
Credits
44
Thanks for the reply, my challenge is i dont want to rename my files unless its a no go without it. I am exploring for any other alternate ways of the script.
 

Tolkem

Well-Known Member
Credits
1,109
Thanks for the reply, my challenge is i dont want to rename my files unless its a no go without it. I am exploring for any other alternate ways of the script.
Renaming is mandatory; names can't have spaces and there's nothing we can't do about it, that's the way Linux works unless you want to go file by file, one at a time which is tedious and you'll have to use quoting marks around every one so bash can read them as a unit, otherwise it won't work.
 


Members online


Latest posts

Top