1

I have some files whose names contain multiple extensions:

$ ls -r
File1_345.R.12345
File1_3.234.R.6789
File1_2345.R.2345
File1_12345.R.12345
$

I want to rename them to remove all the existing extensions and replace them with .txt.  Output should be below:

$ ls -r
File1_345.txt
File1_3.txt
File1_2345.txt
File1_12345.txt

Is it possible to use find and xargs command?

1
  • 1
    What happens in the situation of another file named File1_2345.R.1234? Why does the command need to be on a single line? Commented Aug 14, 2018 at 1:00

3 Answers 3

7

If you want to remove all extensions (everything after the first dot) from each filename, do

$ for f in *
do
    mv -- "$f" "${f%%.*}.txt"
done

Of course, if you really want to do this in one line, just collapse the above to

$ for f in *; do mv -- "$f" "${f%%.*}.txt"; done
1

You can do it with GNU Parallel:

find ... | parallel mv {} '{=s/\..*//=}'.txt
0

For this kind of task, I would highly recommend using the rename tool (e.g. by Larry Wall in Ubuntu):

rename --verbose 's/\..*$/.txt/' File*
# replaces everything after the first dot.

or with a more specific regex: 's/\.R\.[0-9]+$/.txt/'

1
  • Be aware that there are two different commands named rename and they don't work the same. One is written in perl, the other is a GNU utility. Commented Jul 20, 2022 at 13:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.