日付別にグループ化されたファイルリストを取得する

日付別にグループ化されたファイルリストを取得する

毎日送られてくるファイルが入っているディレクトリがあります。これらのファイルを日付ごとにまとめて圧縮したいのですが、同じ日付に届いたファイルをグループ化/リスト化する方法はありますか。

ディレクトリ内に以下のファイルがあるとします

-rw-r--r--. 1 anirban anirban    1598 Oct 14 07:19 hello.txt
-rw-r--r--. 1 anirban anirban    1248 Oct 14 07:21 world.txt
-rw-rw-r--. 1 anirban anirban  659758 Oct 14 11:55 a
-rw-rw-r--. 1 anirban anirban    9121 Oct 18 07:37 b.csv
-rw-r--r--. 1 anirban anirban     196 Oct 20 08:46 go.xls
-rw-r--r--. 1 anirban anirban    1698 Oct 20 08:52 purge.sh
-rw-r--r--. 1 anirban anirban   47838 Oct 21 08:05 code.java
-rw-rw-r--. 1 anirban anirban 9446406 Oct 24 05:51 cron
-rw-rw-r--. 1 anirban anirban  532570 Oct 24 05:57 my.txt
drwxrwsr-x. 2 anirban anirban      67 Oct 25 05:05 look_around.py
-rw-rw-r--. 1 anirban anirban   44525 Oct 26 17:23 failed.log

したがって、ファイルはすべて一意であるため、サフィックス/プレフィックスを使用してファイルをグループ化する方法はありません。今、私が求めているコマンドを実行すると、日付によるグループ化に基づいて以下のような行のセットが取得されます。

[ [hello.txt world.txt a] [b.csv] [go.xls purge.sh] [code.java] ... ] and so on.

そのリストを使ってループしてアーカイブを作成します

tar -zvcf Oct_14.tar.gz hello.txt world.txt a

答え1

ではzsh、キーが日付で、値がその日付に最後に変更されたファイルの NUL 区切りリストである連想配列を使用します。

zmodload -F zsh/stat b:zstat
typeset -A files
for file (./*) {
  zstat -LA date -F %b_%d +mtime $file &&
    files[$date]+=$file$'\0'
}
for date (${(k)files})
  echo tar zcvf $date.tar.gz ${(0)files[$date]}

幸せなら削除しますecho

月名の省略形 ( strftime 形式) は、現在のロケールの言語 (英語システムでは 、ドイツ語システムでは など)%bになることに注意してください。ユーザーのロケールに関係なく常に英語名にするには、でロケールを強制します。OctOktCLC_ALL=C zstat...

GNU ツールを使用すると、次のように同等のことを実行できます。

find . ! -name . -prune ! -name '.*' -printf '%Tb_%Td:%p\0' |
  awk -v RS='\0' -F : -v q=\' '
    function quote(s) {
      gsub(q, q "\\" q q, s)
      return q s q
    }
    {
      date=$1
      sub(/[^:]*:/, "", $0)
      files[date] = files[date] " " quote($0)
    }
    END {
      for (date in files)
        print "tar zcvf " quote(date ".tar.gz") files[date]
    }'

sh嬉しい時はパイプを鳴らします。

答え2

while read date a b filename; do 
    zip -g ../$date.zip "$filename"
done <<< $(stat -c "%y %n" *)

答え3

おそらく私は質問を誤解したのでしょうが、

ls -ls |grep '10月26日' > oct26.list

10 月 26 日のファイルのリストが表示されます。たとえば、grep "| grep 09:" などを追加して、時間についても同様のことが行えます。その後、必要な操作を oct26.list リストで実行します。

関連情報