In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to generate random files through the command line in the Linux system. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. When you don't need to care about the contents of a random file, you just need a fixed-size file.
In Unix systems such as Solaris and Mac OS X, mkfile instructions can generate files of specified size, but there is no such instructions on Linux.
Example: mkfile-n 160g test1
Linux can use the dd directive, and / dev/zero is a special file descriptor that returns a null value.
Example: dd if=/dev/zero of=test.file count=1024 bs=1024
A file that produces count * bs bytes, 1m
The advantage of this method to generate random files is that it is efficient (1G files are generated for about 1s), and the size of the created files is accurate to bytes.
There are disadvantages, too.
Use null characters to populate the contents of the file, and there are no lines in the file count (wc-l test.file is 0)
two。 When you don't need to care about the contents of the random file, but expect the test file to have statistical lines
Change / dev/zero to / dev/urandom,/dev/urandom is a random number generator under linux
The difference between / dev/urandom and / dev/random is not discussed in detail here, probably is that the former is not limited by the system interrupts, even if there is not enough interrupt it can generate enough output value through the random number generator; while the latter if used in dd, it can not be interrupted by ctrl+c or kill-9, if the value of ds is large, the random value generated is insufficient and long-term occupation of CPU. Although the random number generated by / dev/random is more random, it is recommended that / dev/urandom is more efficient when mixed with dd.
Disadvantage with / dev/zero is of course less efficient, generate a 100Mb file takes about 10 seconds, and the file has no readable content, the general situation is basically satisfied.
Missing a sentence, dd is an instruction supported by both linux and unix.
3. When you care about the random number of lines of content in a file, you don't care if the content is duplicated.
The idea here is to find a reference file (such as line 2), redirect the file to a new file, and then mv to overwrite and save it, plus a for loop. (n is the number of loops, resulting in file behavior 2 ^ (nasty 1))
Example: suppose you first create a file.txt file with two lines Hello and World
For i in {1... n}; do cat file.txt file.txt > file2.txt & & mv file2.txt file.txt; done
Because it is factorial, the line of nasty 20 is already 200W, so the efficiency will drop sharply.
4. When you care about the contents of random files and don't want to repeat lines of content
In this case, the instructions of the system should not be satisfied, or it can be achieved by writing a long series of scripts through the instructions of the operating system, but this is not recommended, because for readability and maintainability, you should introduce scripting languages of Ruby or Python classes to help.
But we still need some systematic help.
Idea: / usr/share/dict/words there are some words recorded, a total of 235886 lines, one word per line
You can choose some of the contents of the file from it.
The addition loop meets the random file requirements we want.
For example: ruby-e 'aroomSTDIN.readlinesX. Times do; b = []; Y.times do; b
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.