ヘルプ oneliner - ランダムなファイルの生成、2 文字のファイル名で名前を変更、ランダムな文字列で埋める

ヘルプ oneliner - ランダムなファイルの生成、2 文字のファイル名で名前を変更、ランダムな文字列で埋める

誰かが私にこれについて冗談を言う方法を教えてくれるほど親切ですか?それともsed / awk / xargsを使用できますか?それともパール?または少し簡単にしてみてください

私はstackexchangeを検索していくつかのスクリプトを編集してこれを達成しましたが、専門家のようにする方法を見たいです。

任意のテキストを含むファイルを作成しています。どのくらいのファイルが生成されるのかわかりません。説明してください。

< /dev/urandom tr -dc "\t\n [:alnum:]" | dd of=./filemaster bs=100000000 count=1 && split -b 50 -a 10 ./filemaster && rm ./filemaster

fnames1.txt ファイルに次のファイル名をリストします。

ls >> fnames1.txt

他のファイルに必要なファイル名を作成しています - fnames2.txt

list=echo {a..z} ; for c1 in $list ; do for c2 in $list ; do echo $c1$c2.ext; done; done >> fnames2.txt

これらのファイルを2つの列を持つ1つのファイルにマージしました。

paste fnames1.txt fnames2.txt | column -s $'\t' -t >> fn.txt

列を含むファイルに基づいてファイル名を変更しています(生成されたものよりも多くのファイルが生成されるため、エラーが発生します。正確にこのファイル名の数を変更するにはどうすればよいですか? - 2> / devを使用してエラーを無視できることを知っています)。 /なし):

while read -r line; do mv $line; done < fn.txt

必要な拡張子を持つファイルを別のディレクトリに移動します。

mkdir files && mv ./*.ext ./files/ && cd files

コンテンツが大きくなければならないため、次のファイルを再構築する必要があります。

for file in *; do < /dev/urandom tr -dc "\t\n [:alnum:]" | head -c1500 > "$file"; done

誰かが私に良い方法を教えたり、冗談を言うことができますか?再談の書き方を学んでいて本当にありがとうございます。

ベストアンサー1

私の考えでは、オネライナーはここには適していません。容量が大きくて読めないし不便だろうなスクリプトが良いです。これは関数に変換できます。

このスクリプトは「文書」ディレクトリに作成されたすべてのファイルを保存します。各ファイルのサイズは同じですが、必要に応じて変更できます。ファイル名:aa.ext ab.ext ac.extその他

使用法: ./create_random_files.sh

#!/bin/bash

# Number of files
file_nums=5
# The size of the each file in bytes
file_size=1500

# Creates the "files" directory if it doesn't exist
mkdir -p files

for i in {a..z}{a..z}; do
    # gets data from the /dev/urandom file and remove all unneeded characters
    # from it - all characters except "\t\n [:alnum:]".
    tr -dc "\t\n [:alnum:]" < /dev/urandom |
    # The "head" command takes specified amount of bytes and writes them to the 
    # needed file. 
    # The "files/${i}.ext" is the relative path to new files, which named 
    # like "aa.ext" and placed into the "files" directory
    head -c "$file_size" > "files/${i}.ext"

    # Iterations counter. It will stop "for" loop, when file_nums
    # will be equal to zero
    if !(( --file_nums )); then 
        break
    fi  
done

おすすめ記事