Linux command chown, chgrp, cmp, dd, du

Source: Internet
Author: User
Chown is a command used to set the file owner and file association group in Unix systems. Super User permissions are required to execute this command

Chown command

Chown is a command used to set File owners and file associations in Unix systems. Super User permissions are required to execute this command. Only the file owner of the file group and the super user can change the file association group. If a non-privileged user (non-super user) needs to set an association group, the chgrp command may be used.

Usage

[Plain] view plaincopyprint? 01.# Chown root/var/run/httpd. pid 
# Chown root/var/run/httpd. pid: Set the owner of/var/run/httpd. pid to root.

[Plain] view plaincopyprint? 01.# Chown rob: developers strace. log 
# Chown rob: developers strace. log: Set the owner of strace. log to rob, and set the association Group of the file to developers.

[Plain] view plaincopyprint? 01.# Chown: 512/home 
# Chown: 512/home: Set the/home Association group to 512 (Association group ID) without changing the owner.

[Plain] view plaincopyprint? 01.# Chown-R us base 

# Chown-R us base: Set the owner of base and all files in its directory to us. The-R option recursively modifies the owner and group users of all files in the directory hierarchy.

Chgrp command

Chgrp is a command used in Unix systems to set the group to which the file belongs. Unlike the chown command, chgrp allows common users to change the group to which the file belongs,

As long as the user is a member of the group.

Usage

The syntax for calling chgrp is

Chgrp group name target 1 [Target 2...]

"Group name" is the group to be associated with the file.

"Target 1 [Target 2...]" is a list of files or directories in the group to be changed.

Example

[Plain] view plaincopyprint? 01.$ Ls-l testfile
02.-rw-r -- 1 unixguy staff 545 Nov 04 2004 ttt
03. $ chgrp system testfile
04. $ ls-l ttt
05.-rw-r -- 1 unixguy system 545 Nov 04 2004 ttt
 
$ Ls-l testfile
-Rw-r -- 1 unixguy staff 545 Nov 04 2004 ttt
$ Chgrp system testfile
$ Ls-l ttt
-Rw-r -- 1 unixguy system 545 Nov 04 2004 ttt

[Plain] view plaincopyprint? 01.Chgrp staff/u 
Chgrp staff/u: change the/u group to "staff ".

[Plain] view plaincopyprint? 01.Chgrp-hR staff/u 
Chgrp-hR staff/u: change the group of all files in/u and its subdirectories to "staff ".

The above Command changes the group to which the file "testfile" belongs from "staff" to "system ". (The attacker must belong to the "staff" group .)

Du command

Du (disk usage) is a standard Unix program used to estimate the disk usage of a specific directory or file on a file system.

By default, a Single UNIX system requires that du displays the disk space allocated to each file and directory in the current directory. The link file will display its own size rather than its linked object; the directory will display its content size.

Because du only reflects the disk allocation rather than the actual file size, if the disk is not released after the file is deleted, the displayed number is different from the number reported by df.

Cmp command

Function description: compares two files. When the two files are compared to each other, the command does not display any information. If there are differences, the default value indicates the character and number of the columns in each difference. If no file name is specified or the given file name is "-", the cmp command reads data from the standard input device.

Syntax: cmp [-c [v] [-I <字符数目> ] [-- Help] [first file] [second file]

Parameters:

-C or-print-chars displays the character corresponding to this character in addition to the decimal code indicating the difference.

-I <字符数目> Or-ignore-initial = <字符数目> Specify a number.

-L or-verbose indicates all the different places.

-S or-quiet or-silent do not display error messages.

-V or-version displays version information.

-Help online help.
Example:

1. cmp prog. o. bak prog. o

This compares prog. o. bak and prog. o. If the file is the same, no message is displayed. If the file is different, the first location is displayed. for example:
[Plain] view plaincopyprint? 01.Prog. o. bak prog. o differ: char 4, line 1 
Prog. o. bak prog. o differ: char 4, line 1 [plain] view plaincopyprint? 01.If the message cmp: EOF on prog. o. bak is displayed, the first part of prog. o. bak is the same as that of prog. o. bak, but other data exists in prog. o. 

If the message cmp: EOF on prog. o. bak is displayed, the first part of prog. o. bak is the same as that of prog. o. bak, but other data exists in prog. o. 2. cmp-l proj. o. bak prog. o

This compares the file and then displays the number of bytes (in decimal format) and each different byte (in octal format ). For example, if the fifth byte is octal 101 in prog. o. bak and prog. o is 141, cmp displays:
[Plain] view plaincopyprint? 01.5 101 141
5 101 141
3. [plain] view plaincopyprint? 01. cmp-s prog. c. bak prog. c
Cmp-s prog. c. bak prog. c

In this way, if the file is the same, the value 0 is given. if the file is different, the value 1 is given, or if an error occurs, the value 2 is given.

Du can contain a parameter, that is, specifying a path to work. if it is not specified, the current directory is used. SUS specifies the following options for du:

-A: displays each file (excluding directories) in the current directory as an entry.

-H: calculates the disk usage of the linked object specified in the command line.

-K: divide the size by 1024 bytes, rather than 512 bytes.

-L: calculates the usage of any linked object.

-S: only reports the total disk usage of the current directory. no file is displayed.

-X: only the files and directories on the device where the specified path is located are traversed.

-H: the size of the disk usage plus the unit suffix is displayed as a form that is easier to understand.

Example

Total directory size in KB units:
[Plain] view plaincopyprint? 01.$ Du-sk *
02. 152304 directoryOne
03. 1856548 directoryTwo
 
$ Du-sk *
152304 directoryOne
1856548 directoryTwo more readable directory size format (Byte, KB, MB, GB, TB, PB ):
[Plain] view plaincopyprint? 01.$ Du-sh *
02. 149 M directoryOne
03. 1.8G directoryTwo
 
$ Du-sh *
149 M directoryOne
The current 1.8G directoryTwo directory contains hidden files, and the disk usage of subdirectories and files (sorted from small to large ):
[Plain] view plaincopyprint? 01.$ Du-sk. [!.] ** | Sort-n 
$ Du-sk. [!.] ** | Sort-n the current directory contains hidden files, subdirectories, and disk usage of files (sorted from large to small ):
[Plain] view plaincopyprint? 01.$ Du-sk. [!.] ** | Sort-nr 
$ Du-sk. [!.] ** | Sort-nr: displays the directory size with a depth of 1 and its sum.
[Plain] view plaincopyprint? 01.$ Du-d 1-c-h 
$ Du-d 1-c-hdd command
Dd is a command on UNIx and Unix-like systems. its main function is to convert and copy files.

On Unix, hardware device drivers (such as hard disks) and special device files (such as/dev/zero and/dev/urandom) appear in file systems like normal files; as long as the corresponding functions are implemented in their respective drivers, dd can also read and/or write to these files. In this way, dd can also be used to back up the boot sector of hardware and obtain a certain amount of random data. The dd program can also process data during replication, such as converting the byte order or interchange the ASCII and EBCDIC encoding.

The command line statement of dd is different from other Unix programs, because its command line option format is option = value, rather than the more standard -- option value or-option = value. Dd reads data from the standard input and writes the data to the standard output by default. However, you can use the options if (input file, input file) and of (output file, output file) to change the data.

Block size

A block is the unit of measurement for reading, writing, and converting bytes at a time. The command line option can specify a different block size for the input/Read (ibs) and output/write (obs), although the block size (bs) option will overwrite the ibs and obs options. The default block size for input and output is 512 bytes (traditional disk block size and POSIX defined "block size) the copied count option, the read skip option, and the written seek option are all in blocks. The conversion operation is also affected by the block size (cbs.

In some functions of dd, the block size may affect the performance. For example, when converting data in a hard disk, a smaller block size usually causes more bytes to be converted. Reading many small pieces is a waste of overhead and may have a negative impact on execution performance. A large block size may increase the replication speed. However, since the number of bytes to be copied is given by bs × count, it is impossible to copy the prime byte at a time, unless you use one or two bad options bs = N count = 1 (memory consumption) or bs = 1 count = N (large read request overhead ). The substitution program (see below) allows specifying bytes instead of blocks.

When used for network transmission, the block size may conflict with the packet size according to the network protocol used.

The value provided to the block size is interpreted as a decimal integer, or you can add a specified suffix multiple. The suffix w represents 2 times, B represents 512 times, k represents 1024 times, M represents 1024 × 1024 times, G represents 1024 × 1024 × 1024 times, and so on. In addition, in the block size and counting parameters, some implementations can also use x to represent multiplication.

For example, the block size bs = 2x80x18b indicates 2 × 80 × 18 × 512 = 1474560 bytes, that is, the exact size of a 1440 KiB floppy disk.

Data Conversion

Dd can copy data between files, devices, partitions, and volumes. Data can be input or output from anywhere, but there are important differences when outputting data to a partition. In addition, you can use the conv option to modify the data during transmission to adapt to the media. If the last block has an unexpected length, attempting to copy the entire disk using cp may ignore it; however, dd may succeed. The source and target disks must have the same size.
Dd format in different situations
Dd if =/dev/sr0 of = myCD. iso bs = 2048 conv = noerror, sync creates an ISO disk image from the CD-ROM.

Dd if =/dev/sda2 of =/dev/sdb2 bs = 4096 conv = noerror clone one partition to another.

Dd if =/dev/ad0 of =/dev/ad1 bs = 1 M conv = noerror clone hard disk "ad0" to "ad1 ".

The noerror option means that if an error occurs, the program will continue to run. The sync option indicates filling each block to the specified byte.
Backup and Recovery of Master Boot Records

The primary boot record can be repaired. The master boot record can be transferred to or from a file.

The first two sectors of the floppy disk to be copied:
[Plain] view plaincopyprint? 01.Dd if =/dev/fd0 of = MBRboot. img bs = 512 count = 2 
Dd if =/dev/fd0 of = MBRboot. img bs = 512 count = 2 the image to create the entire x86 primary boot (including the MS-DOS partition table and MBR magic byte ):
Dd if =/dev/sda of = MBR. img bs = 512 count = 1

To create an image that only contains the boot code of the primary boot record (excluding the partition table and the Magic bytes required for boot ):
[Plain] view plaincopyprint? 01.Dd if =/dev/sda of = MBR_boot.img bs = 446 count = 1 
Dd if =/dev/sda of = MBR_boot.img bs = 446 count = 1 data modification

Dd can modify data in the same place.

Overwrite the first 512 bytes of the file with null bytes:
[Plain] view plaincopyprint? 01.Dd if =/dev/zero of = path/to/file bs = 512 count = 1 conv = notrunc 

Dd if =/dev/zero of = path/to/file bs = 512 count = 1 conv = notrunc conversion option notrunc means that the output file is not reduced, that is, if the output file already exists, only change the specified byte, exit, and retain the remaining part of the output file. Without this option, dd creates a 512-byte file.

Copy the disk partition to the disk image file in different partitions:
[Plain] view plaincopyprint? 01.Dd if =/dev/sdb2 of = partition. image bs = 4096 conv = noerror 
Dd if =/dev/sdb2 of = partition. image bs = 4096 conv = noerror disk erasure

Primary Entry: Data erasure

For security considerations, you sometimes need to erase the discarded disk.

Check whether there is data on the drive and output it to standard output:

[Plain] view plaincopyprint? 01.Dd if =/dev/sda 
Dd if =/dev/sda:
[Plain] view plaincopyprint? 01.Dd if =/dev/zero of =/dev/sda bs = 4 k 
Dd if =/dev/zero of =/dev/sda bs = 4kdd the process processes a block at a time. its algorithm only displays the running status on the user interface.

Drive performance benchmark test

Benchmark the drive (usually a single thread), using 1024 bytes to analyze the performance of reading and writing data to the continuous system:

[Plain] view plaincopyprint? 01.Dd if =/dev/zero bs = 1024 count = 1000000 of = file_1GB
02. dd if = file_1GB of =/dev/null bs = 1024
 
Dd if =/dev/zero bs = 1024 count = 1000000 of = file_1GB

Dd if = file_1GB of =/dev/null bs = 1024 generate a file with random data

Use the Kernel random number driver to generate files with 100 random bytes:
[Plain] view plaincopyprint? 01.Dd if =/dev/urandom of = myrandom bs = 100 count = 1 
Dd if =/dev/urandom of = myrandom bs = 100 count = 1 convert the file to uppercase

Convert the file to uppercase:

[Plain] view plaincopyprint? 01.Dd if = filename of = filename1 conv = ucase 
Dd if = filename of = filename1 conv = ucase create an empty file of any size
Create a 1 GiB sparse file or increase the size of an existing file:
[Plain] view plaincopyprint? 01.Dd if =/dev/zero of = mytestfile. out bs = 1 count = 0 seek = 1G

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.