How to write random or zero characters to a device in Linux ?

In this post we will look at an interesting feature of linux to write zeros or random characters to any device in Linux.

One old way is to use dd command with reference of /dev/zero or /dev/random.

However we will use shred command in this example to simplify our job.

Writing Random Characters to a device

### For random characters, use -n1 with shred command.
bash-4.2# screen -L -dm bash -c 'echo $(date; hostname); shred --verbose -n1 /dev/sdb1 2>&1;'

### The logs were saved in screenlog.0 saved in current working directory.
bash-4.2# ls -ltr
total 0
-rw-r--r-- 1 root root 0 May  3 16:48 screenlog.0

bash-4.2# pwd
/home/ngelinux/

### Lets view the progress of random write.
bash-4.2# tail -f screenlog.0 
Thu May 3 16:48:29 GMT 2018 
NGELINUX
shred: /dev/sdb1: pass 1/1 (random)...
shred: /dev/sdb1: pass 1/1 (random)...1.0GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (random)...2.2GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (random)...3.4GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (random)...4.6GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (random)...5.8GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (random)...6.9GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (random)...8.2GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (random)...9.5GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (random)...10GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (random)...11GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (random)...13GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (random)...14GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (random)...15GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (random)...17GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (random)...18GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (random)...19GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (random)...20GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (random)...21GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (random)...23GiB/1.1TiB 2%
shred: /dev/sdb1: pass 1/1 (random)...24GiB/1.1TiB 2%
shred: /dev/sdb1: pass 1/1 (random)...25GiB/1.1TiB 2%
shred: /dev/sdb1: pass 1/1 (random)...26GiB/1.1TiB 2%
shred: /dev/sdb1: pass 1/1 (random)...27GiB/1.1TiB 2%
shred: /dev/sdb1: pass 1/1 (random)...29GiB/1.1TiB 2%
shred: /dev/sdb1: pass 1/1 (random)...30GiB/1.1TiB 2%
shred: /dev/sdb1: pass 1/1 (random)...1013GiB/1.1TiB 90%
shred: /dev/sdb1: pass 1/1 (random)...1014GiB/1.1TiB 90%
shred: /dev/sdb1: pass 1/1 (random)...1015GiB/1.1TiB 90%
shred: /dev/sdb1: pass 1/1 (random)...1017GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (random)...1018GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (random)...1019GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (random)...1020GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (random)...1022GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (random)...1023GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (random)...1.0TiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (random)...1.0TiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (random)...1.1TiB/1.1TiB 100%

Writing Zeros to the disk

### For zero write, put -n0 with shred command.
bash-4.2# screen -L -dm bash -c 'echo $(date); shred --verbose -n0 -vz /dev/sdb1 2>&1;'

### View the progress of the command.
bash-4.2# tail -f screenlog.0 
Thu May 3 18:07:02 GMT 2018
shred: /dev/sdb1: pass 1/1 (000000)...
shred: /dev/sdb1: pass 1/1 (000000)...1.7GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (000000)...3.6GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (000000)...5.4GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (000000)...7.2GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (000000)...9.1GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (000000)...10GiB/1.1TiB 0%
shred: /dev/sdb1: pass 1/1 (000000)...12GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (000000)...14GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (000000)...16GiB/1.1TiB 1%
shred: /dev/sdb1: pass 1/1 (000000)...1014GiB/1.1TiB 90%
shred: /dev/sdb1: pass 1/1 (000000)...1016GiB/1.1TiB 90%
shred: /dev/sdb1: pass 1/1 (000000)...1018GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (000000)...1020GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (000000)...1021GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (000000)...1023GiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (000000)...1.0TiB/1.1TiB 91%
shred: /dev/sdb1: pass 1/1 (000000)...1.1TiB/1.1TiB 100%

Leave a Reply

Your email address will not be published.