LoginSignup
0
0

More than 5 years have passed since last update.

階層が異なるファイルをフラットにして連番を振り直す

Last updated at Posted at 2018-07-18

概要

以下のような感じのディレクトリ構成で配置されたファイルを

.
|-- 1
|   |-- 001.txt
|   |-- 002.txt
|   `-- 003.txt
`-- 2
    |-- 001.txt
    |-- 002.txt
    `-- 003.txt

こうしたい。

.
|-- 001.txt (元1/001.txt)
|-- 002.txt (元1/002.txt)
|-- 003.txt (元1/003.txt)
|-- 004.txt (元2/001.txt)
|-- 005.txt (元2/002.txt)
`-- 006.txt (元2/003.txt)

Bashワンライナー

a=0; find ./ -type f | sort | while read i; do a=`expr $a + 1`; mv "$i" `printf "%03d" $a`.${i##*.}; done

特定のファイルだけリネームするには、findの引数に条件を加える。
例: find ./ -type f -iname "*.txt"

連番の0パディングの桁数はprintfの引数を変更する。
例: 4桁にする場合 printf "%04d" $a

空になったディレクトリを削除する場合は追加で下記のワンライナーを実行。

find ./ -type d -empty | xargs rmdir
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