首页 > 编程知识 正文

linux改名文件,linux的rename命令

时间:2023-05-04 10:02:22 阅读:20886 作者:3149

SYNOPSIS

rename from to file .

from表示需要替换或处理的字符,如文件扩展名和文件名。

to表示from处理后的结果。

file表示目标文件。

[ [电子邮件保护] tmp ] # ls

hello _ 10 _ 2016-03-17.log hello _4_ 2016-03-17.log hello _8_ 2016-03-17.log

hello _1_ 2016-03-17.log hello _5_ 2016-03-17.log hello _9_ 2016-03-17.log

hello _2_ 2016-03-17.log hello _6_ 2016-03-17.log

hello _3_ 2016-03-17.log hello _7_ 2016-03-17.log

[ [电子邮件保护] tmp ] # rename '.log '.jpg ' *

[ [电子邮件保护] tmp ] # ls

hello _ 10 _ 2016-03-17.jpg hello _4_ 2016-03-17.jpg hello _8_ 2016-03-17.jpg

hello _1_ 2016-03-17.jpg hello _5_ 2016-03-17.jpg hello _9_ 2016-03-17.jpg

hello _2_ 2016-03-17.jpg hello _6_ 2016-03-17.jpg

hello _3_ 2016-03-17.jpg hello _7_ 2016-03-17.jpg

使用sed:

[ [电子邮件保护] tmp ] # ls|sed-NR ' s # (^.* [0-9].).* ) #mv(1log#gp ) ]

mv hello _ 10 _ 2016-03-17.jpg hello _ 10 _ 2016-03-17.log

mv hello _1_ 2016-03-17.jpg hello _1_ 2016-03-17.log

mv hello _2_ 2016-03-17.jpg hello _2_ 2016-03-17.log

mv hello _3_ 2016-03-17.jpg hello _3_ 2016-03-17.log

mv hello _4_ 2016-03-17.jpg hello _4_ 2016-03-17.log

mv hello _5_ 2016-03-17.jpg hello _5_ 2016-03-17.log

mv hello _6_ 2016-03-17.jpg hello _6_ 2016-03-17.log

mv hello _7_ 2016-03-17.jpg hello _7_ 2016-03-17.log

mv hello _8_ 2016-03-17.jpg hello _8_ 2016-03-17.log

mv hello _9_ 2016-03-17.jpg hello _9_ 2016-03-17.log

[ [电子邮件保护] tmp ] # ls|sed-NR ' s # (^.* [0-9].).* ) #mv(1log#gp'|bash

[ [电子邮件保护] tmp ] # ls

hello _ 10 _ 2016-03-17.log hello _4_ 2016-03-17.log hello _8_ 2016-03-17.log

hello _1_ 2016-03-17.log hello _5_ 2016-03-17.log hello _9_ 2016-03-17.log

hello _2_ 2016-03-17.log hello _6_ 2016-03-17.log

hello _3_ 2016-03-17.log hello _7_ 2016-03-17.log

3 .使用for替换字符串

[ [电子邮件保护] tmp ] # vim rename.sh

#! /bin/bash

#This script is use to rename files

for name in `ls *.log `;

德奥

echo 'mv $name ${name/.log/.txt} '

道恩

[ [电子邮件保护] tmp ] #./rename.sh|bash

[ [电子邮件保护] tmp ] # ls

hello _ 10 _ 2016-03-17.txt hello _4_ 2016-03-17.txt hello _8_ 2016-03-17.txt

hello _1_ 2016-03-17.txt hello _5_ 2016-03-17.txt hello _9_ 2016-03-17.txt

hello _2_ 2016-03-17.txt hello _6_ 2016-03-17.txt rename.sh

hello _3_ 2016-03-17.txt hello _7_ 2016-03-17.txt

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。