Bash script to generate multiple files with fixed size - bash

I need a simple bash script or a line to generate a number of files with a fixed size, I need to be able to choose the site and the number either in the file or by input.
I tried solutions I found online, but they are either about creating multiple files, or about creating a single file with fixed size, not both things together.
For example:
for i in {1..5}; do dd if=/dev/urandom bs=1024 count=50 of=test.bin
This still creates only a single file though, not multuple.

As Tom K pointed there is a problem with your for loop.
It should be something like
#!/bin/bash
for i in 1 2 3 4 5
do
dd if=/dev/urandom bs=1024 count=50 of=test-${i}.bin
done
resulting:
ll test-*
-rw-r--r-- 1 root root 50K Aug 20 23:48 test-1.bin
-rw-r--r-- 1 root root 50K Aug 20 23:48 test-2.bin
-rw-r--r-- 1 root root 50K Aug 20 23:48 test-3.bin
-rw-r--r-- 1 root root 50K Aug 20 23:48 test-4.bin
-rw-r--r-- 1 root root 50K Aug 20 23:48 test-5.bin
Edit.
Or onliner if you prefer:
for i in {1..5}; do dd if=/dev/urandom bs=1024 count=50 of=test-${i}.bin; done

Related

Redirection to a variable has weird behaviour

I have this small script:
#!/bin/bash
for file in "$(ls | grep -v $0)";do
cat $file > "${file}-test"
done
On this directory:
total 40
-rwxr-xr-x 1 root root 783 Dec 11 09:19 appendToLog.sh
-rwxr-xr-x 1 root root 3995 Dec 11 13:22 con2dd.py
-rwxr-xr-x 1 root root 362 Dec 11 13:26 dd.py
-rw-r--r-- 1 root root 566 Dec 11 13:26 dd.pyc
-rw-r--r-- 1 root root 18558 Dec 25 11:24 moshe.log
-rw------- 1 root root 0 Dec 11 09:20 nohup.out
-rwxr-xr-x 1 root root 88 Dec 25 11:28 task.sh
-rwxr-xr-x 1 root root 560 Dec 11 10:33 test.py
Nevermind that I can achieve that with cp, I want to understand why this exactly is producing this file:
-rw-r--r-- 1 root root 24912 Dec 25 11:28 appendToLog.sh?con2dd.py?dd.py?dd.pyc?moshe.log?nohup.out?task.sh?test.py-test
And nothing else.
The problem is parsing output of ls is just wrong (see Why you shouldn't parse the output of ls(1), filenames in unix can have almost any special characters including whitespace, newlines, commas, pipe symbols. Its because you've quoted the output of ls in one construct, you have a single list of all the files concatenated as one string in the value of "${file}-test" which is quite not what you wanted to do.
Also notice how ls sometimes garbles your filename data (in our case, it turned the \n character in between the words into a ? question mark (could indicate a character that cannot be displayed).
Just use the glob expansion in bash to list the files and do actions on them.
for f in *; do
[[ -e $f ]] || continue
...
done
That said, You could probably have some non-printable characters on end of lines (eg. CRLF imported rom Windows)
Run cat -A scriptname it'll show you all characters in your script. Then, you can convert to unix-like format running dos2unix scriptname.

Why the shell doesn't write the good hour and give me the year?

I'm a little bit confused, i changed my time on one file with the shell command :
touch -t = touch -t 201606012135 trial01
But after the ls -lt, I got this :
-rw-r--r-- 1 CharleyD staff 87 1 jun 2016 trial01
drwxr-xr-x 15 CharleyD staff 510 3 apr 12:57 Hybrid_proj
Why the shell doesn't write the hours like the "Hybrid_proj" directory for the "trial01" ? The trial01 file have the hour : 21:35, so itsn't empty.
Indeed, I search to get this in output :
-rw-r--r-- 1 CharleyD staff 87 1 jun 21:35 trial01
drwxr-xr-x 15 CharleyD staff 510 3 apr 12:57 Hybrid_proj
How I can do this ?
Thx a lot buddies to enlighten my way ! ;)
If a file is not from the current year, ls defaults to showing the year instead of the time. The time is still correctly set, just formatted differently.
To always show the full time, with GNU ls, you can use ls --time-style=long-iso -l:
$ ls --time-style=full-iso -l
total 0
-rw-r--r-- 1 user user 0 2017-04-04 13:20 newfile
-rw-r--r-- 1 user user 0 2016-04-03 12:34 oldfile
With BusyBox ls, you can use -e:
$ busybox ls -e
total 0
-rw-r--r-- 1 user user 0 Tue Apr 4 13:20:42 2017 newfile
-rw-r--r-- 1 user user 0 Sun Apr 3 12:34:00 2016 oldfile
With macOS ls, you can use -lT:
$ ls -lT
total 0
-rw-r--r-- 1 user group 0 Apr 4 13:19:35 2017 myfile
-rw-r--r-- 1 user group 0 Apr 3 12:34:00 2016 oldfile
In each case, you get a long timestamp with the same format for older and newer files.
Use the -T option if your ls supports it.

Find newest file in folder via the terminal (seconds apart)

I am trying to extract the newest file in folder.
I tried this:
ls -1t | head -1
But the problem is, that this command doesn't seem to differentiate in which second the file was created - if multiple files were created in the the same, this command just give the first file in this minute.
Is there a way to make this command more precise?
You should add T argument to the options to do that:
ls -1tT | head -1
See the output when l argument is added:
ls -tTl
-rw-r--r-- 1 user wheel 0 Apr 23 17:54:27 2016 1
-rw-r--r-- 1 user wheel 0 Apr 23 17:54:19 2016 3
-rw-r--r-- 1 user wheel 0 Apr 23 17:54:12 2016 2

How to Loop and pick every string in a file and search the same from a folder

I have a folder called error which contains some 1000 files like below:
-rw-r--r-- 1 orartkp6 dba 1298 Apr 19 09:23 BEN_INV_5_0900091010993_20160419092353.xml.err
-rw-r--r-- 1 orartkp6 dba 1289 Apr 19 09:23 BEN_INV_5_0900091010994_20160419092353.xml.err
-rw-r--r-- 1 orartkp6 dba 1286 Apr 19 09:23 BEN_INV_5_0900091010995_20160419092353.xml.err
-rw-r--r-- 1 orartkp6 dba 1292 Apr 19 09:23 BEN_INV_5_0900091010996_20160419092353.xml.err
-rw-r--r-- 1 orartkp6 dba 1300 Apr 19 09:23 BEN_INV_5_0900091010997_20160419092353.xml.err
Now I have a .txt file which has say only 500 of the above file names.
SO now I have to write a script to read every line from the .txt file and search inside the error folder and move only those 500 files into a new folder (backup_folder).
Can you help me how to do this?
the logic behind this would be,
read only $2 column that is (i suppose) the names of the files,
and in a while loop output each one in a variable and then use mv.
awk '{print $2; var=$2; mv $var dir_to_move}' 500lines.txt
hope i helped.
I used the below while loop to solve this problem.
while read file
do
cp -p "$file" ./backup_folder
done < 500lines.txt

How Can I Create Multiple Files of Random Data?

I've been using dd if=/dev/random of=<file_name> bs=XX count=XX to create single files of random data. But I wondered how I could create multiple files at once. Say 5, 10, or 80,000. I need to do some bandwidth testing and need unique data. The above code works great for 1 or 2 files. My programming (Python) and terminal skill are still very minimal.
Any help would be awesome.
man split.
Split splits a file or stdin to multiple files based on file size or number of lines. It doesn't happen "at once" or in parallel though.
dd if=/dev/random bs=1 count=10 | split -b 2 produces 5 files xaa,xab..xae each consisting of 2 bytes.
I would use GNU Parallel for this. The command line syntax is very intuitive and concise and it does all your work in parallel, i.e. FAST, and uses all those lovely CPU cores in your Mac.
So, to create 8 files in parallel, each of 100MB, you would type this in the Terminal:
$ parallel dd if=/dev/random of=random-{} bs=1000000 count=100 ::: {0..7}
and you will end up with these 8 files just 60 seconds later:
$ ls -l random-*
-rw-r--r--# 1 mark staff 100000000 19 Dec 11:52 random-0
-rw-r--r--# 1 mark staff 100000000 19 Dec 11:52 random-1
-rw-r--r--# 1 mark staff 100000000 19 Dec 11:52 random-2
-rw-r--r--# 1 mark staff 100000000 19 Dec 11:52 random-3
-rw-r--r--# 1 mark staff 100000000 19 Dec 11:52 random-4
-rw-r--r--# 1 mark staff 100000000 19 Dec 11:52 random-5
-rw-r--r--# 1 mark staff 100000000 19 Dec 11:52 random-6
-rw-r--r--# 1 mark staff 100000000 19 Dec 11:52 random-7
Or, if you wanted one file of 1kB, two files of 64kB, one file of 32kB and one of 128kB, you would do this:
$ parallel dd if=/dev/random of=random-{%} bs=1024 count={1} ::: 1 64 64 32 128
which will give you this:
-rw-r--r-- 1 mark staff 131072 19 Dec 12:10 random-5
-rw-r--r-- 1 mark staff 32768 19 Dec 12:10 random-4
-rw-r--r-- 1 mark staff 65536 19 Dec 12:10 random-3
-rw-r--r-- 1 mark staff 65536 19 Dec 12:10 random-2
-rw-r--r-- 1 mark staff 1024 19 Dec 12:10 random-1
The easiest way, to my mind, of installing parallel on OSX is to get homebrew from the homebrew website, then all you do is:
brew install parallel

Resources