Rename multiple files in terminal [duplicate]

I have 40 mp4 files in a folder.

Every file starts with video_. Every file is of format video_*.mp4.

I need to rename all the files with video_ removed from the begining of every file. How can I do that from terminal?


Solution 1:

You can do it by a terminal command in a directory where these files are located.

rename 's/^video_//' *.mp4

That means select all filenames started with video_ and replace video_ with nothing. I guess s is for "substitute".

^ shows the beginning of string. If you omit ^, the first occurrence of video_ will be removed no matter where it is located in the string. But in your case it does not really matter.

Note: Ubuntu versions above 17.04 don't ship with rename package, however you can still install it from default repositories via sudo apt install rename

Solution 2:

  • Using rename (prename) :

    rename -n 's/^video_//' video_*.mp4
    

    If you are satisfies with the changes that are going to be made, remove -n to let the operation happens actually :

    rename 's/^video_//' video_*.mp4
    
  • Using bash parameter expansion :

    for file in video_*.mp4; do mv -i "$file" "${file#video_}"; done
    
    • ${file#video_} is the parameter expansion pattern to remove video_ from the start of the file names.

    Or

    for file in video_*.mp4; do mv -i "$file" "${file/video_/}"; done        
    
    • This one assumes video_ comes only once in file names

    • ${file/video_/} is a bash parameter expansion pattern that will replace video_ from file names with blank.