How to rename multiple files by removing the extension?

I have a number of tiff files named:

sw.001.tif
sw.002.tif
...

and I want to remove the .tif at the end of each of the files. How can I use the rename command to do this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

perl‘s rename (as typically found on Debian where it’s also called prename), or this derivative (rename package on Debian):

rename 's/.tif$//' *.tif

util-linux rename (as typically found on Red Hat, rename.ul on Debian):

rename -- .tif '' *.tif

(note that that one would rename blah.tiffany.tif to blahfany.tif)

Method 2

For a non-rename, you might do:

$ for i in *.tif; do mv -i $i `basename $i .tif`; done

(-i to warn against replacing a file)

Method 3

rename -- .oldext .newext *.oldext

This substitutes the old extension with the new one. To simply remove the extension you can explicitly pass in an empty string as an argument.

rename -- .gz.tmp  '' *.gz.tmp

With the above command all files with .gz.tmp extension in the current folder will be renamed to filename.gz.

Refer to the article : Linux: remove file extensions for multiple files for details.

Method 4

If you use IBM AIX you won’t have a rename command, so in order to batch remove file extensions you’ll have to use plain vanilla System V UNIX commands:

for file in *.tif; do
   mv $file `echo $file | sed 's/.tif$//'`;
done;

Method 5

perl-rename 's/.tif//' *.tif

Use -n for dry run.

Method 6

can we make this recursive

for i in *.gz; do mv -i $i `basename $i .gz`; done


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x