I have a lot of files in a directory that are named as follows ID_OTHER_STUFF.txt I'd like to rename them all to ID.txt. This isn't a duplicate because I don't know how to specify this pattern.
Asked
Active
Viewed 2.7k times
8
-
You can't rename them all to the same name in the same directory - you would have to move them all to different directories. – Graeme Feb 24 '14 at 19:28
-
moving them all to a different directory is fine, thanks – Walrus the Cat Feb 24 '14 at 19:29
-
This also appears to be a duplicate: http://unix.stackexchange.com/questions/24761/rename-all-files-with-a-certain-name – slm Feb 24 '14 at 21:02
-
Can you please give sample input/output for your command? – rahmu Feb 24 '14 at 22:43
-
1possible duplicate of Batch renaming files – Gilles 'SO- stop being evil' Feb 24 '14 at 23:47
-
it's "unclear what i'm asking" after i've accepted an answer that addresses exactly what i'm asking? great policework.... – Walrus the Cat Feb 28 '14 at 19:03
4 Answers
15
Using the perl
script prename
, which is symlinked to rename
on Debian based distros.
rename -n 's/^([^_]*)_.*/$1.txt/' *_*.txt
Remove the -n
once you are sure it does what you want.

Graeme
- 34,027
-
BTW obviously the ID part is a stand-in for the number. Rather than putting them in different directories according to ID, could we put them all in one directory, but the files just named ID? This answer is very good thank you. – Walrus the Cat Feb 24 '14 at 19:46
-
@Walrus, should do the job now, after a quick perl vs regex learning session for me. – Graeme Feb 24 '14 at 19:55
-
3Becareful w/ the
rename
command there's the Perl script version (Debian/Ubuntu) and an actual executable (Red Hat distros). http://unix.stackexchange.com/questions/24761/rename-all-files-with-a-certain-name. – slm Feb 24 '14 at 21:03
4
You want to use your shell's parameter expansion to manipulate the name, like this:
for file in *.txt
do
mv $file ${file/_*/}.txt
done
E.g.:
$ ls -1
1_foo_bar.txt
2_foo_bar.txt
$ for file in *.txt
for> do
for> mv $file ${file/_*/}.txt
for> done
$ ls -1
1.txt
2.txt
Look up Parameter Expansion in your shell's man page for more details about ${name/pattern/repl}
usage.

bahamat
- 39,666
- 4
- 75
- 104
1
Suppose your files are:
1_other_stuff.txt 2_other_stuff.txt 3_other_stuff.txt
You could do:
$ seq 1 3 | xargs -I ID mv ID_other_stuff.txt ID.txt

sandyp
- 1,177
-
Wont work,
mv
only takes more that 2 arguments when the last one is a directory. Then it puts the files in that directory. Usingmv
requires one run per file. You can do this in a loop or userename
which is the standard tool for doing this kind of thing. – Graeme Feb 24 '14 at 20:12 -
-
0
You want to mv
them in a loop, mini bash script, there are several variations. in this Question Batch renaming files.
-
I'm looking for a line of code that turns that particular pattern into the other specified pattern, thanks. – Walrus the Cat Feb 24 '14 at 19:44
-
all of the answers in that post can be written on one line, and contain the fundamental principles behind both of the other two answers, given to your question here. – X Tian Feb 24 '14 at 19:49