linux - Copy file names to another folder

07
2014-07
  • problemofficer

    I need to copy file names from one folder to another. File contents are never changed. No files are overwritten, moved or copied. It's only about file names.

    I have a folder A with files named:

    • Show - S01E01 - Descriptive title.mkv
    • Show - S01E02 - Another descriptive title?.mkv
    • Show - S01E03 - Another complex name!.mkv
    • ...

    I have a folder B with files named:

    • show.s01e01.mkv
    • show.s01e02.mkv
    • show.s01e03.mkv
    • ...

    I don't need the files in folder A anymore because the files in folder B have a better quality. But I don't want to manually rename all files in folder B to have the proper file name including the episode title. I would like to automatically transfer the proper file names from the files in folder A to the files in folder B. The alphabetical order is identical in both folders. So the nth file in folder B corresponds to the nth file in folder A. In the end folder B would contain exactly the same file names as in folder A but the files contents are different of course.

    Example:

    Before:

    Folder A:

    • Show - S01E01 - Green Apples.mkv
    • Show - S01E02 - Peaches in the Sky.mkv

    Folder B:

    • show.s01e01.mkv
    • show.s01e02.mkv

    After:

    Folder A:

    • Show - S01E01 - Green Apples.mkv
    • Show - S01E02 - Peaches in the Sky.mkv

    Folder B:

    • Show - S01E01 - Green Apples.mkv
    • Show - S01E02 - Peaches in the Sky.mkv

    How do i do this in Bash? (Or any other GNU/Linux tool, KDE OK too)

  • Answers
  • Paul

    To do what you want would take some scripting. Not too involved, but it is re-inventing the wheel a bit.

    What I would suggest is installing tvnamer https://github.com/dbr/tvnamer

    This will use a database to consistently rename all of your media. If you run this across everything then both the low and high quality shows will have the same name, so you can just copy over the high quality ones over the low quality ones with cp.

  • jaychris

    You can try bash arrays. I am giving a skeletal solution below.

    cd $DIR1
    list1=( * )
    
    cd $DIR2
    i=0
    for f in *
    do
      mv $f ${list1[$i]}
      (( i += 1 ))
    done
    

    As you have mentioned in the question the assumption is that exact number of files are in both directories and alphabetically they are in order. More error checking and may be pattern matching can be done to make it robust.

    list1 is an array with the filenames in that directory. ${list1[0]} is the first element in the array, etc. I am not sure if this will work with filenames with whitespaces, etc.


  • Related Question

    linux - How to recursively rename files/folders to make their names Windows-friendly?
  • romkyns

    I have a bunch of files on a Ubuntu box, which have various characters in their filenames that Windows doesn't accept (mostly ":" and "*", but possibly others).

    What's the simplest way to get these all renamed and moved to a Windows machine? It's OK to replace these characters with something like "[colon]" and "[asterisk]".


  • Related Answers