1. #1

    Anyone with strong Linux CLI skills...

    ... Willing to take a crack at this? I am trying to rename a large amount of files. Each file is located in its own separate directory. The command I used was as follows:

    Code:
    find -type f -name '*.mp4' | rename -f 's/.*/clip.mp4/'
    The problem is that instead of renaming, it simply deletes , not ideal.

    Without the -f param I get the following output:

    ./00740/nqZNKM33BWDf4UJuwYfW53.mp4 not renamed: clip.mp4 already exists
    ./00393/clip.mp4 not renamed: clip.mp4 already exists
    ./00267/clip.mp4 not renamed: clip.mp4 already exists
    ./00580/Lb7986nGqRpBzMcWWQUvHd.mp4 not renamed: clip.mp4 already exists
    ./00403/clip.mp4 not renamed: clip.mp4 already exists
    ./00037/clip.mp4 not renamed: clip.mp4 already exists
    In the instances with the random file name such as in directory 00740 a file named clip.mp4 does NOT exist in that directory, so I assume its problem is that file clip.mp4 DOES exist somewhere within the top level directory overall... Any assistance would be very much appreciated, thanks!

  2. #2
    Deleted
    Quote Originally Posted by Applenazi View Post
    ... Willing to take a crack at this? I am trying to rename a large amount of files. Each file is located in its own separate directory. The command I used was as follows:

    Code:
    find -type f -name '*.mp4' | rename -f 's/.*/clip.mp4/'
    The problem is that instead of renaming, it simply deletes , not ideal.

    Without the -f param I get the following output:



    In the instances with the random file name such as in directory 00740 a file named clip.mp4 does NOT exist in that directory, so I assume its problem is that file clip.mp4 DOES exist somewhere within the top level directory overall... Any assistance would be very much appreciated, thanks!
    You are renaming all files to clip.mp4 in your current directory, i.e all renames overwrite the same file over and over again, like this:

    rename ./a/a.mp4 -> clip4.mp4
    rename ./b/b.mp4 -> clip4.mp4 <- overwrites the previously created clip4.mp4 (and removes the source since this is a rename)

    The end result of this (forced) rename is obviously one file named clip4.mp4 with the contents of the last file renamed and all other files effectively deleted.

    You need to include the directory path in your rename operation like this:

    Code:
     find -type f -name "*.mp4" | rename 's/(.*)\/.*/$1\/clip.mp4/'
    The (.*)\/ will match the path until the last / as expression $1 and then replace the remaining file name with clip4.mp4
    Note that if you have 2 files in the same directory they will still overwrite each other.

    If you add the -n option to the rename command it should show you what it would do.
    Last edited by mmoc1a2258818d; 2017-03-30 at 07:37 AM.

  3. #3
    Quote Originally Posted by lloewe View Post
    ....
    Worked flawlessly, thanks so much for your assistance. Super impressed with how easy it seemed for you to debug that, thanks again

  4. #4
    Deleted
    Quote Originally Posted by Applenazi View Post
    Worked flawlessly, thanks so much for your assistance. Super impressed with how easy it seemed for you to debug that, thanks again
    You're welcome.

    Paying attention to the error messages often helps a lot. In this case the overwrite warning is a good hint to look at the file names and the missing directory part of clip4.mp3 (while the first file has it) makes it fairly easy to spot the problem. The harder part is figuring out the correct regular expression

    Quite often Linux command line tools come with a verbose option (usually -v) that gives more info, also the --help option and "man <program>" can be quite helpful.

  5. #5
    Quote Originally Posted by lloewe View Post
    You're welcome.

    Paying attention to the error messages often helps a lot. In this case the overwrite warning is a good hint to look at the file names and the missing directory part of clip4.mp3 (while the first file has it) makes it fairly easy to spot the problem. The harder part is figuring out the correct regular expression

    Quite often Linux command line tools come with a verbose option (usually -v) that gives more info, also the --help option and "man <program>" can be quite helpful.
    I do a dry run with any bulk change that requires RegEx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •