Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some files named like these:

63018933.mp4?token=1325697436_b0c3e70c6e339380b4a484c576a8c287

63808488.mp4?token=1325697401_4ae5f7a68d93873c8881b303e7655e14

How do I rename all them to, for example 63018933.mp4(remove characters after mp4)?

share|improve this question
up vote 11 down vote accepted

This could be one way:

for file in *.mp4\?token*; do mv --no-clobber "$file" "${file%%\?*}"; done
share|improve this answer
1  
you might want to add -n or --no-clobber to mv if there's any chance that one of the mp4's might have the same name. Doesn't seem likely, but it costs nothing. – bsd Jan 11 '12 at 15:25
    
@bdowning: Thanks, added to the answer. – Birei Jan 11 '12 at 15:28

If you have the rename(1) tool (which you do if you have Debian or a Debian-based Linux, including Ubuntu and derivatives):

rename -n 's/\.mp4.*$/.mp4/' *mp4*

Once you're convinced you have the right pattern, just remove the -n (dry-run) and let it run properly.

rename(1) will apply a Perl regular expression to the filenames given it.

Note that on most other Linux distributions, rename is a different file renaming program, which doesn't help for this particular renaming pattern.

share|improve this answer

There is also mmv:

mmv -n "*.mp4*" "#1.mp4"

Remove the "-n" when output looks right.

share|improve this answer

protected by Community Oct 4 '15 at 18:07

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.