67

I have multiple files in a directory, example: linux_file1.mp4, linux_file2.mp4 and so on. How do I move these files, using shell, so that the names are file1.mp4, file2.mp4 and so on. I have about 30 files that I want to move to the new name.

jww
  • 97,681
  • 90
  • 411
  • 885
elzwhere
  • 869
  • 1
  • 6
  • 13

4 Answers4

116

I like mmv for this kind of thing

mmv 'linux_*' '#1'

But you can also use rename. Be aware that there are commonly two rename commands with very different syntax. One is written in Perl, the other is distributed with util-linux, so I distinguish them as "perl rename" and "util rename" below.

With Perl rename:

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

As cweiske correctly pointed out.

With util rename:

rename linux_ '' linux_*.mp4

How can you tell which rename you have? Try running rename -V; if your version is util rename it will print the version number and if it is perl rename it will harmlessly report and unknown option and show usage.

If you don't have either rename or mmv and don't want to or can't install them you can still accomplish this with plain old shell code:

for file in linux_*.mp4 ; do mv "$file" "${file#linux_}" ; done

This syntax will work with any POSIX sh conforming to XPG4 or later, which is essentially all shells these days.

sorpigal
  • 25,504
  • 8
  • 57
  • 75
  • The link provided is 404. – xApple Mar 19 '13 at 12:10
  • 1
    Can you explain this syntax: ${file#linux_}, I have a similar situation except I want to change *_32.png to *_24.png – catalyst294 Oct 08 '14 at 19:38
  • 2
    @catalyst294: Shell variables can be expanded with `$foo` or `${foo}` (these are equivalent), and this is called parameter expansion, but the version with curly braces allows for modification of the value at the time of expansion, and this is called [parameter substitution](http://pubs.opengroup.org/onlinepubs/009604599/utilities/xcu_chap02.html#tag_02_06_02). The [bash](http://tldp.org/LDP/abs/html/parameter-substitution.html) implementation includes substitutions not required by the standard. Please remember to **always full-quote your variable expansions**, or suffer unexpected results! – sorpigal Oct 09 '14 at 19:48
  • What package usually provides `mmv`? – Alexej Magura Nov 08 '16 at 16:16
  • @AlexejMagura: Debian provides it in a package also named `mmv`. I cannot comment on other distributions. – sorpigal Nov 08 '16 at 22:53
  • Thank you for `rename` (which was already installed on Slackware) and the explanation about the name clash! `man rename` was also helpful. – DaveGauer Aug 17 '22 at 22:00
18
$ rename 's/linux_//' linux_*.mp4
cweiske
  • 30,033
  • 14
  • 133
  • 194
6

A simple native way to do it, with directory traversal:

find -type f | xargs -I {} mv {} {}.txt

Will rename every file in place adding extension .txt at the end.

And a more general cool way with parallelization:

find -name "file*.p" | parallel 'f="{}" ; mv -- {} ${f:0:10}trump${f:4}'
dtrckd
  • 657
  • 7
  • 17
5

I was able to achieve replace filenames within directories by combining @dtrckd and @Sorpigal answers.

for file in `find -name "linux_*.mp4"`; do mv "$file" "${file/linux_/}" ; done
Mrinal Saurabh
  • 948
  • 11
  • 16