9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

lsオプションを使って、Excel貼り付けを楽にしたい

Last updated at Posted at 2024-08-08

:point_up:調べようと思ったきっかけ

ファイル情報の取得と証跡や検証として出力するのは1日に何回も行っているのですが、現状とても効率が悪いと思って調べてみました。

現状
findで取得したディレクトリ配下のファイルをlsコマンド形式でテキストとして出力する▼

find /BENKYO -type f | xargs -r ls -l --time-style=+'%Y%m%d %H:%S' > BENKYO.txt

出力されたテキストをExcelにコピペする
★セル1つに横並びの1行が入ってしまうので区切り文字を使って都度データの整理を行う

今回省略したい手順は★の行

方法その1(?)

出力されたテキストをExcelに貼り付ける前にメモ帳に貼って、スペースをタブに置換する
➡別ツールを起動しなくてはならない手間がある
➡タブキーは多くの機能を持っているため、タブスペースを置換後として認識させるには、別で用意したタブスペースをコピペする必要がある
➡不採用

:point_up:point_up方法その2

以下オプションのついたコマンドを実行する

tr -s ' ' '¥t'

説明:ls形式で出力されるリストのスペースをタブで表示させる
実用例▼

find /BENKYO -type f | xargs -r ls -l --time-style=+'%Y%m%d %H:%S'| tr -s ' ' '¥t' > BENKYO.txt

レコード間がタブスペースとして出力されてあるため、そのままExcelにコピペすると自動的にそれぞれのセルにそれぞれのデータが分けられる。
➡コマンドに即反映させられるので手間がかなり省ける
➡採用

:point_up:その他

findコマンドに -mathdepth 1 を足すと、指定したディレクトリ直下のファイルのみを参照してくれる▼

find /BENKYO -maxdepth 1 -type f | xargs -r ls -l --time-style=+'%Y%m%d %H:%S'| tr -s ' ' '¥t' > BENKYO.txt

参考

【Linux】ls -lコマンドの実行結果をタブ区切りで取得する

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?