Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Several commonly used commands: df, dd, du, locate, find

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Df command:

Df-report file system disk space usage

Df [OPTION]... [FILE]...

-h,-- human-readble: unit conversion

-l,-- local: displays only the local file system, not the network file system

-I,-- inodes: displays inode usage status

-T,-- print-type: displays the file system type

-P,-- portability: use the POSIX output format, making it easier to read

Use this command to display disk usage information on the current system, using the default unit KB

[root@localhost ~] # dfFilesystem 1K-blocks Used Available Use% Mounted on/dev/mapper/vg_localhost-lv_root 51475068 6523056 42330572 14% / tmpfs 1857540 72 1857468% / dev/shm/dev/sda1 487652 43139 418913% / boot/dev/mapper/vg_localhost-lv_home 424688000 93992 403014440% / home [root@localhost ~] #

Because the unit displayed only with the df command is KB, which is not easy for us to view, we can add the "- h" option to change the displayed unit to an easy-to-read unit:

[root@localhost ~] # df-hFilesystem Size Used Avail Use% Mounted on/dev/mapper/vg_localhost-lv_root 50G 6.3G 41G 14% / tmpfs 1.8G 72K 1.8G 1% / dev/shm/dev/sda1 477M 43M 410M 10% / boot/dev/mapper/vg_localhost-lv_home 406G 92m 385G 1% / home [root@localhost] #

In the course of daily use, there are times when you need to know the file system of the partition, you can use the "- T" option:

[root@localhost] # df-ThFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/vg_localhost-lv_root ext4 50G 6.3G 41G 14% / tmpfs tmpfs 1.8G 72K 1.8G 1% / dev/shm/dev/sda1 ext4 477M 43M 410m 10% / boot/dev/mapper/vg_localhost-lv_home ext4 406G 92m 385G 1% / home [root@localhost ~] #

If you only want to view partitions of file system type ext4, you can use the "- t" or "--type=ext4" options:

[root@localhost] # df-t ext4Filesystem 1K-blocks Used Available Use% Mounted on/dev/mapper/vg_localhost-lv_root 51475068 6523088 42330540 14% / / dev/sda1 487652 43139 418913 10% / boot/dev/mapper/vg_localhost-lv_home 424688000 93992 403014440% / home [root@localhost ~] #

All of the above options display blocks, or you can use this command to show inode usage by adding the "- I" option:

[root@localhost] # df-iFilesystem Inodes IUsed IFree IUse% Mounted on/dev/mapper/vg_localhost-lv_root 3276800 119567 3157233 4% / tmpfs 464385 3 464382 1% / dev/shm/dev/sda1 128016 39 127977 1% / boot/dev/mapper/vg_localhost-lv_home 26976256 534 26975722 / home [root@localhost ~] #

In the above example, we can find that the format is not exactly in accordance with the table, so it is a bit messy, so you can use the "- P" option:

[root@localhost] # df-hTPFilesystem Type Size Used Avail Use% Mounted on/dev/mapper/vg_localhost-lv_root ext4 50G 6.3G 41G 14% / tmpfs tmpfs 1.8G 72K 1.8G 1% / dev/shm/dev/sda1 ext4 477M 43M 410m 10% / boot/dev/mapper/vg_localhost- Lv_home ext4 406G 92m 385G 1% / home [root@localhost ~] #

Dd command: convert and copy a file

I think the biggest help of this command is that it allows me to create a file of any size at will.

Dd [OPERAND]...

Dd OPTION

Common OPERAND:

Dd if=/PATH/FROM/SRC of=/PATH/TO/DEST

Bs=BYTES:block size, copy unit size

Count=N: how many bs are copied

Of=FILE: write to a named file instead of to standard output

If=FILE: read from named files instead of standard input

Ibs=BYTES: read size byte at a time

Obs=BYTES: write size byte at a time

Skip=BLOCKS: ignore blocks blocks of ibs size from the beginning

Seek=BLOCKS: ignore blocks blocks of obs size from the beginning

Conv=conversion [, conversion...]: converts the file with the specified parameters.

Conversion parameters:

Ascii: convert EBCDIC to ASCII.

Ebcdic: convert ASCII to EBCDIC.

Block: converts each line to a record of length cbs, with blanks to fill in the deficiency.

Unblock: make the length of each line cbs, and fill the gaps with blanks.

Lcase: converts uppercase characters to lowercase characters.

Ucase: converts lowercase characters to uppercase characters.

Swab: swap each pair of bytes entered.

Noerror: continue reading when there is a read error.

Notrunc: output files are not truncated.

Sync: fill each input block into ibs bytes, and fill in the insufficient parts with NULL characters

Some common methods are as follows:

Disk copy:

~] # dd if=/dev/sda of=/dev/sdb

Backup MBR:

~] # dd if=/dev/sda of=/tmp/mbr.bak bs=512 count=1

Destroy the bootloader in MBR:

~] # dd if=/dev/zero of=/dev/sda bs=64 count=1 seek=446

There is a binary file fileA,size > 2K. Now to start reading from the 64th byte position, the size you need to read is 128Byts. There is also fileB. I want to write the 128Bytes read above to the beginning of the 32nd byte and replace 128Bytes. How can I achieve it?

~] # dd if=fileA of=fileB bs=1 count=128 skip=63 seek=31 conv=notrunc

Backup:

Back up the local / dev/sdx entire disk to / dev/sdy

~] # dd if=/dev/sdx of=/dev/sdy

Back up the whole / dev/sdx data to the p_w_picpath file of the specified path

~] # dd if=/dev/sdx of=/path/to/p_w_picpath

Backup / dev/sdx full data, and use gzip tool to compress and save to the specified path

~] # dd if=/dev/sdx | gzip > / path/to/p_w_picpath.gz

Restore:

Restore backup files to the specified disk

~] # dd if=/path/to/p_w_picpath of=/dev/sdx

Restore the compressed backup file to the specified disk

~] # gzip-dc / path/to/p_w_picpath.gz | dd of=/dev/sdx

Copy memory data to hard disk

Copy the data in memory to the mem.bin file in the root directory

~] # dd if=/dev/mem of=/root/mem.bin bs=1024

Copy an iso image from a CD

Copy the CD data to the root folder and save it as a cd.iso file

~] # dd if=/dev/cdrom of=/root/cd.iso

Destroy disk data

The hard disk is filled with random data, which can be used to destroy data in some necessary situations. "after this operation, / dev/sda1 cannot be mounted, and the create and copy operations cannot be performed."

~] # dd if=/dev/urandom of=/dev/sda1

Get the most appropriate block size

By comparing the command execution time shown in the dd instruction output, the optimal block size size of the system can be determined.

~] # dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file~] # dd if=/dev/zero bs=2048 count=500000 of=/root/1Gb.file~] # dd if=/dev/zero bs=4096 count=250000 of=/root/1Gb.file~] # dd if=/dev/zero bs=8192 count=125000 of=/root/1Gb.file

Test the read and write speed of the hard disk

The read / write speed of the test hard disk can be calculated from the execution time of the output of the last two commands.

~] # dd if=/root/1Gb.file bs=64k | dd of=/dev/null~] # dd if=/dev/zero of=/root/1Gb.file bs=1024 count=1000000

Repair hard disk

When the hard disk is not in use for a long time (for example, 1 or 2 years), degaussing points will occur on the disk. It is difficult for the head to read these areas and may result in an Icano error. When this situation affects the first sector of the hard disk, it may cause the hard disk to be scrapped. The above command has the potential to bring the data back to life. And the process is safe and efficient.

~] # dd if=/dev/sda of=/dev/sda

The du command, which is similar to df, is also used to view usage space, but this command is to view files and directories, and the df command is to view disk:

Du-estimate file space usage

Du [OPTION]... [FILE]...

-s, sumary,: displays the file size of the entire directory summary

-h, human-readble: unit conversion

-an or-all displays the size of individual files in the directory.

-b or-bytes displays the directory or file size in byte.

-c or-- total displays not only the size of individual directories or files, but also the sum of all directories or files.

-k or-- kilobytes output in KB (1024bytes). -m or-- megabytes output in MB.

-s or-- summarize displays only the total and lists only the last plus value. -h or-- human-readable is in the unit of KMagazine GetWord ("G"); to improve the readability of information.

-x or-- one-file-xystem is based on the file system at the beginning of the processing, and will be skipped in case of other different file system directories.

The source file size of the symbolic link specified in-L or-- dereference display options.

When-S or-- separate-dirs displays the size of an individual directory, it does not include the size of its subdirectories.

-X or-- exclude-from= is in the specified directory or file.

-- exclude= skips the specified directory or file.

-D or-- dereference-args displays the source file size of the specified symbolic link.

The parameters of-H or-- si are the same as-h, but the conversion unit of K _ ~ ~ M ~ G is 1000.

-l or-- count-links repeatedly calculates hardware-linked files.

Use this command to count the file size, such as the size of the root directory "/" (if there is no "- s" option, all directories under the root directory will be counted):

[root@localhost ~] # du-s / du: cannot access "/ proc/3895/task/3895/fd/4": without that file or directory du: cannot access "/ proc/3895/task/3895/fdinfo/4": without that file or directory du: cannot access "/ proc/3895/fd/4": does not have that file or directory du: cannot access "/ proc/3895/fdinfo/4" There is no such file or directory 6533337 / [root@localhost ~] #

The string of numbers shown on the last line is the size of the root directory, but this is not convenient for us to observe, so we can add the "- h" option:

[root@localhost ~] # du-sh / du: cannot access "/ proc/3884/task/3884/fd/4": without that file or directory du: cannot access "/ proc/3884/task/3884/fdinfo/4": without that file or directory du: cannot access "/ proc/3884/fd/4": does not have that file or directory du: cannot access "/ proc/3884/fdinfo/4" There is no such file or directory 6.3G/ [root@localhost ~] #

Locate command: when using Linux, sometimes you can't remember where a file is put, so you can use the find operation to find the file you want. There are many search commands, the first to talk about the search command, this command is fuzzy search, can also be said to be a search in greedy mode, will find all the matching content, such as want to find the "a" this file, but after using this command, all files containing the "a" letter will be displayed. This command is based on a dedicated database, which should be created in advance and updated regularly, and we can update the locate database manually using the updatedb command. Because you don't have to traverse the directory, you only need to find the database, so the search speed is very fast, but the search accuracy is very limited, because if you create a new file, but it is not updated in the database, then you will not be able to find this file. Only when you update the database, you can find it. The method used is to keep up with the name of the file you want to find after the command: locate fileName. So if you want to use this command, you need to update the locate database regularly, and you can use the updatedb command to update the locate database manually.

Find command: as opposed to the locate command, this command is a precise search, which scans the file name or file attributes at the specified location, so it is strongly not recommended to find the root directory, because the search time will be very long, and this command is a real-time search, so you will face a problem. If another user logs in when the search operation is performed, the file will be deleted. Then there will be an error, so when you use this command, you will find it offline. When using the find command, you can only search for directories where the current user has read and execute permissions, just as an ordinary user cannot access system configuration files. The find command is used as follows:

Find command:

Find-search for files in a directory hierarchy

Find [OPTIONS...] [search path] [search condition] [processing action]

Search path: the default is the current working directory, and you can specify a specific directory path

Search criteria: the criteria for this search can be file name, file size, file type, file permissions, etc.; the default is all files in the specified directory

Processing action: perform a processing operation on files that meet the criteria; output the search results to the monitor by default

Look up by file name:

-name file name. Globbing is supported. (*,?, [], [^])

-iname file name, ignore the case of letters, support the use of Globbing, (*,?, [], [^])

Look up according to the inode number of the file:

-inum inode number: find the corresponding file name and path through the given inode number

-samefile name: find the corresponding inode number through the given file name, and then determine all the file names and paths with the inode number

-links n: find all files with n links

Look up according to the regular expression:

-regex pattern: matches the entire file path string with pattern, not just the name of the given file

Search according to the owner and group of the file:

-user uname: search based on the user name of the specified user by the owner

-uid UID: search for a UID based on the owner

-group gname:

-gid GID:

-nogroup: there is no group name corresponding to the group in the file.

-nouser: there is no corresponding user name on the owner of the file

Look up according to the type of file:

-type file type:

B: block equipment

C: character device

D: catalog file

F: ordinary files

L: symbolic link file

P: pipe fil

S: socket file

-xtype file type: matching of symbolic link files needs to be matched with other options

Look up according to the timestamp:

In days:

-atime [+ | -] n: search based on access time

-ctime [+ | -] n: search according to the change time

-mtime [+ | -] n: search based on modification time

N: [NJ Numer1)

+ n: [nasty 1pm + ∞)

-n: [now,n)

In minutes:

-amin [+ | -] n

-cmin [+ | -] n

-mmin [+ | -] n

Example:

5-28-11-18

-mtime-3

5-25-11-18

-mtime 3

5-24-11-18

-mtime + 3

Look up according to the size of the file:

-size [+ | -] n [cwbkMG]

N: (n Mei 1Jing n]

-n: [05m nmuri 1]

+ n: (NMagna + ∞)

Example:

Find-size + 2k

All files greater than 2KB in the current directory

Find-size 2k

Files between all 1KB-2KB in the current directory

Find-size-2k

All files smaller than 1KB in the current directory

Combination conditions:

-a: logic and, by default, can be omitted

-o: logical or

-not,!: logic not

The logical combination conditions follow de Morgan's law:

Not (An and B) = not An or B

Non (An or B) = non-An and non-B

Find based on permissions:

-perm [/ | -] mode

Mode: exactly matches the specified permissions

/ mode: the logical OR relationship is implied. The condition can be satisfied as long as there is a permission match in any permission bit.

-mode: implies the relationship between logic and. The permission of each permission bit must also contain the specified permission bit in order to meet the condition.

All of them are against any one of them.

! (an and b and c) =! an or! B or! C

None of them are reversed. Any one of them has.

! (! an and! B and! C) = an or b or c

Processing actions:

-print: output to the display screen, default action

-ls: execute the ls-li command to display the results found

-exec COMMAND {}\;:

-ok COMMAND {}\;:

Execute the COMMAND command for the found results

Difference:

-exec is non-interactive

-ok is interactive

{}: placeholder to refer to the path information of all files found by the find command

Instead of performing operations,-exec and-ok:

Chmod Amurr $(find-perm-444-type f)

Find-perm-444-type f | xargs chmod Amurr

Note: the pipeline carries pure string information, so if the command after the pipe is not a command for processing strings, you need to use the xargs command to convert it into parameters that can be processed by subsequent commands.

Here are some examples from: http://man.linuxde.net/find:

Match based on file or regular expression

Lists all files and folders in the current directory and subdirectories

Find.

Look for file names ending in .txt in the / home directory

Find / home-name "* .txt"

Same as above, but ignore case

Find / home-iname "* .txt"

Find all files ending in .txt and .pdf in the current directory and subdirectories

Find. \ (- name "* .txt"-o-name "* .pdf"\)

Or

Find. -name "* .txt"-o-name "* .pdf"

Match file path or file

Find / usr/-path "* local*"

Matching file paths based on regular expressions

Find. -regex ". *\ (\ .txt\ |\ .pdf\) $"

Same as above, but ignore case

Find. -iregex ". *\ (\ .txt\ |\ .pdf\) $"

Negative parameter

Find files under / home that do not end in .txt

Find / home!-name "* .txt"

Search by file type

Find. -type

Type parameter

Type parameter list:

F ordinary file

L symbolic connection

D directory

C character device

B block equipment

S socket

P Fifo

Search based on directory depth

The maximum downward depth is limited to 3

Find. -maxdepth 3-type f

Search for all files with a depth of at least 2 subdirectories from the current directory

Find. -mindepth 2-type f

Search based on file timestamp

Find. -type f

Time stamp

The UNIX/Linux file system has three timestamps for each file:

Access time (- atime/ days,-amin/ minutes): the last time the user visited.

Modification time (- mtime/ days,-mmin/ minutes): the last time the file was modified.

Change time (- ctime/ days,-cmin/ minutes): the last time the file data element (such as permissions, etc.) was modified.

Search for all files accessed in the last seven days

Find. -type f-atime-7

Search for all files that were accessed exactly seven days ago

Find. -type f-atime 7

Search all files that have been accessed in more than seven days

Find. -type f-atime + 7

Search all files that have been accessed for more than 10 minutes

Find. -type f-amin + 10

Find all the files that take longer to modify than file.log

Find. -type f-newer file.log

Match based on file size

Find. -type f-size

File size unit:

B-block (512 bytes)

C-byte

W-word (2 bytes)

K-kilobytes

M-megabytes

G-gigabyte

Search for files greater than 10KB

Find. -type f-size + 10k

Search for files less than 10KB

Find. -type f-size-10k

Search for files equal to 10KB

Find. -type f-size 10k

Delete matching file

Delete all .txt files in the current directory

Find. -type f-name "* .txt"-delete

Match based on file permissions / ownership

Search for files with permissions of 777 in the current directory

Find. -type f-perm 777

Find out the php files in the current directory whose permissions are not 644.

Find. -type f-name "* .php"!-perm 644

Find out all the files owned by the current directory user tom

Find. -type f-user tom

Find out all the files owned by the current directory user group sunk

Find. -type f-group sunk

Use the-exec option in conjunction with other commands

Find all the root files in the current directory and change the ownership to the user tom

Find.-type f-user root-exec chown tom {}\

In the above example, {} is used in conjunction with the-exec option to match all files, which is then replaced with the appropriate file name.

Find all the .txt files in your home directory and delete them

Find $HOME/. -name "* .txt"-ok rm {}\

In the above example, the-ok behavior is the same as the-exec behavior, but it gives a hint as to whether to perform the appropriate action.

Find all .txt files in the current directory and splice them together and write them to the all.txt file

Find. -type f-name "* .txt"-exec cat {}\; > all.txt

Move the .log file from 30 days ago to the old directory

Find. -type f-mtime + 30-name "* .log"-exec cp {} old\

Find all the .txt files in the current directory and print them in the form of "File: file name"

Find. -type f-name "* .txt"-exec printf "File:% s\ n" {}\

Because multiple commands cannot be used in the-exec parameter of a single-line command, the following method allows you to accept multiple commands after-exec

-exec. / text.sh {}\

Search but jump out of the specified directory

Find all .txt files in the current directory or subdirectory, but skip the subdirectory sk

Find. -path ". / sk"-prune-o-name "* .txt"-print

Find collection of other techniques

To list all files with zero length

Find. -empty

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report