Mass file rename in linux

How can I rename many files at once?

I need to rename a bunch of files in order to fix their sort order, therefore I need to be able to do a sort of «find and replace» so I can replace chunks of the filenames at a time. What tools should I use? I prefer GUI but a command line tool recommendation would also be fine.

16 Answers 16

There are a few but I recommend gprename which is a good compromise between usability and functionality.

Other tools are: rename, krename , pyrenamer , cuteRenamer, .

I have use pyrenamer often and sometimes gprename unless the change is very easily done on the command line. The best thing about these GUI’s is the built-in ‘preview’ action. This will save you headaches.

I really like qmv from the renameutils package. It enables you to use your favorite (terminal based) text editor to rename files. I prefer to invoke it with -f do which gives you a single column (one row per file) with filenames. Combined with the power of Vim it gives you all the tools you need to do massive filename editing.

may be a little difficult to handle, but really powerful!

If you like the shell and perl regular expressions I’d recommend rename . It’s as plain as it’s name.

thunar file manager is a GUI with a bulk rename option

it’s not standard on Gnome but can be installed through the software centre

If you are familiar with Emacs, I think nothing beats Dired for this task. Even if you don’t use Emacs that often you may find Dired a handy tool.

sudo aptitude install emacs23-nox

Start Emacs Dired mode for a directory:

Now enter edit directory mode:

C-x C-q (that is Ctrl+x followed by Ctrl+q)

You can now edit the filenames like editing text in every plain text editor. You may even chose to replace the filenames using regular expressions (note that unfortunately Emacs uses a different syntax than PCRE).

For example, to rename files with counter (starting by 1):
M-x replace-regexp (that is Alt+x followed by the string «replace-regexp» typed in the minibuffer at the bottom of the screen).
Replace regexp: DSCN\(3+\).JPG
Replace with: \,(format «P%04d.jpg» (1 + \#))

To reuse a counter in the filename:
M-x replace-regexp
Replace regexp: DSCN\(9+\).JPG
Replace with: \,(format «P%04d.jpg» (string-to-number \1))

C-c C-c (that is Ctrl+c followed by Ctrl+c)

Or alternatively press the following sequence to abort your changes:

C-c Esc (that is Ctrl+c followed by Esc)

I tend to use mmv, which is command-line based, and has a somewhat quirky expression syntax but tend to solve most of my problems.

If you use Nautilus and know how Nautilus Scripts work, you could use nautilus-renamer.

find -execdir rename

This renames files and directories with a regular expression affecting only basenames, for example:

PATH=/usr/bin find . -depth -execdir rename "s/^find/replace/" '<>' \; 
PATH=/usr/bin find . -type f -execdir rename "s/^find/replace/" '<>' \; 

On the command line theres also the possibility to use magic of sed and shell:

ls *.c | sed "s#\(.*\)\.c#mv \0 \1.cpp#" | sh 

That doesn’t seem to work as printed. You might also use ‘#’ or other character instead of «\» as separators in examples for possibly more clarity. e.g., ` ls .c | sed ‘s#([a-z])\.c#\1.cpp#’ ` (This example is not do the rename, just to display the new filename, for simplicity)

Читайте также:  Подключение usb linux ubuntu

+1 I find it a really nice way (that additionally works across all distros), without installing anything. But I would probably use the -d switch for ls to avoid listing content of directories which might also end with .c (most of the times not an issue, but still ..).

For me the pyRenamer worked the best. Nothing (sadly) comes close to Total Commanders built it renaming tool. pyRenamer doesn’t integrate with Krusader but considering the fact, that you don’t use the renaming tools that often anyway, pyRenamer is a very good option.

rnm file-name -ns new-filename # single file rnm ./* -ns '/n//i/' # files will be sorted and indexed. rnm ./* -rs '/search/new/g' # 'search' in filenames will be replaced with 'new' rnm ./* -ns '/fn//i/' -ss 'search' # only files/directories which contain 'search' in their name will be indexed (renamed). rnm ./* -ns '/fn//id/' -fo # file only mode, directories will be ignored. rnm ./* -ns '/fn//id/' -fo -dp -1 # recursive to subdirectories all the way. etc. 

Personally I use Ant Renamer with Wine because nothing I’ve tried (Métamorphose, PyRenamer, GPRename, KRename) is as powerful, easy to install and easy to use. And it’s still FOSS so I don’t see the problem.

Krename is a KDE GUI for renaming files, generally found in most package managers.

This answer applies to removing leading numbers that may be found in music files such as mp3s. But Krename does mass rename as well.

  1. Load selected files or folder. Krename loads all files in the selected folder, just click on Open without having to individually select all files.
  2. Go to the Destination tab to overwrite or copy files to another folder.
  3. Go to the Filename tab, then to the Advanced Filename tab.
  4. Start by removing the token $ in the Template window. A change takes place in the Renamed window below. No filenames appear, only the file extensions. 3. Image shows that the filenames have been removed, leaving the file extensions.
  5. Click on Insert Part of Filename. and select the parts of the filename you want to keep. This operation will include all the files and the changes will be obvious in the Renamed window below. 4.
  6. Click OK . The files are renamed without the leading numbers. 5. Final results showing.
  7. Finally, click Finished at the bottom right corner.

Источник

How to do a mass rename?

How can I do it? I understand that i need more than one mv command because they are at least 25000 files.

11 Answers 11

Easiest solution is to use «mmv»

mmv "long_name*.txt" "short_#1.txt" 

Where the «#1» is replaced by whatever is matched by the first wildcard. Similarly #2 is replaced by the second, etc.

mmv "index*_type*.txt" "t#2_i#1.txt" 

To rename index1_type9.txt to t9_i1.txt

mmv is not standard in many Linux distributions but is easily found on the net.

If this is your first time using mmv, it’s best to first try mmv -n , which will show you what it would do without rewriting anything.

This is a nice little tool. I like that it has alias forms for copying, linking, and even appending files together.

If you are using zsh you can also do this:

autoload zmv zmv 'transform.php?dappName=Test&transformer=YAML&v_id=(*)' '$1.txt' 

You write a fairly simple shell script in which the trickiest part is munging the name.

Читайте также:  Linux no screens found

The outline of the script is easy (bash syntax here):

for i in 'transform.php?dappName=Test&transformer=YAML&v_id='* do mv $i done 

Modifying the name has many options. I think the easiest is probably an awk one-liner like

for i in 'transform.php?dappName=Test&transformer=YAML&v_id='* do mv $i `echo $i | awk -F'=' ''`.txt done 

update

Okay, as pointed out below, this won’t necessarily work for a large enough list of files; the * will overrun the command line length limit. So, then you use:

$ find . -name 'transform.php?dappName=Test&transformer=YAML&v_id=*' -prune -print | while read do mv $reply `echo $reply | awk -F'=' ''`.txt done 

Maybe so, but not all UNIX systems (not on my Mac for example) and this is a general pattern that can be applied to other issues.

At least in Bash and Dash, a for loop over a glob (or any builtin, at that) is not subject to «command line too long» limitation, so your first approach would also work for a very long list of files; see this answer.

Or you could pipe the results of an ls into a perl regex.

@8jean: As I understand the question the result should be: s/.*?v_id=(.*)/$1.txt/. I don’t see that in your comment.

You may use whatever you want to transform the name (perl, sed, awk, etc.). I’ll use a python one-liner:

for file in 'transform.php?dappName=Test&transformer=YAML&v_id='*; do mv $file `echo $file | python -c "print raw_input().split('=')[-1]"`.txt; done 

Here’s the same script entirely in Python:

import glob, os PATTERN="transform.php?dappName=Test&transformer=YAML&v_id=*" for filename in glob.iglob(PATTERN): newname = filename.split('=')[-1] + ".txt" print filename, '==>', newname os.rename(filename, newname) 

Side note: you would have had an easier life saving the pages with the right name while grabbing them.

Источник

Mass file rename in Linux

I’m trying to rename a lot of files in Kali Linux, nearly 16,000 of them. They’re arranged in nearly 600 folders with random numbers for names, and the files in them are labelled as 0,1,2. (No file extn.) starting from 0 again in each new folder. I’m trying to convert the files into a .jpg format. The following code works only when I open the folder directory in terminal-

 find . -name '368' -exec sh -c 'mv $0 $0.jpg' <> \; find . -name '99' -exec sh -c 'mv $0 $0.jpg' <> \; find . -name '4' -exec sh -c 'mv $0 $0.jpg' <> \; 

But to do this for each and every folder in the directory is going to be rather tedious. Any suggestions for a .sh executable script? Edit: The extension has to be changed, the names, can remain as they are.

Write a script that loops for each folder found, then renames all the object in that folder, moves onto the next folder and does the same. Linux also has inbuilt rename functions you can access from the GUI. Select all the folders and hit rename.

I did get the general idea of what I would have to do, but unfortunately, I’ve just started with Linux, and am not sure how to proceed.

@Anaklusmosismyfavoriteweapon: Your Windows thinks those files are now JPEGs, but they still are the same files as before, with a different name. An application opening them and expecting JPEG format will a) fail, or b) ignore the name and read them as what they actually are. Either way, your files are NOT magically JPEGs now just by renaming.

Источник

How do I easily rename multiple files using command line?

But in Ubuntu/Nautilus, I can’t tab to next file. But being on Linux, I think there must be a command line alternative. However, sometimes, I may want more control over how to rename specific files. In that case, perhaps its better to be able to tab to the next file

Читайте также:  Linux zip folder with subfolders

I think this question is not answered. What you are asking for (F2 and then jump in F2 mode to the next file) is currently not available I think in Nautilus.

8 Answers 8

I use rename all the time. It is pretty simple, but hopefully you know basic regex:

This will replace the string SEARCH with REPLACE in every file (that is, * ). The /g means global, so if you had a SEARCH_SEARCH.jpg , it would be renamed REPLACE_REPLACE.jpg . If you didn’t have /g , it would have only done substitution once, and thus now named REPLACE_SEARCH.jpg . If you want case-insensitive, add /i (that would be, /gi or /ig at the end).

With regular expressions, you can do lots more.

Note that this rename is the prename (aka Perl rename ) command, which supports complete Perl regular expressions. There is another rename which uses patterns, and is not as powerful. prename used to be installed by default on Ubuntu (along with Perl), but now you may have to do:

Prefix

Also you can remove unwanted strings. Let’s say you had 20 MP3 files named like CD RIP 01 Song.mp3 and you wanted to remove the «CD RIP» part, and you wanted to remove that from all of them with one command.

Notice the extra space in ‘^CD RIP ‘ , without the space all files would have a space as the first character of the file. Also note, this will work without the ^ character, but would match CD RIP in any part of the filename. The ^ guarantees it only removes the characters if they are the beginning of the file.

Suffix

will change Something.pdf into Something.doc . (The reason for the backslash is, . is a wildcard character in regexp so .pdf matches qPDF whereas \.pdf only matches the exact string .pdf . Also very important to note, if you are not familiar with BASH, you must put backslashes in SINGLE quotes! You may not omit quotes or use double quotes, or bash will try to translate them. To bash \. and «\.» equals . . (But double-quotes and backslashes are used, for example «\n» for a newline, but since «\.» isn’t a valid back escape sequence, it translates into . )

Actually, you can even enclose the parts of the string in quotes instead of the whole: ‘s/Search/Replace/g’ is the same as s/’Search’/’Replace’/g and s/Search/Replace/g to BASH. You just have to be careful about special characters (and spaces).

I suggest using the -n option when you are not positive you have the correct regular expressions. It shows what would be renamed, then exits without doing it. For example:

This will list all changes it would have made, had you not put the -n flag there. If it looks good, press Up to go back, then erase the -n and press Enter (or replace it with -v to output all changes it makes).

Note: Ubuntu versions above 17.04 don’t ship with rename by default, however it’s still available in the repositories. Use sudo apt install rename to install it

Источник

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