0

Each folder has a .mkv or .avi file.

E.g:

Sommerferien201308/eins.avi Sommerferien201309/eins.mkv Herbst201401/film.avi Herbst201402/krz.mkv 

Renaming to:

Sommerferien201308/Sommerferien201308.avi Sommerferien201309/Sommerferien201309.mkv Herbst201401/Herbst201401.avi Herbst201402/Herbst201402.mkv 

How can I rename the filename to the name of its folder? Of course every foldername is unique. And too much to do it manually for each file/folder.

I Would try with find.

find . -type d -name "" -exec cd "" && mv {} \; 

But i dont know how to select the folder name and how to make the .avi or .mkv selection and how to store the selected folder name..

7
  • One folder is named Sommerferien2013 and the Videofile eins.mkv I have many of them and sometimes there is .mkv or .avi or whatever. I would start with a find command? The output would be Sommerferien2013\Sommerferien2013.mkv Commented Mar 5, 2017 at 10:12
  • 1
    Imagine you're someone else and you come across this question. Would you say all the necessary information is available to give a good answer? Commented Mar 5, 2017 at 10:23
  • Okay thank you for explanation i tried to make it more precise. Please give feedback if the question is not better understandable Commented Mar 5, 2017 at 10:27
  • It's much better now. However, one thing remains unclear: Where does recursion fit in? Commented Mar 5, 2017 at 10:29
  • That was my "idea". Because the script has to look in the folder if there is a mkv or avi file and then rename with mv. I thought when there are many of this folders then this will be done recursively? Commented Mar 5, 2017 at 10:33

1 Answer 1

1

You can use this find command from base folder of your folders that contain *.avi and *.mkv files:

while IFS= read -rd '' f; do ( IFS=/ arr=($f) if [[ $f != *"/${arr[len-2]}"* ]]; then len=${#arr[@]} ext="${arr[len-1]##*.}" cd "$(dirname "$f")" && echo mv "${arr[len-1]}" "${arr[len-2]}.$ext" fi ) done < <(find . \( -name '*.mkv' -o -name '*.avi' \) -print0) 

When you're satisfied with the output remove echo before mv.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. How to use it? I create a .sh file in the folder containing all other folders? and paste it in the .sh file? I tried it and then executed $sh rename.sh. Following output came rename.sh: line 9: syntax error near unexpected token <' rename.sh: line 9: done < <(find . \( -name '*.mkv' -o -name '*.avi' \) -print0)'
Great! That gives me output when i execute it. what is the -o switch in the find command? If i intend to extend with .mp4 ? I dont find -o in the manpages of my OS. Edit: i found it. An OR Expression. :)
i removed the echo and i got this error mv: cannot stat sommer.mkv': No such file or directory` what can i do?
Look at the bash -x (debug mode) output. cd "$(dirname "$f")" takes care of changing directory into each directory before running mv command.
hi @anubhava, why did you remove the post how to call the script in a non bash shell?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.