歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下批量修改文件名的詳細操作

Linux下批量修改文件名的詳細操作

日期:2017/2/25 10:37:17   编辑:Linux教程

  linux下批量修改文件名.最近想把一部分文件加上日期序。也就是在文件名稱前加上當前的年月日進行輸出。

  獲取日期格式如下

  Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ date +%Y%m%d

  20091112

  1、使用find、awk/grep/sed、mv等命令組合來實現

  Ubuntu下

  利用awk命令進行制作,操作如下

  1)找到需要進行操作的文件名

  Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ find -name ‘*.jpg’ -printf %fn|grep -v ^[0-9] |grep -v logo.jpg

  se_platform.jpg

  soa1.jpg

  soa4.jpg

  java_primitive_data_type.jpg

  soa3.jpg

  soa2.jpg

  2)提取文件名,保持文件擴展名不變

  Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ find -name ‘*.jpg’ -printf %fn|grep -v ^[0-9] |grep -v logo.jpg|awk -F ‘.’ ‘{print $1}’

  se_platform

  soa1

  soa4

  3、使用rename命令

  Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ rename –help

  Unknown option: help

  Usage: rename [-v] [-n] [-f] perlexpr [filenames]

  man rename發現,

  “rename” renames the filenames supplied according to the rule specified as the first argument.

  The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified.If a given filename is not modified by the expression,it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input.

  For example, to rename all files matching “*.bak” to strip the extension, you might say

  rename ’s/.bak$//’ *.bak

  To translate uppercase names to lower, you’d use

  rename ‘y/A-Z/a-z/’ *

  命令格式:

  rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

  perlexpr 是一種 Perl 腳本格式的正則表達式。

  files 文件名通配符

  參數:

  -v, –verbose

Copyright © Linux教程網 All Rights Reserved