One day a shell command text operation series-linux dd use tutorial _linux shell

Source: Internet
Author: User
Tags lowercase

The first day of writing today, first of all, the basics of writing shell scripts

1. The shell script is extended with. SH, usually running./${filename}.sh or SH ${filename}.sh
2. Shell script begins with #!/bin/bash #! read as "Shebang"
3. Open Debugging #!/bin/bash–xv
4./dev/null any thing thrown in will disappear, Linux black hole. /dev/zero is used for initialization, resulting in 0

We'll introduce the DD command today.

Origin (full name): should be based on its function description "convert an" named "CC", but "CC" has been used to represent "ccomplier", so named "DD"

Opening example:

1. DD If=infile.log of=outfile.log bs=1m count=1

Copy infile.log content to Outfile.log. A read-write bit of 1M, read and write one block at a time (1M, determined by BS)

2. Echo-n "Hello World" | DD Cbs=1 Conv=unblock 2>/dev/null

Main demo CBS, one byte at a time, Conv=unblock is to replace the last space in the CBS size with a newline character 2> is an error message output stream/dev/null the beginning of the introduction

Interpreting Help:

(Do not want to see such a large paragraph, try to skip, read interpretation)

Usage: dd [operand] ...
Or: DD option
Copy the files and format them according to the operands.

bs= bit number of bits once read and write
The number of bits converted at one cbs= bit
Conv=convs convert files according to each comma-separated list of flags
count= blocks only copy the specified number of blocks to a block
ibs= bit number of bits read at a time (default: 512)
if= file read from specified file
The iflag= symbol is read as specified by a comma-delimited list of symbols
obs= bits at once write the specified number of bits (default: 512)
of= files to the specified file
The oflag= symbol is written in a comma-delimited list of symbols specified
seek= blocks the specified number of blocks at the beginning of the output
skip= blocks the specified number of blocks at the beginning of the input
Status=noxfer Prohibit transmission statistics

The block and the number of bytes may be followed by one or more of the following suffixes:
C =1, W =2, b =512, KB =1000, K =1024, MB =1000*1000, M =1024*1024, XM =m
GB =1000*1000*1000, G =1024*1024*1024, and so in for T, P, E, Z, Y.

Each CONV symbol may be:

ASCII is converted from EBCDIC code to ASCII code
EBCDIC from ASCII code to EBCDIC code
IBM converts from ASCII code to replacement EBCDIC code
Block replaces the line break in the end character blocks with equal-length spaces
Unblock replaces the trailing space in a block of CBS size with a newline character
LCase converts uppercase characters to lowercase
UCase converts lowercase characters to uppercase
Swab Exchange each pair of input data bytes
Sync fills each input block with a nul character to the size of the IBS;
Or unblock, it is filled with a space instead of a nul character
Excl FAIL if the output file already exists
Nocreat do not create the output file
Notrunc does not truncate output file
NOERROR read data continues after an error has occurred
Write output file data to disk before Fdatasync ends
Fsync is similar to the above, but the metadata is also written together

The FLAG symbol can be:

Append Append mode (only meaningful for output; Conv=notrunc implied)
Direct I/O access mode
Directory fails unless directory is a catalog
Dsync using synchronous I/O access mode
Sync is similar to the above, but it also takes effect on the metadata
Fullblock accumulate complete blocks for input (Iflag only)
Nonblock using non-blocking I/O access mode
Noatime does not update access time
NoCache Discard Cached data
Noctty does not assign control terminals according to file
Nofollow does not follow linked files

Interpretation:

1. BS IBS OBS CBS

BS: once read/write = Ibs+obs
IBS: Once read
OBS: Write Once
CBS: one Conversion

2. If of

If is a read file
Of is the output file

3. Seek Skip

Skip how many bits to skip when reading from If
How many bits are skipped when the Seek is written to

Examples of actual combat:

1. Batch generation of random name test files

For i in {10..10}  
does  
dd If=/dev/zero of=junk.test$i bs= "$RANDOM" K count=20  
Done

2. Backup and Recovery

Backup

DD if=abc.gz OF=ABC.GZ.BAK1 bs=1k count=10000  
dd if=abc.gz of=abc.gz.bak2 bs=1k skip=10000 count=70000  

The recovery method is as follows:

DD IF=ABC.GZ.BAK1 of=abc.gz  
dd if=abc.gz.bak2 of=abc.gz bs=1k seek=10000  

3. Convert case

Generate uppercase: DD if=dd.txt of=my.log bs=1m count=1 conv=ucase

Generate lowercase: To not be lazy, leave the reader to try

4. Copy Yourself

Copy Code code as follows:

File_subscript=copy
DD if=$0 of=$0. $file _subscript 2>/dev/null

5. To copy the contents of a floppy disk to another floppy disk, use/tmp as a temporary storage area. Insert the source disk into the drive and enter the following command:

$ dd if =/dev/fd0 of =/tmp/tmpfile

After the copy is complete, remove the source disk from the drive, insert the target disk, and enter the command:

$ dd if =/tmp/tmpfile of =/dev/fd0

After the floppy copy is complete, you should delete the temporary file:

$ rm/tmp/tmpfile

6. Write the net.i file to the floppy disk and set the number of read/write buffers.

(Note: The contents of the floppy disk will be completely overwritten)

$ dd if = net.i of =/dev/fd0 bs = 16384

7. Copy the file sfile to the file Dfile.

$ dd If=sfile Of=dfile

8. Create a 100M empty file

DD If=/dev/zero of=hello.txt bs=100m count=1

=============================================

/dev/null, nicknamed the Bottomless pit, you can output any data to it, it take all, and will not hold!
/dev/zero is an input device that you can use to initialize the file.

/dev/null------It is an empty device, also known as a bit bucket (bit bucket). Any output written to it will be discarded. If you do not want the message to be displayed or written to the file in standard output, you can redirect the message to the bucket.
/dev/zero------The device provides an endless supply of 0, you can use any number you need-the equipment to provide much more. He can be used to write string 0 to a device or file.
$ dd If=/dev/zero of=./test.txt bs=1k count=1
$ ls-l
Total 4
-rw-r--r--1 Oracle DBA 1024 16:56 test.txt

eg

$ find/-name Access_log 2>/dev/null


This article comes from "Scholar" blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.