27

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?

3
  • 2
    Bear in mind that NO linux or unix filesystem uses Windows or VMS-style "extensions" - it is only by convention that a suffix of the file name is ".tif" or ".c" or ".o" or ".so" or whatever.
    – user732
    Mar 19, 2012 at 23:07
  • That's fine... I'm importing files from windows to linux :)
    – Paul
    Mar 19, 2012 at 23:14
  • 1
    Technically speaking Windows doesn't use file extensions either, it's just that Windows 95's file manager kept using the last characters of the filename to determine file type after breaking out of the MS-DOS naming scheme and the convention has since remained.
    – RAKK
    Feb 20, 2015 at 19:04

7 Answers 7

35

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)

4
  • 3
    I believe the "Debian" rename is by Larry Wall (comes with Perl?) and "Red Hat" one is from util-linux (rename.ul in Debian IIRC).
    – XTL
    Mar 20, 2012 at 14:19
  • @XTL: True. I just call them that because they're the "rename" programs found in Debian or Red Hat derivatives. Mar 20, 2012 at 14:39
  • Thanks so much! I never realized that there were different versions of 'rename'. I assume there are other subtle differences between the commands in debian & redhat. I'll have to lookup some of their differences.
    – Paul
    Mar 20, 2012 at 14:45
  • The second command resulted invalid syntax, but first one worked.
    – SmallChess
    Mar 2, 2020 at 5:54
17

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)

5
  • 27
    Safer faster version: for i in ./*.tif; do mv -i "$i" "${i%.tif}"; done
    – jw013
    Mar 20, 2012 at 14:13
  • 1
    @jw013 your comment should be a top-level answer.
    – Roland
    Jan 23, 2020 at 7:31
  • 1
    @jw013 Why ./*.tif instead of *.tif?
    – Pedro A
    Jul 4, 2021 at 18:03
  • 3
    @PedroA ./ is a pretty safe prefix to prevent file names from being misinterpreted, for example a file beginning with - may be interpreted as option. Example
    – jw013
    Jul 5, 2021 at 13:50
  • I went with the anwer from jw013, though I wanted to skip warnings for replacing (as that was my intention) so I've switched -i to -f (CentOS 6) to force overwrite.
    – LuxZg
    Jun 5, 2023 at 9:22
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.

2
  • 1
    That assumes the util-linux implementation of rename which is incompatible with (and much more limited than) the traditional rename command from perl. May 27, 2015 at 9:49
  • Also note that it replaces the first occurrence of .oldext within the file name, not necessarily the extension (foo.oldextasy.oldext would be renamed to footasy.oldext). May 27, 2015 at 9:50
3

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;
0
1
perl-rename 's/\.tif//' *.tif

Use -n for dry run.

1
  • This is a duplicate of an answer posted 9 years ago. Sep 5, 2021 at 1:53
0

A non rename method.

ls | grep .tif | xargs -I % sh -c 'mv % $(basename % .tif)'
-2

can we make this recursive

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .