Linux Command chown, chgrp, cmp, dd, du

Source: Internet
Author: User
Tags disk usage

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
Only the file owner of a Super User and a group can change the file association group. Non-privileged users (non-Super Users) may need to set an association group
Use the chgrp command.
Usage

# chown root /var/run/httpd.pid
Set the owner of/var/run/httpd. pid to root.
# chown rob:developers strace.log
Set strace. log owner to rob and the file association group to developers.
# chown :512 /home
Set the/home association group to 512 (Association group ID) without changing the owner.
# chown -R us base
Set base and the owner of all files under 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
$ls -l testfile-rw-r--r--   1 unixguy  staff           545 Nov 04 2004  ttt$chgrp system testfile$ls -l ttt-rw-r--r--   1 unixguy  system          545 Nov 04 2004  ttt

chgrp staff /u
Change the/u group to "staff ".
 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 <Character Count>] [-- 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 <character quantity> or-ignore-initial = <character quantity> specifies 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:

prog.o.bak prog.o differ: char 4, line 1
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:
5 101 141

3.
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:
 $ du -sk * 152304  directoryOne 1856548 directoryTwo
More easily read the total directory size format (Byte, KB, MB, GB, TB, PB ):
 $ du -sh * 149M directoryOne 1.8G directoryTwo
The current directory contains hidden files, and the disk usage of subdirectories and files (sorted from small to large ):
 $ du -sk .[!.]* *| sort -n
The current directory contains hidden files, and the disk usage of subdirectories and files (sorted from large to small ):
 $ du -sk .[!.]* *| sort -nr
Displays the directory size and total size with a depth of 1.
 $ du -d 1 -c -h
Dd 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:
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 ):
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:
dd if=/dev/zero of=path/to/file bs=512 count=1 conv=notrunc
The conversion option notrunc means that the output file is not reduced. That is to say, if the output file already exists, only the specified bytes are changed, then the output file is exited, and the remaining part of the output file is retained. Without this option, dd creates a 512-byte file.
Copy the disk partition to the disk image file in different partitions:
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:
dd if=/dev/sda
Erase disk with zero:
dd if=/dev/zero of=/dev/sda bs=4k
The dd process processes a block at a time, and 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:
dd if=/dev/zero bs=1024 count=1000000 of=file_1GBdd 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:
dd if=/dev/urandom of=myrandom bs=100 count=1
Convert an object to uppercase

Convert the file to uppercase:

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:
dd if=/dev/zero of=mytestfile.out bs=1 count=0 seek=1G

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.