ディレクトリ、サブディレクトリ、およびファイルのmtimeを繰り返し変更するためにユーザー入力を必要とするBashスクリプトatime linux

ディレクトリ、サブディレクトリ、およびファイルのmtimeを繰り返し変更するためにユーザー入力を必要とするBashスクリプトatime linux

GNU bash バージョン 4.3.11(1)-リリース(x86_64-pc-linux-gnu)の使用

私はbashスクリプトに初めてアクセスし、shebang以外はどこから始めるべきかわかりません#!。次のコマンド。

  • touch -a -m -t 201501010000.00 somefile.txt、"somefile.txt"のアクセス時間と修正時間を修正します。
  • Bashスクリプトを取得する方法はありますか?

    1. "/mnt/harddrive/BASE/" ディレクトリで動作します。

    2. ユーザーに入力するように求められます。 「somefilename.txt」または「somedirectoryname」。

    3. ユーザーに入力するように求められます。 「日付時間シリーズ」。現在のタイムスタンプを使用せず、代わりに-tオプションを使用して-d時間/日付を明示的に指定してください。
    4. 再帰的に変更/修正します。 BASEディレクトリの「サブディレクトリ」の「atime」、「mtime」、およびその「サブディレクトリ」の「files」。そして
    5. 変更/修正。 「somefilename.txt」の「atime」と「mtime」は、「/mnt/hardrive/BASE/」ディレクトリにあります。

    オプション6.「somefilename」および「somedirectoryname」ファイル拡張子の前に「mtime」を追加します。つまり、「somefilename-01-01-2015.txt」または「somedirectoryname-01-01-2015」です。ユーザーに尋ねます。 「はい/いいえ」が続く場合は、「somefilename.txt」「はい/いいえ」に「mtime」を追加しますか?

    1. statディレクトリとファイルをコンソールまたは「/tmp」ディレクトリのテキストファイルに出力して表示し、cat「sometmpfile」を削除しますrm -r

ベストアンサー1

次のように見えます。

#!/bin/bash

# 1. change directory
cd "/mnt/harddrive/BASE/" 

# 2. prompt for name of file or directory
echo -n "file or directory name: "
# ...  and read it
read HANDLE

# 2. b - check if it exists and is readable
if [ ! -r "$HANDLE" ] 
then
    echo "$HANDLE is not readable";
    # if not, exit with an exit code != 0
    exit 2;
fi

# 3. prompt for datetime
echo -n "datetime of file/directory: "
# ... and read it
read TIMESTAMP

# 4. set datetime for HANDLE (file or directory + files) 
find $HANDLE | xargs touch -a -m -t "$TIMESTAMP"

# 5. ask, if the name should be changed
echo -n "change name of file by appending mtime to the name (y/n)?: "
# ... and read it
read YES_NO
if [ "$YES_NO" == "y" ]
then
    # get yyyy-mm-dd of modification time 
    SUFFIX_TS=$(stat -c "%y" $HANDLE  | cut -f 1 -d" ")
    # rename, supposed, the suffix is always .txt
    mv $HANDLE $(basename $HANDLE txt)-$SUFFIX_TS.txt
    # let HANDLE hold the name for further processing
    HANDLE=$HANDLE-$TIMESTAMP.$SUFFIX
fi

# 7. stat to console
stat $HANDLE

これはテストの一部にすぎませんが、開始する必要があります。

ここで何が起こっているのかを理解するには、echo、read、test、cut、touch、find、xargs コマンドを探す必要があります。

また、パラメータ置換、コマンド置換、パイプなど、いくつかの基本的なbash概念を知る必要があります。

おすすめ記事