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
    What happens in the situation of another file named File1_2345.R.1234? Why does the command need to be on a single line? – Jeff Schaller Aug 14 '18 at 1:00
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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