自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你在 Centos8 中如何更改文件夾里多個文件的擴展名

系統(tǒng) Linux
本教程將討論將文件從特定擴展名更改為另一個擴展名的快速方法。我們將為此使用 shell循環(huán)、rename命令。
本教程將討論將文件從特定擴展名更改為另一個擴展名的快速方法。我們將為此使用 shell循環(huán)、rename命令。

方法一:使用循環(huán)

在目錄中遞歸更改文件擴展名的最常見方法是使用 shell 的 for 循環(huán)。我們可以使用 shell 腳本提示用戶輸入目標(biāo)目錄、舊的擴展名和新的擴展名以進行重命名。以下是腳本內(nèi)容:

  1. [root@localhost ~]# vim rename_file.sh 
  2. #!/bin/bash 
  3. echo "Enter the target directory " 
  4. read target_dir 
  5. cd $target_dir 
  6.  
  7. echo "Enter the file extension to search without a dot" 
  8. read old_ext 
  9.  
  10. echo "Enter the new file extension to rename to without a dot" 
  11. read new_ext 
  12.  
  13. echo "$target_dir, $old_ext, $new_ext" 
  14.  
  15. for file in *.$old_ext 
  16. do 
  17.     mv -v "$file" "${file%.$old_ext}.$new_ext" 

上面的腳本將詢問用戶要處理的目錄,然后 cd 進入設(shè)置目錄。接下來,我們得到?jīng)]有點.的舊擴展名。最后,我們獲得了新的擴展名來重命名文件。然后使用循環(huán)將舊的擴展名更改為新的擴展名。

其中${file%.$old_ext}.$new_ext意思為去掉變量$file最后一個.及其右面的$old_ext擴展名,并添加$new_ext新擴展名。

使用mv -v,讓輸出信息更詳細。

下面運行腳本,將/root/test下面的以.txt結(jié)尾的替換成.log:

  1. [root@localhost ~]# chmod +x rename_file.sh  
  2. [root@localhost ~]# ./rename_file.sh  
  3. Enter the target directory  
  4. /root/test 
  5. Enter the file extension to search without a dot 
  6. txt 
  7. Enter the new file extension to rename to without a dot 
  8. log 
  9. /root/test, txt, log 
  10. renamed 'file10.txt' -> 'file10.log' 
  11. renamed 'file1.txt' -> 'file1.log' 
  12. renamed 'file2.txt' -> 'file2.log' 
  13. renamed 'file3.txt' -> 'file3.log' 
  14. renamed 'file4.txt' -> 'file4.log' 
  15. renamed 'file5.txt' -> 'file5.log' 
  16. renamed 'file6.txt' -> 'file6.log' 
  17. renamed 'file7.txt' -> 'file7.log' 
  18. renamed 'file8.txt' -> 'file8.log' 
  19. renamed 'file9.txt' -> 'file9.log' 

如果想將.log結(jié)尾的更改回.txt,如下操作:

 

責(zé)任編輯:武曉燕 來源: Linux就該這么學(xué)
相關(guān)推薦

2009-08-25 09:26:57

Windows 7查看文件擴展名

2010-04-27 12:32:05

2009-10-21 10:13:19

Linux文件擴展名

2022-11-02 08:20:43

Linux

2017-03-21 10:11:36

Windows 10Windows文件擴展名

2022-04-30 09:41:14

LinuxNTP服務(wù)器

2020-06-15 18:40:15

Ubuntu 20.0文件夾顏色Ubuntu

2022-04-15 07:51:36

Centos8遷移邏輯卷

2011-08-02 09:38:58

ActiveDirec域控制器共享文件夾

2022-03-16 20:27:12

微軟Windows操作

2009-10-29 14:50:22

VB.NET擴展名分組

2011-08-02 09:22:15

ActiveDirec域控制器共享文件夾

2010-05-21 14:09:41

2016-12-07 09:30:00

Power QueryExcel文件

2019-10-28 14:38:36

RsyslogCentOS8開源

2021-04-01 16:36:07

macOS文件夾磁盤

2021-08-16 13:34:07

Linux終端刪除文件

2023-05-13 17:43:17

Linux文件文件夾

2022-07-08 15:09:26

Linux

2011-04-11 16:07:13

系統(tǒng)備份
點贊
收藏

51CTO技術(shù)棧公眾號