ファイル変更時間の名前を付けたディレクトリの作成

ファイル変更時間の名前を付けたディレクトリの作成

ファイルの変更時間に基づいてディレクトリ名を作成したいと思います。たとえば、ファイルの作成または変更が2016年3月17日18:00:00の場合、名前が1703206HP-UXのファイルを生成しようとします。

ベストアンサー1

POSIXツール(HP-UXにはもうありません)のみを使用すると、ファイルの修正時間を取得する便利な方法がないため難しいです。を使用する場合は、ls -l月、日、家、分を含む最近のファイルと、月、日、年を含む古いファイル(> 6ヶ月)の2つのケースを処理する必要があります。 (適切なロケールを作成するより簡単な方法があるかもしれませんが、HP-UXでは可能かどうかわかりません。)

#!/bin/sh
set -e -f
set -- $(LC_ALL=C ls -dlog -- "$1")
# $1=permissions $2=link_count $3=size $4,$5,$6=date_time $7...=filename
case $4 in
  Jan) month=1;;
  Feb) month=2;;
  Mar) month=3;;
  Apr) month=4;;
  May) month=5;;
  Jun) month=6;;
  Jul) month=7;;
  Aug) month=8;;
  Sep) month=9;;
  Oct) month=10;;
  Nov) month=11;;
  Dec) month=12;;
esac
case $6 in
  *:*) # The timestamp is within the last 6 month, so
       # the year is the current or previous year.
    current_month=$(LC_ALL=C date +%Y%m)
    year=${current_month%??}
    current_month=${current_month#????}
    case $current_month in 0*) current_month=${current_month#0};; esac
    if [ $current_month -lt $month ]; then year=$((year-1)); fi;;
  *) year=$6;;
esac
if [ $month -le 9 ]; then month=0$month; fi
day=$5
if [ $day -le 9 ]; then day=0$day; fi
mkdir $year$month$day

古いBourneシェルを含む以前のバージョンのHP-UXを使用している場合は、shebang行をPOSIXシェル(kshなど)のパスに置き換える必要が/bin/shあります。/bin/sh

おすすめ記事