Linux change file extensions

HowTo: Unix / Linux Rename File Extension From .OLD to .NEW

To rename a file called resume.docz to resume.doc, run:

To rename file extension from .txt to .doc, enter:

To fix the extension of all your .txt files, enter::

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

The above command will rename all *.txt files to *.doc i.e. fix the extension of your txt files.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

How to rename multiple file with a single cmd …

“rename .txt .doc *.txt” was great command . Thank you

I have missed up my photos directory,
how can I rename all files
07.jpg.13 07.jpg.14 07.jpg.15 ……..

to the correct format
07-13.jpg 07-14.jpg 07-15.jpg …..

Источник

Linux Rename Multiple Files Extension

Linux Tutorials

We can use mv command to change the file name. We can use it to change the file extension too. But, it works with a single file only and it doesn’t take wild characters.

We can create a shell script to change the extension of multiple files at once.

Linux Shell Script to Change Extension of Multiple Files

Let’s look at the script code where we will use the mv command in a for loop to change the extension of all the files in the current directory.

Usage: multimove.sh doc txt (to change all .doc to .txt)

Testing the Rename Shell Script

Below is the sample output from the above program execution.

Script Assumptions and Limitations

  1. The files have only one period (.)
  2. It loops through all files in the current directory only. However, you can extend it to look for files in the child directories also.
  3. Whitespaces in the file name can cause a problem with the script. It has worked on my system with filenames having spaces but I can’t guarantee that it will work for you too.

Further Readings: Linux mv command

Crontab example — every 5 minutes

I love Open Source technologies and writing about my experience about them is my passion.

Leave a Reply Cancel reply

Newsletter for You

JournalDev is one of the most popular websites for Java, Python, Android, and related technical articles. Our tutorials are regularly updated, error-free, and complete. Every month millions of developers like you visit JournalDev to read our tutorials.

JournalDev was founded by Pankaj Kumar in 2010 to share his experience and learnings with the whole world. He loves Open source technologies and writing on JournalDev has become his passion.

Источник

How to Use Bash to Change the File Extension of Multiple Files in a Folder

Method 1: Bash loop

The most common way to change file extensions recursively in a directory is to use a bash for loop. We can prompt the user to enter the target directory, old extension, and the new extension to rename using a bash script.

Step 1: Assemble the script
Let us begin assembling the script. The first part we need is to get the target directory. For this, we can bash read as:

#!/bin/bash
echo «Enter the target directory »
read target_dir
cd $target_dir

echo «Enter the file extension to search without a dot»
read old_ext

echo «Enter the new file extension to rename to without a dot»
read new_ext
echo » $target_dir , $old_ext , $new_ext «

The script above will ask the user for the directory to process and then cd into the set directory.

Next, we get the old extension without the dot (.); finally, we get the new extension to rename the files.

Now let us get to processing the files. For this; we can implement a simple rule that recursively searches the files as:

The for loop above will search the passed directory for all files with the old extension and rename them to the new extension.

To get verbose, we use the mv command with -v. You can suppress this output by replacing the -v flag with –

Step 2: Run the script
Now, let us put the script to the test. The final script is below:

#!/bin/bash
echo «Enter the target directory »
read target_dir
cd $target_dir

echo «Enter the file extension to search without a dot»
read old_ext

echo «Enter the new file extension to rename to without a dot»
read new_ext

echo » $target_dir , $old_ext , $new_ext «

for file in * . $old_ext
do
mv -v » $file » » $ . $new_ext »
done ;

In this test, we will use the /var/log directory and rename all the .log files to .bak. Here are the contents of the directory before running the script.

Now, let us run the script.

The screenshot above shows the script processing the files and renaming all the files with .log to .bak.

Since this is an interactive script, it comes in handy when you do not want to hard code the extension.

The following is the contents of the /var/log directory after the script.

To revert the changes, switch the old extension to .bak and the new extension as .log

Method 2: Rename command

If you do not feel like working with a script, you can use the rename tool to change the file extensions recursively.

To install rename, use the command:

Once installed, you can use the rename command as:

To revert the changes, change the .bak to .log and vice versa.

Method 3: MMV command

You can also use the mmv command that allows you to move multiple files simultaneously. Install mmv with the command:

To rename files with mmv command:

The #1 moves the files to the current directory. Once you run the command, it will rename all .log files to the specified extension.

Summing Up

This article discussed various methods you can recursively rename file extensions in a specific directory. However, it is good to note that you can implement strategies other than those discussed in this guide.

Thank you for reading, and remember to share!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

How to change the default application for a type of file on Linux

This howto explains how to change the default program to open all files with a given extension on Linux Mint, Ubuntu, Debian, and most Linux distributions. It also details how to change the default application for a batch of filetypes, for example all audio files or all video files.

Set the default program for a given filetype

1. In Nemo / Nautilus / Caja, right-click on any file with the desired file type or extension, choose “Properties” from the context menu.

2. The “Properties” dialog appears. Click on the “Open With” tab.

3. Select the desired application for the given filetype. All files with the same extension will now be opened with this program by default.

On Linux Mint, replace step one and two choosing “Open With” → “Other Application…” in the contextual menu, as seen in the picture above.

Change the default app for multiple filetypes

Changing the default application for one type of file is really easy, while changing a batch of file type associations reveals a little clumsier, but extremely efficient.

These instructions should work with a large spectrum of Linux flavours, please share your experience with your favorite distribution in the comments.

Associate all audio and video files to VLC instead of Totem media player (Movie Player)

Video files: .avi .mp4 .mpg .ogv .ogm .mkv .wmv etc.
Audo files: .mp3 .ogg .flac .wav .wma etc.

Open defaults.list with gedit:
gksudo gedit /usr/share/applications/defaults.list

And replace all occurrences of totem with banshee/rythmbox/vlc or the media player of your choice.
(Search → Replace → Replace All)

Save the modified file, and you’re done! Change are effective immediately.

Associate all office documents to LibreOffice instead of OpenOffice.org or Abiword

Office files: .odt .ods. .doc .docx .xls .xlsx etc.

Open defaults.list with gedit:
gksudo gedit /usr/share/applications/defaults.list

And replace all occurences of “openoffice.org” with “libreoffice”.
(Search → Replace → Replace All)

Finally, save the file. No need to restart, you’re all set!

Other file associations

Use the same technique to change the default application for all kind of file types or extensions, for example:

  • all pictures (.jpg, .png, .gif, etc.) should open up with gThumb instead of EOG/Eye of Gnome/Image Viewer
  • all html documents should open with Firefox instead of Chrome
  • all pdf files should open with Adobe Reader instead of Evince/Document Viewer.

Different solution for some Ubuntu versions

For an obscure reason, changing the file associations directly in the defaults.list doesn’t seem to work on some Ubuntu versions. Nevertheless, importing information to the the mimeapps.list works.

The file associations for each user are stored in:

In Ubuntu this file is almost empty. Let’s import informations from
/usr/share/applications/defaults.list

Display all the video MIME types / Media types associations with this command:
cat /usr/share/applications/defaults.list | grep video

Append all the lines containing video types to the local file:
cat /usr/share/applications/defaults.list | grep video >>

Open defaults.list with gedit:
gedit

And replace all occurrences of totem with vlc (Search → Replace…) Proceed similarly for audio files if needed. Unlike the standard method described at the beginning of this article, this method permits to assign a separate program to audio or to video files.

By Johannes Eva, January 2011 – November 2019

This article has been linked on LXer.com, Linux Today and some more…

  • How to install LibreOffice 7.2 on Linux Mint,… This article describes how to install LibreOffice 7.2 on Debian/Ubuntu-based Linux distributions, such as Linux Mint,…
  • Multimedia, codecs, MP3 & DVD support on CentOS,… This tutorial details how to install full multimedia support, media codecs, MP3 & DVD support on…
  • Sudo on CentOS / Scientific Linux / RHEL This short howto is about setting up sudo on Red Hat Entreprise Linux and its derivates…
  • Install VirtualBox on CentOS / RHEL / Scientific Linux This how-to is about installing VirtualBox on CentOS. Provided instructions should also work on Scientific Linux…
  • Install Flash Player directly from Adobe on Linux… Adobe Flash Player freezes constantly on my Ubuntu installation, sometimes crashing Firefox. Most of the time…
  • How to edit image metadata on Linux Image metadata is a complex field with multiple standards, the most important among them being Exif,…
  • How to Sync Fonts Between Linux Devices This small guide is about syncing fonts between two or more Linux devices, for example one…

3 Comments

I have .wma audio files showing as text files. It doesn’t help to use “open with” a media player because Mint thinks they’re text docs so nothing happens. Weirdly, one song in one folder IS still an audio file – and plays fine. Is there a global way to wipe out file associations and then re-associate via terminal?

In my Debian Jessie system, the per user settings are at: $HOME/.config/mimeapps.list

Helped me setting PDF files to be opened with Atril instead of Gimp (Ubuntu MATE 14.04). Thank you.

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Источник

Читайте также:  Occt для windows x64
Оцените статью