Five Useful Linux DD Examples

Written by Indirekt on . Posted in Programming

Linux dd is a common Linux program whose primary purpose is the low-level copying and conversion of raw data. You can backup whole hard drives, create a large file filled with only zeros, create and modify image files at specific points, and even do conversions to upper case.

To display dd’s help simply enter:
erik@debian:~$dd –help
Alright, lets get to the juicy stuff.

1. Create an ISO of a your favourite CD just for storage purposes with linux dd command:
dd if=/dev/cdrom of=/home/erik/myCD.iso bs=2048 conv=sync
Breaking down the linux dd command:
+ if is “input file”, so in this case our cdrom drive at /dev/cdrom
+ of is “output file”, in this case myCD.iso
+ bs is “block size”, in this case 2048 bytes per block
+ conv is for conversion, in this case we are using “sync” which tells DD to execute synchronized input and output, this is needed for the CD-ROM as we want to read a whole block to ensure no data loss occurs.

2. Replicate one hard disk partition to another hard disk with dd:
dd if=/dev/sda1 of=/dev/sdb1 bs=4096 conv=noerror
In this case everything is the same as example 1 but our conversion methods states that noerror should be executed, this tells DD to continue after read errors.

3. Create a file with 1MB of random bytes with dd:
erik@debian:~$dd if=/dev/urandom bs=1024 count=1000 of=fun.bin
1000+0 records in
1000+0 records out
1024000 bytes (1.0 MB) copied, 0.198349 s, 5.2 MB/s
This time I stated that our block size is 1024 bytes, and we are going to make 1000 of them sequentially. I also used the built-in kernel device urandom which provides random bytes.

4. Skip first 128K of input file then write remaining with dd:
dd if=/home/erik/fun.bin skip=128k bs=1 of=/home/erik/fun2.bin
The skip command tells DD to move passed the (in this case) 128k of data in fun.bin then write the rest to fun2.bin. This can be useful if you have a large file that needs to be written across more than one partition. For instance, if you had 3 partitions each 128k. You wouldn’t want to write the same 128k to each partition, you would want to write the first 128k to partition 1, then from 128k-256k of the file to partition 2 and so on.

5. Using dd to convert a file to uppercase:
dd if=erik.txt of=erik_up.txt conv=ucase
Finally, we use conv again to do a conversion. In this case we convert with the specifier of ucase.

What is your favourite use of the linux dd command?

I have used dd on more occasions then I can recall. It definitely is a tool that provides hard drive copying, backup, and replication without having to dig too deep into Linux internals. Another neat use of dd is to send data across a network socket, so you can use dd as the packet data generator with the socket pre opened.

If you are surfing for more info about the niche of internet marketing, then please check out the site that is quoted in this line.

Incoming search terms:

  • co2 carbon capture sequestration
  • dd examples

Remove Variables In The Bash Shell Environment

Written by Indirekt on . Posted in Programming

The Bash shell (bourne-again shell) is the most common default shell on most Linux distributions. Having a working knowledge of the environment is useful for any novice Linux user. This article will outline how to view, set, change, and retain environment variables in your shell.

Out-of-the-box Linux installs have a somewhat long list of environment variables that are set for various programs to use, so do not be alarmed by the amount of output.

1. How To View Bash Shell Environment Variables Using env
To view all the variables currently set in your linux bash shell environment you can enter the command env. This will output all variables currently set in your environment. For example:

erik@debian:~$ env
TERM=xterm
SHELL=/bin/bash
SSH_CLIENT=X.X.X.X 51688 22
SSH_TTY=/dev/pts/2
USER=erik
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.flac=01;35:*.mp3=01;35:*.mpc=01;35:*.ogg=01;35:*.wav=01;35:
SSH_AUTH_SOCK=/tmp/ssh-Hdelx27792/agent.27792
MAIL=/var/mail/erik
PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
PWD=/home/erik
LANG=en_CA.UTF-8
HISTCONTROL=ignoredups
SHLVL=1
HOME=/home/erik
LOGNAME=erik
SSH_CONNECTION=X.X.X.X 51688 X.X.X.X 22
LESSOPEN=| /usr/bin/lesspipe %s
DISPLAY=localhost:11.0
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/env

As you can see by the output, there are a large set of values. Some of which make logical sense. For instance, HOME=/home/erik is my home directory upon login. My current working directory of PWD=/home/erik. An interesting variable is the _=/usr/bin/env displays the last command executed by me.

2. Display Specific Environment Variable In Bash Using echo $VAR
Lets say there is a specific variable that you want to view instead of displaying the whole set and grepping for that variable. You can just enter the command echo $VAR where $VAR is the environment variable you want to view.

erik@debian:~$ echo $SHELL
/bin/bash

3. Create Environment Variable Temporarily In Bash
To temporarily create a variable in the linux bash shell you can simply enter erik=”hi” . Then doing a echo $erik the prompt will display “hi”. However, in doing so, the variable will not show up in a env output. To modify the variable you can:

erik@debian:~$ echo $erik
hi
erik@debian:~$ erik=”no”
erik@debian:~$ echo $erik
no

These methods of creating variables is local only to this shell and will not be seen to child shells.

4. Creating Environment Variable In Bash Using export
For a linux shell variable to persist to a child shell, and also show up during an env output, the user must use the export feature.

erik@debian:~$ data=hi
erik@debian:~$ echo $data
hi
erik@debian:~$ env | grep data
erik@debian:~$ export data
erik@debian:~$ env | grep data
data=hi

In this example I have created an %KEYWORD2 called data in which a child shell could use. Keep in mind that this variable is only present in this session. If you were to logout, then log back in, the data variable would disappear.

5. Removing Environment Variable In Bash Using unset
At this point we now have the data variable in our environment. But perhaps we want to remove it because it smells bad. Its actually quite easy to do so.

erik@debian:~$ echo $data
hi
erik@debian:~$ env | grep data
data=hi
erik@debian:~$ unset data
erik@debian:~$ echo $data

erik@debian:~$ env | grep data
erik@debian:~$

So in this example I showed the data variable is set, and can be found in our environment. I then ran the unset command to remove the variable. I then tried to display the contents of our variable, and as you can see it no longer has a value and is no longer part of our environment.

Stay tuned for my next article where I show you how to create an environment variable from a C process.

Bloggers that are searching for more info about the topic of internet marketing, please go to the web page which is mentioned in this passage.

Incoming search terms:

  • bash remove environment variable
  • how to remove variable in bash