Archive for 5月, 2011

renameコマンドについて

金曜日, 5月 20th, 2011

同僚がつぶやいたので調べてみました。

普通はmv使うんだろうけど、こんな感じ。

[root@osho ~]# for i in `seq 1 20`;do touch /tmp/test.$i ;done
[root@osho ~]# ll /tmp/test.*
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.1
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.10
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.11
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.12
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.13
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.14
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.15
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.16
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.17
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.18
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.19
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.2
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.20
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.3
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.4
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.5
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.6
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.7
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.8
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/test.9
[root@osho ~]# rename test tests /tmp/test.*
[root@osho ~]# ll /tmp/test.*
ls: /tmp/test.*: No such file or directory
[root@osho ~]# ll /tmp/test*
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.1
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.10
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.11
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.12
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.13
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.14
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.15
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.16
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.17
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.18
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.19
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.2
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.20
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.3
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.4
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.5
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.6
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.7
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.8
-rw-r–r– 1 root root 0 May 20 10:59 /tmp/tests.9

typoもしくは、スクリプトのミスで間違ったファイル名を作ってしまっていた。

なんt時には置換する文字列(置換前、置換後)を第一引数、第二引数に渡し、最後に該当ファイルを指定。ワイルドカードが使えるので便利。