LoginSignup
0
0

More than 5 years have passed since last update.

今日学んだことを書く(renameコマンドによるファイル名変換)

Posted at

ログなど日付因子を付加して記録していくことが多々ありますが
後々それらをまとめるため日付因子を取り除きたいことがあります。
mvコマンドでもよいのですもうちょっと簡単に書きたい時にどうしたらよいか
探していたらrenameコマンドがよさ気だったので自身の備忘録も兼ねて書きます。

rename

   rename - renames multiple files

SYNOPSIS
rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E perlexpr]*|perlexpr
[ files ]

DESCRIPTION
"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/\e.bak$//' *.bak

   To translate uppercase names to lower, you'd use

           rename 'y/A-Z/a-z/' *

どうやら正規表現が使えるということなのでやってみる。
ファイル名から日付因子部分を消してみます(正規表現を使って)

$touch aaaaaaaaaaaa_20171111.txt

$ls -l $_
aaaaaaaaaaaa_20171111.txt

$rename -v 's/(^.*)(_[0-9]{8})(.txt$)/$1$3/' aaaaaaaaaaaa_20171111.txt
aaaaaaaaaaaa_20171111.txt renamed as aaaaaaaaaaaa.txt

うまく行きました。
ここまでわかればあとは入力ファイルをまとめて(*などつかって)
一括変換すれば良さそうですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0