Changing file extensions in linux

Changing all file’s extensions in a folder using CLI in Linux

How to change all file’s extensions in a folder using one command on CLI in Linux?

5 Answers 5

rename ‘s/.old$/.new/’ *.old

If you have the perl rename installed (there are different rename implementations) you can do something like this:

You could use a for-loop on the command line:

this will take all files with extension .old and rename them to .new

This should works on current directory AND sub-directories. It will rename all .oldExtension files under directory structure with a new extension.

Explanation: this recursively finds all files (-type f) starting from the current directory (.) and applies the move command (mv) to each of them. Note also the quotes around <>, so that filenames with spaces (and even newlines. ) are properly handled.

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

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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.

Читайте также:  Irbis nb61 установка windows 10 с флешки

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?

Читайте также:  Что делать если windows не может выполнить поиск обновлений

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.

Источник

Recursively rename files (change extension) in Linux

How do I rename all files in a directory, recursively, changing one file extension to another, for thousands of files in thousands of subfolders? I see a lot of commands that do almost what I want, but not quite.

7 Answers 7

of course remove the -v when actually doing it, or it will waste time displaying all the files

Remove the -n to actually perform the renaming.

Explanation

The above starts walking the directory tree starting at the current working directory ( . ). Every time a file name matches the pattern *.andnav (e.g., foo.andnav ) the following command is executed:

Where $0 is foo.andnav and $<0%.andnav>.tile replaces the .andnav suffix with .tile so basically:

I found this method is easier and easier to read:

At least on Ubuntu derivations rename takes a list of files from STDIN if none are on the command line. And this can be tested easily with:

until you get it right.

find -execdir rename

https://superuser.com/a/213146/128124 works directly only for suffixes, but this will work for arbitrary regex replacements on basenames:

or to affect files only:

-execdir first cd s into the directory before executing only on the basename.

Tested on Ubuntu 20.04, find 4.7.0, rename 1.10.

Convenient and safer helper for it

Sample usage to replace spaces ‘ ‘ with hyphens ‘-‘.

Dry run that shows what would be renamed to what without actually doing it:

Command explanation

The awesome -execdir option does a cd into the directory before executing the rename command, unlike -exec .

-depth ensure that the renaming happens first on children, and then on parents, to prevent potential problems with missing parent directories.

-execdir is required because rename does not play well with non-basename input paths, e.g. the following fails:

The PATH hacking is required because -execdir has one very annoying drawback: find is extremely opinionated and refuses to do anything with -execdir if you have any relative paths in your PATH environment variable, e.g. ./node_modules/.bin , failing with:

find: The relative path ‘./node_modules/.bin’ is included in the PATH environment variable, which is insecure in combination with the -execdir action of find. Please remove that entry from $PATH

-execdir is a GNU find extension to POSIX. rename is Perl based and comes from the rename package.

Rename lookahead workaround

If your input paths don’t come from find , or if you’ve had enough of the relative path annoyance, we can use some Perl lookahead to safely rename directories as in:

I haven’t found a convenient analogue for -execdir with xargs : Xargs: change working directory to file path before executing?

The sort -r is required to ensure that files come after their respective directories, since longer paths come after shorter ones with the same prefix.

Читайте также:  Windows failed to start перевод

Источник

How do I change extension of multiple files recursively from the command line?

I have many files with .abc extension and want to change them to .edefg
How to do this from command line ?

I have a root folder with many sub-folders, so the solution should work recursively.

9 Answers 9

A portable way (which will work on any POSIX compliant system):

In bash4, you can use globstar to get recursive globs (**):

The (perl) rename command in Ubuntu can rename files using perl regular expression syntax, which you can combine with globstar or find :

70k files file structure. at least on my shell it most definitely did not replace it.

This will do the required task if all the files are in the same folder

To rename the files recursively use this:

One problem with recursive renames is that whatever method you use to locate the files, it passes the whole path to rename , not just the file name. That makes it hard to do complex renames in nested folders.

I use find ‘s -execdir action to solve this problem. If you use -execdir instead of -exec , the specified command is run from the subdirectory containing the matched file. So, instead of passing the whole path to rename , it only passes ./filename . That makes it much easier to write the regex.

  • -type f means only look for files, not directories
  • -name ‘*.abc’ means only match filenames that end in .abc
  • ‘<>‘ is the placeholder that marks the place where -execdir will insert the found path. The single-quotes are required, to allow it to handle file names with spaces and shell characters.
  • The backslashes after -type and -name are the bash line-continuation character. I use them to make this example more readable, but they are not needed if you put your command all on one line.
  • However, the backslash at the end of the -execdir line is required. It is there to escape the semicolon, which terminates the command run by -execdir . Fun!

Explanation of the regex:

  • s/ start of the regex
  • \.\/ match the leading ./ that -execdir passes in. Use \ to escape the . and / metacharacters (note: this part vary depending on your version of find . See comment from user @apollo)
  • (.+) match the filename. The parentheses capture the match for later use
  • \.abc escape the dot, match the abc

$ anchor the match at the end of the string

/ marks the end of the «match» part of the regex, and the start of the «replace» part

version1_ add this text to every file name

  • $1 references the existing filename, because we captured it with parentheses. If you use multiple sets of parentheses in the «match» part, you can refer to them here using $2, $3, etc.
  • .abc the new file name will end in .abc. No need to escape the dot metacharacter here in the «replace» section
  • / end of the regex
  • Hint: rename ‘s -n option is useful. It does a dry run and shows you what names it will change, but does not make any changes.

    Источник

    Оцените статью