首页 > 系统相关 >Rename – A Command Line Tool For Renaming Multiple Files in Linux

Rename – A Command Line Tool For Renaming Multiple Files in Linux

时间:2024-05-25 10:56:00浏览次数:26  
标签:Rename Files Multiple rename html command Linux php files

from: https://www.tecmint.com/rename-multiple-files-in-linux/

We often use the mv command to rename a single file in Linux. However, renaming multiple or groups of files quickly makes it a very difficult task in a terminal.

Linux comes with a very powerful built-in tool called rename, which is used to rename multiple files or groups of files, convert filenames to lowercase, convert filenames to uppercase, and overwrite files using Perl expressions.

This article will guide you through the basics of using rename to efficiently rename multiple files in Linux.

What is Rename?

rename is a command line utility that allows you to rename multiple files at once using regular expressions, which are patterns used to match character combinations in strings. This tool is particularly useful for batch renaming files based on specific patterns or rules.

The rename command is part of a Perl script and it resides under /usr/bin/ on many Linux distributions.

You can run the which command to find out the location of the rename command.

which rename

/usr/bin/rename

Basic Syntax of Rename Command

The basic syntax of the rename command is:

rename 's/old_pattern/new_pattern/' files

Here is the breakdown of the command:

  • s/old_pattern/new_pattern/: This is the substitution command used by rename, that tells rename to replace the old_pattern with the new_pattern.
  • files: This specifies the files you want to rename.

The rename command also comes with a few optional arguments along with a mandatory perl expression that guides the rename command to do actual work.

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
  • -v: Print names of files successfully renamed.
  • -n: Show what files would have been renamed.
  • -f: Force overwrites existing files.
  • perlexpr: Perl Expression.

For better understanding of the rename utility, we’ve discussed a few practical examples of this command in the article.

Installing Rename in Linux

Before using rename, you need to ensure it is installed on your system by running the following command.

rename --version

If it is not installed, you can install it using your package manager as shown.

sudo apt install rename          [On Debian, Ubuntu and Mint]
sudo yum install prename         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/rename   [On Gentoo Linux]
sudo apk add rename              [On Alpine Linux]
sudo pacman -S rename            [On Arch Linux]
sudo zypper install rename       [On OpenSUSE]    
sudo pkg install rename          [On FreeBSD]

1. Changing File Extensions in Linux

Suppose you have a bunch of files with the ".html" extension and you want to rename all ".html" files to ".php" at once.

To do so, first change to the directory containing your .html files and Use the ls command to list all the files with the .html extension.

cd /path/to/your/files
ls -l *.html

Now use the rename command to change the file extensions from .html to .php.

rename 's/\.html$/\.php/' *.html

Explanation of the command:

  • 's/\.html$/.php/': This is a Perl expression where s/ indicates substitution. The \.html$ matches the .html extension at the end of the filename, and /.php/ replaces it with .php.
  • *.html: This specifies that the command should be applied to all files with the .html extension.

Now use the ls command to verify that the files have been renamed.

ls -l *.php

Change File Extensions in Linux

Change File Extensions in Linux

Now you can see above that all the html files are renamed to php.

2. Preview Changes Before Renaming Files

When undertaking critical or major renaming tasks, you can always check the changes by running the rename command with the -n argument, which will show you exactly what changes would take place, but the changes are not executed for real.

Below is an example of the command:

rename -n 's/\.html$/\.php/' *.html

Dry Run File Renaming

Dry Run File Renaming

Note: The above command only displays changes, but in real the changes are not done, unless you run the command without “-n” switch.

3. View Detailed Rename Information

The rename command doesn’t display information about the changes it makes by default. If you want to see details about the renames (similar to using the -n option for dry runs), use the -v option, which will print the complete details of all the changes made by the rename command.

rename -v 's/\.html$/\.php/' *.html

See Exactly What Changed

See Exactly What Changed

4. Change File Name Case in Linux

In Linux, you can easily change the case of file names, meaning you can convert them from uppercase to lowercase (and vice versa) using the rename command.

Convert Filenames to Uppercase in Linux

To batch rename all files with lowercase names to uppercase. For example, I want to convert all the following files from lowercase to uppercase.

rename 'y/a-z/A-Z/' *.html

Convert Filenames to Uppercase

Convert Filenames to Uppercase

Convert Filenames to Lowercase in Linux

Similarly, you can also convert all uppercase characters to lowercase using the following command.

rename 'y/A-Z/a-z/' *.HTML

Change Filenames to Lowercase

Change Filenames to Lowercase

5. Capitalize First Letter of Filename

To capitalize only the first letter of each filename use the following command.

rename 's/\b(\w)/\U$1/g' *.html

Capitalize First Letter of Filename

Capitalize First Letter of Filename

6. Replacing Spaces with Underscores

To replace all occurrences of whitespace (spaces) with underscores (_) in the filenames of HTML files within the current directory.

rename 's/\s+/_/g' *.html

Explanation of the above command.

  • \s+: Matches one or more whitespace characters.
  • _: Replaces whitespace with underscores.
  • g: Global replacement, affecting all matches in each file name.

7. Overwrite Existing Files

If you would like to forcefully overwrite existing files, use the “-f” option as shown below.

rename -f 's/a/b/' *.html

If you would like to know more about rename command, type the “man rename” in the terminal.

man rename

The rename command is very useful when dealing with multiple or batch renaming of files from the command line. Give it a try and let me know how useful it is for renaming files.

标签:Rename,Files,Multiple,rename,html,command,Linux,php,files
From: https://www.cnblogs.com/bluestorm/p/18212175

相关文章

  • Ubuntu 解决 Too many open files 问题
    #查看限制结果ulimit-a#修改配置#删除最后一行echo-e"#add_config"|sudotee-a/etc/security/limits.conf#加上文本echo-e"\n"|sudotee-a/etc/security/limits.confecho-e"mzc\tsoft\tnproc\t204800"|sudotee-a......
  • 无法加载文件 C:\Program Files\nodejs\pnpm.ps1,因为在此系统上禁止运行脚本。有关
    无法加载文件C:\ProgramFiles\nodejs\pnpm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅https:/go.micros  原因:现用执行策略是Restricted(默认设置)解决方法:1、使用管理员的身份打开powershell, win+x2、输入set-executionpolicyremotesigned后按y,问题得......
  • how BabyFile app transfer files with Windows PC though data cable
    1.WindowsPCdownload"iTunes"App.Asfollows:2.WindowsPCconnecttheiPhone/iPadwithadatacable,andthen open"iTunes"app, findandclicktheconnecteddevice.asmarkedby①inthepicture. 2.Findand clickthe"Fi......
  • Windows Basics - Finding Files on Your Computer Back to Tutorial
     everything 推荐用这个工具搜索文件 FindingfilesonyourcomputerInthepreviouslesson,wetalkedabouthowfolderscanhelptokeepyourfilesorganized.However,theremaybetimeswhenyouhavetroublefindingacertainfile.Ifthishappenstoyou......
  • Windows Basics - Working with Files
     WorkingwithfilesUnderstandinghowtoworkwithfilesandfoldersisanimportantpartofusingyourcomputer.Onceyouunderstandhowfilesandfolderwork,you'llusethemallthetime.Inthislesson,we'llshowyoutheabsolutebasicsof......
  • Error: Cannot find module 'C:\Program Files\nodejs\node_modules\npm\bin\no
     #参考:https://stackoverflow.com/questions/69541725/error-cannot-find-module-c-program-files-nodejs-node-modules-npm-bin-node-mod --- #问题描述在一直倒腾重新安装nodejs时报的一个这样的错,记录一下 在执行npm-v时报了如标题的错,见下图 --- #原因......
  • 题解【[ABC077D] Small Multiple】
    题目链接题意简述:给定正整数\(K\),求数位之和最小的\(K\)的倍数的数位和。错误方向:\(K\)的倍数一定满足\(K\timesS\),根据\(K\)的特征构造出合适的\(S\)。正确方向考虑直接构造出K的倍数,由于从1开始可以通过×10和+1构造出所有数字,并且在此......
  • 好用的自动同步软件:FreeFileSync
    最近研究怎么能够将文件从电脑上自动备份到移动硬盘,发现了很多人都在安利的FreeFileSync(官网:FreeFileSync )FreeFileSync​freefilesync.org/这是一个免费软件,在各个系统中都有提供安装包。软件安装过程非常简单,目前我将其直接安装到了C盘,安装好后即可看到两个软件:绿色的FreeFil......
  • WPF MVVM Datagrid Selected Multiple items via behavior interaction.trigger,event
    1.Install Microsoft.Xaml.Behaviors.WpffromNuget;2.Addbehaviorreferenceinxamlxmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"3.Passmethodtomvvmviabehavior,interaction,trigger,eventname,TargetObject,MethodNameinxaml......
  • 使用 chezmoi & vscode, 管理你的 dotfiles
    什么是dotfilesInUnix-likeoperatingsystems,anyfileorfolderthatstartswithadotcharacter(forexample,/home/user/.config),commonlycalledadotfileordotfile.任何以.开头去命名的文件或者目录都可以称为dotfile,在Unix-like系统一般用的比较多......