Linux beginners must learn file processing commands

The file processing commands that Linux entry must learn mainly include the following commands file, mkdir, grep, dd, find, mv.

Linux system information is stored in files, which are similar to ordinary official documents. Each file has its own name, content, storage address and other management information, such as the user of the file, the size of the file, and so on. The file can be a letter, an address book, or the source statement of the program, the data of the program, and can even include executable programs and other non-text content. The Linux file system has a good structure, and the system provides many file processing programs. Here mainly introduces commonly used file processing commands.

file

1. Function

file determines the file type by detecting the file content, and the usage permission is all users.

2. Format

file [options] file name

3. [options] main parameters

-v: Display version information after standard output, and exit.

-z: detect compressed file types.

-L: Allow conformant connections.

4. Brief description

Use the file command to know whether a file is a binary (ELF format) executable file, a Shell  Script file, or any other format. The file types recognized by file include directories, shell scripts , English text, binary executable files, C language source files, text files, and DOS executable files.

5. Application examples

If we see a file grap without a suffix, we can use the following command:

$file grap
grap:English text

At this time, the system shows that this is an English text file. It should be noted that the file command cannot detect multimedia file types including graphics, audio, and video.

mkdir

1. Function

The function of the mkdir command is to create a subdirectory named dirname, which is similar to the md command under MS DOS, and its usage authority is all users

2. Format

mkdir [options] directory name

3. [options] main parameters

-m,--mode=mode: Set permissions <mode>;, similar to chmod.

-p,--parents: Create the upper-level directory if necessary; if the directory already exists, it is not considered an error.

-v, --verbose: Display information every time a new directory is created.

--version: leave after displaying the version information.

4. Application examples

You can set the permissions of the directory when creating the directory, and the parameter used at this time is "-m". Assuming that the name of the directory to be created is "tsk", so that all users have rwx (that is, read, write, and execute permissions), then the following command can be used:

$mkdir -m 777 tsk

grep

1. Function

The grep command can specify to search for specific content in the file, and output the line containing these content to standard output. The full name of grep is Global Regular Expression Print, which means the global regular expression version, and its usage permission is for all users.

2. Format

grep  [options]

3. [options] main parameters

-c: Only output the count of matching lines.

-I: case insensitive (only for single characters).

-h: Do not display the file name when querying multiple files.

-l: When querying multiple files, only output the file names containing matching characters.

-n: Display matching lines and line numbers.

-s: Do not display error messages that do not exist or have no matching text.

-v: Display all lines that do not contain matching text.

The main parameters of pattern regular expression:

\: Ignore the original meaning of the special character in the regular expression.

^: Matches the start line of the regular expression.

$: Matches the end of the regular expression line.

\<: Start with the line matching the regular expression.

\>;: To the end of the line matching the regular expression.

[]: A single character, such as [A], that is, A meets the requirements.

[ - ]: Range, such as [AZ], that is, A, B, C and Z all meet the requirements.

*: There are characters, and the length can be 0.

Regular expressions are very important concepts in Linux/Unix systems. A regular expression (also known as "regex" or "regexp") is a pattern that can describe a class of strings. If a string can be described by a regular expression, we say that the character matches the regular expression (Match). This is similar to DOS where users can use the wildcard "*" to represent any character. On Linux systems, regular expressions are often used to find patterns in text, and to perform "search-replace" operations and other functions on text.

4. Application examples

Querying DNS services is one of the daily tasks, which means maintaining a large number of IP addresses covering different networks. Sometimes there are more than 2000 IP addresses. If you want to look up the nnn.nnn network address, but forget the rest of the second part, you only know that there are two periods, such as nnn nn... To extract all nnn.nnn IP addresses in it, use [0-9 ]\{3\}\.[0-0\{3\}\. The meaning is 3 occurrences of any number followed by a period, followed by 3 occurrences of any number followed by a period.

$grep '[0-9 ]\{3 \}\.[0-0\{3\}\' ipfile

As a supplement, the grep family also includes fgrep and egrep. fgrep is fix grep, which allows to search for strings instead of a pattern; egrep is an extended grep, which supports basic and extended regular expressions, but does not support the application of \q pattern range and some more standardized patterns corresponding to it.

dd

1. Function

The dd command is used to copy files, and convert and format the data according to parameters.

2. Format

dd  [options]

3. [options] main parameters

bs=bytes: force ibs=<bytes>; and obs=<bytes>;.

cbs=bytes: specify <bytes> for each conversion;.

conv=keywords: Convert files according to the comma-separated keywords.

count=block number: only copy the specified <block number>; input data.

ibs=byte: read the specified <byte> each time;.

if=file: read <file>; content instead of standard input data.

obs=byte: write the specified <byte> each time;.

of=file: write data to <file>; instead of displaying it on standard output.

seek=number of blocks: first skip the output data of the specified <number of blocks> in obs.

skip=number of blocks: first skip the specified <number of blocks> in ibs; the input data.

4. Application examples

The dd command is often used to make a Linux boot disk. First find a bootable kernel, make its root device point to the correct root partition, and then use the dd command to write it to the floppy disk:

$rdev vmlinuz /dev/hda 
$dd if=vmlinuz of=/dev/fd0

The above code shows that use the rdev command to point the root device in the bootable kernel vmlinuz to /dev/hda, please replace "hda" with your own root partition, and then use the dd command to write the kernel to the floppy disk.

find

1. Function

The function of the find command is to search for files in the directory, and its usage authority is all users.

2. Format

find [path][options][expression]

path specifies the directory path from which the system starts to search for files down the directory tree. It is a list of paths separated by spaces. If path is not written, the default is the current directory.

3. [options] main parameters

-depth: Use the depth-level search process method to search for file content first in the specified directory of a certain layer.

-maxdepth levels: Indicates that at most the first level subdirectory of the starting directory can be found. level is a non-negative number, if level is 0, it means only search in the current directory.

-mindepth levels: Indicates that at least the first level subdirectory of the start directory has been found.

-mount: Do not search in directories and files of other file systems (such as Msdos, Vfat, etc.).

-version: print version.

[expression] is a matching expression, which is an expression accepted by the find command, and all operations of the find command are for expressions. It has a lot of parameters, only some commonly used parameters are introduced here.

-name: wildcard characters * and ? are supported.

-atime n: Search for files that have been read in the past n days.

-ctime n: Search for files modified in the past n days.

-group grpoupname: Search for all files whose group is grpoupname.

-user username: Search for all files whose owner is the username (ID or name).

-size n: Search for files whose file size is n blocks.

-print: Output the search results and print them.

4. Application examples

There are several ways to find files with the find command:

(1) Search by file name

For example, if we want to find a file named lilo.conf, we can use the following command:

find / -name lilo.conf

The "/" after the find command means to search the entire hard disk.

(2) Quickly find files

Finding files by filename encounters a practical problem, that is, it takes quite a while, especially when large Linux file systems and large-capacity hard disk files are placed in deep subdirectories. If we know that this file is stored in a certain directory, we can save a lot of time just looking down in this directory. For example, the smb.conf file can be judged from its file suffix ".conf" that it is a configuration file, so it should be in the /etc directory. At this time, the following command can be used:

find /etc -name smb.conf

In this way, time can be shortened by using the "Quick Find Files" method.

(3) Search method based on part of the file name

Sometimes we know that only a certain file contains the 4 characters abvd, so to find all files containing these 4 characters in the system, you can enter the following command:

find / -name '*abvd*'

After entering this command, the Linux system will search for all files containing the 4 characters abvd in the / directory (where * is a wildcard), such as abvdrmyz and other qualified files can be displayed.

(4) Use the hybrid search method to find files

The find command can use a mixed search method. For example, if we want to find a file that is larger than 500,000 bytes in the /etc directory and has been modified within 24 hours, we can use -and (and) to link the two search parameters. combined into a hybrid lookup.

find  /etc  -size +500000c -and -mtime +1

mv

1. Function

The mv command is used to rename a file or directory, or move a file from one directory to another, and its usage authority is all users. This command is like the combination of ren and move in the DOS command.

2. Format

mv [options] source file or directory target file or directory

3. [options] main parameters

-i: Interactive operation. If the mv operation will lead to the overwriting of the existing target file, the system will ask whether to rewrite and ask the user to answer "y" or "n", so as to avoid overwriting the file by mistake.

-f: Disable interactive operation. When the mv operation wants to overwrite an existing target file, no instructions are given. After specifying this parameter, the i parameter will no longer work.

4. Application examples

(1) Move all files in /usr/cbu to the current directory (indicated by "."):

$mv /usr/cbu/ * .

(2) Rename the file cjh.txt to wjz.txt:

$mv cjh.txt wjz.txt

 

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/130718910