- How to create 100 files
- 2 Answers 2
- Create many files with random content
- 6 Answers 6
- How can I create 1000 files that I can use to test a script?
- 9 Answers 9
- Жизнь — это движение! А тестирование — это жизнь 🙂
- 1 комментарий:
- How to Create Thousands/Millions Files in Linux
- Linux File Creation – 10,000 Files
- Create the master file
- Split the master file into thousands of pieces
- 5 Ways to Create a File in Linux
- 1. Creating a File using the touch Command
- 2. Creating a File using the echo Command
- 3. Creating a File using the cat Command
- 4. Creating a File using the printf Command
- 5. Creating a File using a Text Editor
How to create 100 files
so I have to generate 100 files in which would be any random number. Then I have to read those files and find in which file is the biggest number also sort all values and put everything in new .txt file. What I have to do first? Thank you!
Sounds like a homework assignment. We can, perhaps, give hints (not sure), but you need to ask specific questions showing what you have already tried and why it isn’t working.
2 Answers 2
First, generate all random numbers using $RANDOM in a loop and redirect them to the individual files, like that:
for i in ; do echo $RANDOM > $RANDOM.txt; done;
(There’s a slight chance of duplicate numbers, so be aware of that.)
Next, read and sort these files, and tail just that last one. The full script will be something like that:
#!/bin/bash for i in ; do echo $RANDOM > $.sample; done; cat *.sample | sort | tail -n1;
@fkraiem $RANDOM is also a Bashism. Given that $RANDOM is being used, there’s no good reason to avoid brace expansion. Anyway, this code has #!/bin/bash at the top. (It was always intended to be there, even before my edit. But it was placed on the same line as, and immediately after, the triple backticks that introduce a code fence. So it didn’t appear, since that’s where the optional name of a language given for purposes of specifying syntax highlighting goes.)
I think you need to tell sort it is sorting numbers and not text. Also sorted output isn’t going to a text file as the homework assignment requests.
Guys, listen, it wasn’t my intention to give the whole asnwer, just directions, ok? And about the bashisms, I’m aware of that. =)
You haven’t specified a scripting language so I’m assuming you are open to any language that can do what you need. You could use PHP. Just create a loop that generates a number and saves it to a file until it’s created 100. Then read those 100 files into an array. Sort the array to have the numbers in order. Convert the array to a string, and save it to a new text file. Also get the last value from the array as this will be the largest number.
I’m not sure why you need to create all these files though. You could skip all that, and just create the final file containing all the numbers.
/.txt", random_int(0, 1000000000)); > #Read numbers from the 100 files into a sorted new text file $numbers = array(); for ($n = 1; $n /.txt"); > sort($numbers, SORT_NUMERIC); file_put_contents("/sorted_numbers.txt", implode("\n", $numbers)); #Determine the biggest number $biggest_number = end($numbers); echo "This biggest number is $biggest_number.\n"; ?>
Create many files with random content
What did you mean by “exclusive”? It doesn’t make sense in context, so you probably used the wrong word.
6 Answers 6
Since you don’t have any other requirements, something like this should work:
#! /bin/bash for n in ; do dd if=/dev/urandom of=file$( printf %03d "$n" ).bin bs=1 count=$(( RANDOM + 1024 )) done
(this needs bash at least for ).
This needs bash for numerous reasons, including $((…)) and $RANDOM . Even $(…) might not exist in every shell.
@G-Man, in any case, none of those features are specific to bash nor did they originate in bash ( <1..1000>comes from zsh , for n in. ; done and variable expansion comes from the Bourne shell, $(. ) , $((. )) and $RANDOM come ksh). The features that are not POSIX are <1..1000>, $RANDOM and /dev/urandom .1..1000>
If you wanted 1..1000 to be constant-width you need «%04d» in which case bash or zsh can do with no printf
Additional feature: I need a way to spread these files over lots of randomly named nested subdirectories.
A variation with seq , xargs , dd and shuf :
seq -w 1 10 | xargs -n1 -I% sh -c 'dd if=/dev/urandom of=file.% bs=$(shuf -i1-10 -n1) count=1024'
Explanation as requested per comments:
seq -w 1 10 prints a sequence of numbers from 01 to 10
xargs -n1 -I% executes the command sh -c ‘dd . % . ‘ for each sequence number replacing the % with it
dd if=/dev/urandom of=file.% bs=$(shuf . ) count=1024 creates the files feeded from /dev/urandom with 1024 blocks with a blocksize of
shuf -i1-10 -n1 a random value from 1 to 10
How can I create 1000 files that I can use to test a script?
I would like to create 1000+ text files with some text to test a script, how to create this much if text files at a go using shell script or Perl. Please could anyone help me?
Can you create one text file? Can you devise a scheme to generate file names, eg. file0001, file002? Can you write a for loop to do those two tasks. At what point do you get stuck?
9 Answers 9
for i in do echo "some text" > "file_$.txt" done
or if you want to use Python
for x in range(1000): open("file%03d.txt" % x,"w").write("some text")
Your filenames in the Bash version will be 10 through 1000999. I think you mean echo «some text» > file$file
The first option requires Bash 4 to make use of the padding. In earlier versions, you can specify leading zeros, but they won’t appear in the result.
#!/bin/bash seq 1 1000 | split -l 1 -a 3 -d - file
Above will create 1000 files with each file having a number from 1 to 1000. The files will be named from file000 to file999 .
use strict; use warnings; for my $i (1..1000) < open(my $out,">",sprintf("file%04d",$i)); print $out "some text\n"; close $out; >
Why the first 2 lines? Because they are good practice so I use them even in 1-shot programs like these.
#!/usr/bin/perl use strict; use warnings; use File::Slurp; write_file $_, "$_\n" for map sprintf('file%04d.txt', $_), 1 .. 1000;
#!/bin/bash for suf in $(seq -w 1000) do cat myfile.$suf this is my text file there are many like it but this one is mine. EOF done
I don’t know in shell or perl but in python would be:
#!/usr/bin/python for i in xrange(1000): with open('file%0.3d' %i,'w') as fd: fd.write('some text')
I think is pretty straightforward what it does.
I tried your script, but I’m getting an error: ./crea.py:4: Warning: ‘with’ will become a reserved keyword in Python 2.6 File «./crea.py», line 4 with open(‘file%0.3d’ %i, ‘w’) as fd: ^ SyntaxError: invalid syntax
That would be from __future__ import with_statement (the system ate the underscores in ghostdog74’s comment).
You can use only Bash with no externals and still be able to pad the numbers so the filenames sort properly (if needed):
read -r -d '' text do printf -v filename "file%04d" "$i" echo "$text" > "$filename" done
Bash 4 can do it like this:
for filename in file; do echo $text > $filename; done
Both versions produce filenames like «file0001» and «file1000».
Just take any big file that has more than 1000 bytes (for 1000 files with content). There are lots of them on your computer. Then do (for example):
split -n 1000 /usr/bin/firefox
This took only 0.253 seconds for creating 10000 files.
Only 1.974 seconds for 100k files with about 5 bytes each.
If you only want files with text, look at your /etc directory. Create one million text files with almost random text:
split -n 1000000 /etc/gconf/schemas/gnome-terminal.schemas
20.203 seconds for 1M files with about 2 bytes each. If you divide this big file in only 10k parts it only takes 0.220 seconds and each file has 256 bytes of text.
Жизнь — это движение! А тестирование — это жизнь 🙂
Допустим, у нас есть некая папка test. Создадим в ней сотню директорий и кучу файликов в каждой:
Вот и все, дальше можно играться с ними!
- mkdir — создать директорию
- touch — создать файл (или изменить существующий, но если файла с таким именем нет, то команда создаст новый, пустой)
А выражения в скобках играют роль функции, которая выполняется в цикле и делает ручную работу за вас:
Можете опробовать эти команды на нашей тестовой площадке. Но там я рекомендую создать себе отдельную папку с песочницей, чтобы понять, что она ваша — по нику или имени, или как-то еще.
Я нашла их в книге «Командная строка Linux. Полное руководство», они используются для того, чтобы создать песочницу для прощупывания команды find . Я, как и автор, восхищаюсь мощью командной строки в данном случае. Всего 2 строчки, а сколько боли бы принесло сделать похожую структуру через графический интерфейс!
В книге, правда, команды были чуть сложнее:
Но на хабре мне подсказали, что можно и сократить!
Книга хорошая, очень рекомендую =)
1 комментарий:
полезная команда в копилку:
fallocate
Позволяет создать файл заданного размера. Например
fallocate -l 16K 16K.txt
создаст файл размером в 16 килобайт Ответить Удалить
How to Create Thousands/Millions Files in Linux
Do you need an ultra fast way to create a large number of files in Linux? Perhaps, you are doing some load testing for an application and you need to create 1000 or even 1,000,000 files in the matter of seconds. Well, here is how to do it!
There are two parts to creating these files. First, is creating a single master file that contains the data that the thousands/millions files are based on. Second, splitting this master file into the total number of files that you require.
Linux File Creation – 10,000 Files
Create the master file
- Determine the number of files and the size of each file that you require
- Multiply the total number of files times the size (in bytes). For example: If you want to create 10000 files that are 10 bytes each, do 10000 * 10 = 1,000,000. This represents the size of the master file that is needed.
- To create this master file, run the command:
dd if=/dev/zero of=masterfile bs=1 count=1000000
The above command will create a 1 MB file called ‘masterfile’. This file contains all 0’s. If you would like it to contain random binary data, use /dev/urandom
Split the master file into thousands of pieces
- Now that the master file is created, you can now use this to generate the desired 10,000 files that are 10 bytes each.
- Run the command:
split -b 10 -a 10 masterfile
The -b option specifies the size in bytes of each file. The -a option defines the length of the filename of the new files +1 (-a 10 means create a 11 character long filename)
5 Ways to Create a File in Linux
1. Creating a File using the touch Command
This is the simplest way to create a new file.
2. Creating a File using the echo Command
The echo command can be used to create a file and write data to it.
echo "This is some text" > newfile.txt
3. Creating a File using the cat Command
The cat command allows you to create a new file and enter the content in one go.
4. Creating a File using the printf Command
Similar to echo , the printf command can also be used to create a file.
printf "This is some text" > newfile.txt
5. Creating a File using a Text Editor
You can use a text editor like nano , vi , or emacs to create a file.