How to delete a file extension

Updated: 06/30/2020 by Computer Hope
Tip

If a file has a file extension it's likely required. To delete a file extension because you want it hidden, use Windows to hide file extensions. For help with hiding and showing file extensions, see: How to view a computer file extension.

File list in explorer with name and file extension

A file extension is part of a file name and can be removed by renaming the file. Below are the steps and examples of how to delete file extension for each of the operating systems.

Windows users

Before a file extension can be deleted, you must have the show file extensions enabled. By default, this option is disabled.

Assuming the file is named myfile.txt, we remove its file extension by performing the following steps.

  1. Right-click the file (not the shortcut).
  2. Select Rename in the menu
  3. Erase the .txt from myfile.txt and press Enter.
  4. Click Yes on the warning about the file becoming unusable if you're sure you want to delete the file name extension.

Unusable file after deleting file extension.

Deleting multiple file extensions

To delete the file extensions of multiple files, we recommend you enter the Windows command line and follow the steps below.

MS-DOS and Windows command line users

Open an MS-DOS or Windows command line.

Change the directory where the file is located. In our example below, we rename the file "myfile.txt" to "myfile" to delete the file extension.

move myfile.txt myfile

Unless the file already exists, you'll get no message or an "OK" message indicating that the file was renamed and the file extension was removed.

  • See the move command page for more information about this command.

Delete multiple file extensions

To delete the file extension of all files with the same file extension, follow the steps below.

The example below we delete the file extension on all the files in the current directory that end with .txt.

ren *.txt *.
Tip

In our example, we are using an asterisk as a wildcard that tells the command line that we want every file name.

Unix and Linux users

In Unix-like operating systems such as Linux, you can use the mv command to rename a single file or directory.

To rename multiple files, you can use the rename utility.

To rename files recursively across subdirectories, you can use the find and rename commands together.

Rename a single file or directory

Change the directory to where the file is located. In our example below, we remove the file extension of "myfile.txt." Additionally, this example is done from the shell and not a GUI (graphical user interface).

mv myfile.txt myfile

If the operation is successful, you are returned to the shell prompt.

  • See the mv command page for more information on this command.

Rename multiple files

To rename multiple files, use the rename utility. On most systems, it is not installed by default, but can be installed with your package manager. In Ubuntu, Debian, or Linux Mint, it can be installed with apt.

sudo apt install rename

The general syntax of the rename command is as follows.

rename [ options... ] regexp files

The parameter regexp is a Perl regular expression. To substitute text, the regular expression form is 's/pattern1/pattern2/'.

The files parameter specifies which files are processed by the command. You can provide a list of files, or a wildcard to be expanded by the shell. The wildcard * specifies all files.

Useful options include -n (show what changes would be made, but change nothing), -v (show verbose output), and -f (force overwrite of new files if they exist).

For example, to remove the file extension .txt from all files in the current directory, you can run the following command.

rename -n 's/\.txt//' *

In the above command, the regexp parameter is enclosed in single quotes, to "protect" the string, preventing the shell from interpreting it. It is passed literally to the rename command.

In the regular expression, pattern1 is the file extension .txt, and pattern2 is an empty string, specifying that pattern1 should be replaced with nothing.

The backslash is an escape character, meaning "treat the next character literally." The period must be escaped, because if not escaped, it is interpreted by rename as a regular expression metacharacter that matches any character.

The -n option performs a dry run, displaying what changes would be made, but changing nothing. The output of the above command would look like this:

rename(file1.txt, file1)
rename(file2.txt, file2)
rename(file3.txt, file3)

If the new file names look correct, you can run the same command again without the -n option to perform the rename. Specify -v to see what files are being renamed.

rename -v 's/\.txt//' *
file1.txt renamed as file1
file2.txt renamed as file2
file3.txt renamed as file3

Rename multiple files, including subdirectories

To rename files including those in subdirectories, use the find command, and pipe its output to rename, which processes the list of files found.

For example, to remove the extension .txt from all files in the current directory, and in any subdirectories, run the following command.

find . -iname '*.txt' | rename -v 's/\.txt//'

The above command says: "Recursively find all files in the current directory and its subdirectories, whose name ends in .txt. Pipe those filenames to the rename command, and substitute .txt with nothing." Its output would look like the following.

Reading filenames from file handle (GLOB(0x55dda79131b8))
./dir1/file4.txt renamed as ./dir1/file4
./file1.txt renamed as ./file1
./file2.txt renamed as ./file2
./file3.txt renamed as ./file3
./dir2/file5.txt renamed as ./dir2/file5
  • See the find command page for more information on this command.