歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> find結合rename實現批量修改後綴

find結合rename實現批量修改後綴

日期:2017/2/28 14:54:40   编辑:Linux教程

記得曾經去騰訊面試運維的職位時,筆試中就有這麼道題:要求查找/tmp下以.sh為後綴的文件並將所有這些文件修改為.bash後綴。其實很簡單,一個命令可以實現:

# rename .sh .bash *.sh

上面這個命令是將當前目錄下的所有.sh文件修改為.bash做後綴。

首先要確保這個命令所在的軟件包安裝了,

[jeff@rhel55 mydir]$ rpm -qif `which rename`
Name : util-linux Relocations: (not relocatable)
Version : 2.13 Vendor: Red Hat, Inc.
Release : 0.52.el5_4.1 Build Date: Thu 07 Jan 2010 06:25:25 PM CST
Install Date: Thu 02 Feb 2012 06:49:04 PM CST Build Host: x86-007.build.bos.RedHat.com
Group : System Environment/Base Source RPM: util-linux-2.13-0.52.el5_4.1.src.rpm
Size : 4706614 License: distributable
Signature : DSA/SHA1, Wed 13 Jan 2010 05:19:06 PM CST, Key ID 5326810137017186
Packager : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Summary : A collection of basic system utilities.
Description :
The util-linux package contains a large variety of low-level system
utilities that are necessary for a Linux system to function. Among
others, Util-linux contains the fdisk configuration tool and the login
program.

那麼配合find就更省事了,

# find /tmp -name "*.sh" -exec rename .sh .bash {} \;

Copyright © Linux教程網 All Rights Reserved