LoginSignup
1
0

【ファイル管理tips】ファイル名に更新日を挿入した時のメモ

Last updated at Posted at 2024-03-13

概要

大量の写真ファイルを管理するためにファイル名にファイル更新日を一括挿入した時のメモ。
重複するファイルが多数あったから。
前回も同じことやって忘れたから。

やったこと

最終系

$ ls -l
total 23040
-rwx------  1 Taiti  staff  3670016  6 17  2021 DSC02362.JPG
-rwx------  1 Taiti  staff  4456448  6 17  2021 DSC02363.JPG
-rwx------  1 Taiti  staff  3670016  3 13 23:13 DSC02364.JPG
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ for f in `ls`;do mv ${f} ${f%%.*}_`date -r $f +%Y%m%d`.${f##*.}; done
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ ls -l                                                                
total 23040
-rwx------  1 Taiti  staff  3670016  6 17  2021 DSC02362_20210617.JPG
-rwx------  1 Taiti  staff  4456448  6 17  2021 DSC02363_20210617.JPG
-rwx------  1 Taiti  staff  3670016  3 13 23:13 DSC02364_20240313.JPG

要素分解

ファイル更新日の取得

[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ date -r DSC02362.JPG
2021年 6月17日 木曜日 22時37分12秒 JST
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ date -r DSC02362.JPG +%Y:%m:%d:%H:%M:%S:%N
2021:06:17:22:37:12:N
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ date -r DSC02362.JPG +%Y%m%d          
20210617

ファイル名の取得

# 状態確認
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ ls -l
total 23040
-rwx------  1 Taiti  staff  3670016  6 17  2021 DSC02362_20210617.JPG
-rwx------  1 Taiti  staff  4456448  6 17  2021 DSC02363_20210617.JPG
-rwx------  1 Taiti  staff  3670016  3 13 23:13 DSC02364_20240313.JPG

# ファイル名
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ for f in `ls`;do echo ${f%%.*};done
DSC02362_20210617
DSC02363_20210617
DSC02364_20240313

# 拡張子
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ for f in `ls`;do echo ${f##*.};done 
JPG
JPG
JPG

連結

# ファイル名+日付
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ for f in `ls`;do
echo ${f%%.*}_`date -r $f +%Y%m%d` 
done
DSC02362_20210617
DSC02363_20210617
DSC02364_20240313

# ファイル名+日付+拡張子
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ for f in `ls`;do mv ${f} ${f%%.*}_`date -r $f +%Y%m%d`.${f##*.}; done
[Taiti@Taichi : ~/Documents/WORK/20240313_filerename_automation]
$ ls -l                                                                
total 23040
-rwx------  1 Taiti  staff  3670016  6 17  2021 DSC02362_20210617.JPG
-rwx------  1 Taiti  staff  4456448  6 17  2021 DSC02363_20210617.JPG
-rwx------  1 Taiti  staff  3670016  3 13 23:13 DSC02364_20240313.JPG

修正したい時〜

# 確認
for f in `ls`;do if [ ${f} = ${f%%_*}_`date -r $f +%Y%m%d`.${f##*.} ];then echo ${f%%_*}_`date -r $f +%Y%m%d`.${f##*.}; else; echo ${f};fi;done

# 修正
for f in `ls`;do if [ ${f} = ${f%%_*}_`date -r $f +%Y%m%d`.${f##*.} ];then `mv ${f} ${f%%_*}.${f##*.}`; else; echo ${f};fi;done

[ xxxx (1).jpg ] のようにスペースを含む場合

# 確認
find . -type f | while read file_path;do basename=`basename ${file_path}`;echo ${basename%%[_ .]*};echo ${basename#*.};done

# ファイル名修正
find . -type f | while read file_path;do basename=`basename ${file_path}`;`mv ${basename} ${basename%%[_ .]*}.${basename#*.}`;done

参考

bashの変数展開によるファイル名や拡張子の取得

1
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
1
0