3

I have a folder called movies on my Ubuntu, which contains many subfolders.

Each subfolder contains 1 mp4 file and may contain other files (jpg, srt).

Each subfolder has the same title format:

My Subfolder 1 (2001) Bla Bla
My Subfolder 2 (2000) Bla
My Subfolder 3 (1999)

How can I rename the mp4 files same as parent folder but without the year and the blabla?

For example, the mp4s inside the subfolders above become :

My Subfolder 1.mp4
My Subfolder 2.mp4
My Subfolder 3.mp4

I want the mp4s to stay in their subfolder, just their name will be changed. The year is always in parentheses.

0

2 Answers 2

1

Here's a bash solution:

cd movies
for mp4 in */*.mp4
do
  if [[ $mp4 =~ ^(.*)\ \( ]]
  then
    echo mv -- "$mp4" ...to... "${mp4%%/*}/${BASH_REMATCH[1]}".mp4
  fi
done

This loops over every mp4 file in every subdirectory of "movies" and applies a pattern-matching test to it. If it matches:

  • ^ - from the beginning
  • (.*) - capture any number of characters and save them off
  • \ \( - followed by a space and an open parenthesis

If that match succeeds, then we've found an mp4 file in a directory that has the pattern you're expecting. Bash saves the parenthesized matches in the $BASH_REMATCH array variable, so we (would) call mv with the original filename and a pieced-together new name:

  • ${mp4%%/*} is the original directory name
  • // - directory separator
  • ${BASH_REMATCH[1]}".mp4 - the saved portion from above, suffixed with .mp4

If the results look correct, remove the echo and ...to... portions.

2
  • Thank you! @Jeff What tweak can I add for it to handle also Folders that don't have (year) or other text after the name ?
    – Hamza
    Commented Aug 21, 2019 at 11:35
  • It would be very similar, but since it's different from what was asked in this question, I'm going to suggest (since questions are free!) that you ask a new question, perhaps linking back to this one for history. Be sure to spell out exactly which files should be matched and which should not be (if any)!
    – Jeff Schaller
    Commented Aug 21, 2019 at 12:13
1

Using the perl rename utility:

rename 's/\(\d\d\d\d\)//' */*.mp4

If you want a dry-run to see what it would do, use the -n (aka --nono) option:

rename -n 's/\(\d\d\d\d\)//' */*.mp4

On Debian (and Ubuntu, etc), the perl-based rename is in the file-rename package. It may also be invoked as prename or file-rename.

BTW, one very useful feature of this version of rename is that while it's trivially easy to do sed or tr style transformations on filenames, you can actually use ANY perl code that modifies $_ to rename files, up to an including complex scripts.

From the man page:

rename renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input.

For example, to rename all files matching "*.bak" to strip the extension, you might say

rename 's/\.bak$//' *.bak

To translate uppercase names to lower, you'd use

rename 'y/A-Z/a-z/' *

Note that there are other versions of rename with completely different command-line options and arguments, including one in the util-linux package (often named /usr/bin/rename.ul). It's important to verify which version of rename you have installed before using it - or, more accurately, which one is called or sym-linked to /usr/bin/rename

On Debian and Ubuntu and related systems, you can check with update-alternatives:

# update-alternatives --display rename
rename - auto mode
  link best version is /usr/bin/file-rename
  link currently points to /usr/bin/file-rename
  link rename is /usr/bin/rename
  slave rename.1.gz is /usr/share/man/man1/rename.1.gz
/usr/bin/file-rename - priority 70
  slave rename.1.gz: /usr/share/man/man1/file-rename.1p.gz
2
  • I only got Folders name changed, mp4 files remained the same. And Folders with other text after (year) still remained.
    – Hamza
    Commented Aug 21, 2019 at 11:15
  • do your directories end in .mp4? if not, then there's no way the example rename commands I gave would rename directories. If directories as well as files can end in .mp4, then try changing the rename command to something like: rename -e '-f $_ && s/\(\d\d\d\d\)//' */*.mp4, or use rename's -d option.
    – cas
    Commented Aug 21, 2019 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .