-2

How to move all mkv files from the root of the media folder to own subfolder for each mkv file, with the same name as mkv filename beside .mkv extension on the end. Example home/incoming/media/ is the media folder where mkv files reside in the root, files naming examples Ibica.festival.2020.mkv in the root should get folder home/incoming/media/Ibica.festival/Ibica.festival.2020.mkv I've got around 300 files, some files where with the Chinese names and some where with Russian Cyrillic if that changes situation, at all. File attribute and dates should be detained in the new folder.

I tried and I had some errors and some success with the command find . -name ".mkv" -exec sh -c 'mkdir "${1%.}" ; mv "$1" "${1%.*}" ' _ {} \; –

Lots of files where moved some where not, but how would I include subtitles and info files which carry the same name as mkv file like Ibica.festival.2020.mkv srt file would be Ibica.festival.2020.en.srt and the info file would be Ibica.festival.2020.en.info

      ///media/
             /Ibica.festival.2020.mkv
             /Ibica.festival.2020.hr.srt
             /Ibica.festival.2020.sr.srt
             /Ibica.festival.2020.br.srt
             /Ibica.festival.2020.info
     ///media/
             /exit festival 2019.mkv
             /exit festival 2019.hr.srt
             /exit festival 2019.sr.srt
             /exit festival 2019.en.srt
             /exit festival 2019.info

Should be moved to

       ///media/
               /Ibica.festival.2020/
                                   /Ibica.festival.2020.mkv
                                   /Ibica.festival.2020.hr.srt
                                   /Ibica.festival.2020.sr.srt
                                   /Ibica.festival.2020.br.srt
                                   /Ibica.festival.2020.info
   ///media/
           /exit festival 2019/
                              /exit festival 2019.mkv
                              /exit festival 2019.hr.srt
                              /exit festival 2019.sr.srt
                              /exit festival 2019.en.srt
                              /exit festival 2019.info

Loop1
  • 21
  • Sorry I eddied the post if it sounded like an order I didn't meant it like that – Loop1 Oct 16 '23 at 14:08
  • 1
    What's the logic of the folder for file Ibica.festival.2020.mkv being Ibica.festival, not ibica.fetsival.2020 or ibica? – treuss Oct 16 '23 at 14:13
  • It seems like you didn't even try to do it yourself. You should try solving your problem by yourself first. Here is how to ask a good question https://stackoverflow.com/help/how-to-ask. – davidt930 Oct 16 '23 at 14:14
  • I tried and I had some errors and some success with the command find . -name ".mkv" -exec sh -c 'mkdir "${1%.}" ; mv "$1" "${1%.*}" ' _ {} ; – Loop1 Oct 16 '23 at 14:30
  • Lots of files where moved some where not, but how would I include subtitles and info files which carry same name as mkv file like Ibica.festival.2020.mkv srt file would be Ibica.festival.2020.en.srt and info file would be Ibica.festival.2020.en.info – Loop1 Oct 16 '23 at 14:34

1 Answers1

0

First of all like everyone said you have to follow the question asking guidelines , and do some research on your own. That said you can try this to test the command first and then apply it to your media folor:

mkdir test && cd test
for i in {0..10};do touch file$i.mkv;done
ls | grep '\.mkv$' | while read line; do mkdir $(echo "$line" |sed 's/\.mkv$//');mv "$line" $(echo "$line" |sed 's/\.mkv$//') ;done

after Edit:

ls | grep '\.mkv$' | while read line
do
    folder_name=$(echo "$line" |sed 's/\.mkv//')
    mkdir "$folder_name"
    mv "$line" "$folder_name"
    mv "${folder_name}."* "$folder_name"
done

you can save this as a script.sh and run it by ./script.sh, don't forget to chmod +x ./script.sh. make sure to save it and run it inside the media directory.

  • No this is not working for i in {0..10};do touch file$i.mkv;done is making 10 mkv files from 1 to 10. Second command is moving miss-mashed some srt file in their own folder some info in their own folder, files that have spaces in file name structure will get folder partially named . – Loop1 Oct 16 '23 at 15:43
  • as i said this is a test, the main command is the third one, which creates directories and moves the files into it. so now, please re-edit your post telling exactly what you have in the media directory. and what are the extensions of the files, and what exactly you're having trouble with. and PLEASE don't copy paste commands from the internet without knowing what do they do. I'll edit the command to only move mkv files. – white-hat-er Oct 16 '23 at 16:47
  • I edited the post look at the end examples and how it should sort. – Loop1 Oct 18 '23 at 21:58
  • your edited script worked like a charm – Loop1 Oct 23 '23 at 05:07
  • Parsing ls should be avoided. That grep is unnecessary; the same effect is achieved by simply using ls *.mkv. The read utility should be invoked with -r and with IFS reset, like this: while IFS= read -r line etc. – Vilinkameni Oct 27 '23 at 08:28