加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux利用sed批量修改文件名

发布时间:2020-12-14 02:02:34 所属栏目:Linux 来源:网络整理
导读:初始文件名 # ls -lh total 5.5G-rw-r--r-- 1 root root 193K Sep 28 09:38 20180908.txtdrwxr-xr-x 2 root root 4.0K Mar 7 16:37 batch-rw-r--r-- 1 root root 160M Mar 13 16:35 batch_dispatcher.log.1-rw-r--r-- 1 root root 17M Jan 8 12:00 batch_gen
初始文件名

# ls -lh 
total 5.5G
-rw-r--r-- 1 root root 193K Sep 28 09:38 20180908.txt
drwxr-xr-x 2 root root 4.0K Mar  7 16:37 batch
-rw-r--r-- 1 root root 160M Mar 13 16:35 batch_dispatcher.log.1
-rw-r--r-- 1 root root  17M Jan  8 12:00 batch_gen_scheduler.log.1
-rw-r--r-- 1 root root 3.5G Mar 13 16:36 batch_scheduler.log.1
-rw-r--r-- 1 root root 329M Jan 25 10:11 batch_scheduler_test.log.1
drwxr-xr-x 2 root root 4.0K Jan 21 13:54 batch_test
-rw-r--r-- 1 root root  30M Jan 25 10:10 batch_test_dispatcher.log.1
-rw-r--r-- 1 root root 137M Mar 13 16:29 boss_bi_access.log.1
-rw-r--r-- 1 root root  86M Mar 13 16:29 boss_bi_error.log.1
-rw-r--r-- 1 root root 185M Mar 13 14:32 boss_channel_access.log.1
-rw-r--r-- 1 root root  96M Mar 13 14:32 boss_channel_error.log.1
-rw-r--r-- 1 root root  77K Jan 21 14:45 monitor_once.log.1
-rw-r--r-- 1 root root 638M Mar 13 16:35 monitor_scheduler.log.1
-rw-r--r-- 1 root root  26M Dec 10 22:00 yqb_job.log.1
drwxr-xr-x 2 root root 4.0K May 29  2018 zhongcai
-rw-r--r-- 1 root root 348M Mar 13 16:35 zhongcai_scheduler.log.1
-rw-r--r-- 1 root root 688K Jan 28 10:51 zhongcai_test_access.log.1
-rw-r--r-- 1 root root 538K Jan 29 11:23 zhongcai_test_error.log.1
drwxr-xr-x 2 root root 4.0K Aug 21  2018 zhongcai_v1.4

需要将.log.1结尾的文件更改为.log
命令如下:

# for file in $(ls -lh | awk ‘$NF~/log/{print $NF}‘);do newfile=$(echo $file | sed ‘s/.1//g‘); mv $file $newfile; done
# 美化一下
for file in $(ls -lh | awk ‘$NF~/log/{print $NF}‘); do
    newfile=$(echo $file | sed ‘s/.1//g‘)
    mv $file $newfile
done

法二:

#  ls *.1 | sed -r "s#(.*).1#mv & 1#" | bash

注:
sed替换标记:
1表示前面第一个左括号所表示的内容,2表示前面第二个左括号中表示的内容,以此类推&表示前面匹配到的内容

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读