Linux system information is stored in a file similar to a common official file. Each file has its own name, content, storage address and other management information, such as the file user, file size and so on. A file can be a letter, an address book, or a program’s source statements, program data, or even executable programs and other non-text content. The Linux file system is well structured and provides many file handlers. This section describes common file processing commands.

file

1. The role

File Determines the file type by detecting the file content. The permission is granted to all users.

2. The format

File [options] File name

3.[options] Main parameters

-v: displays the version information after the standard output and exits.

-z: detects compressed file types.

-l: allows conformance connections.

-f name: reads the list of file names to be analyzed from file namefile.

4. Brief instructions

The file command tells you whether a file is a binary (ELF format) executable, a Shell Script file, or some other format. File The file types that can be identified 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

The system displays an English text file. Note that the file command cannot detect multimedia file types, including graphics, audio, and video files.

mkdir

1. The role

The mkdir command is used to create a subdirectory named dirname. Similar to the MD command in MS DOS, it is available to all users.

2. The format

Mkdir [options] Directory name

3.[options] Main parameters

-m, –mode= mode: set permission < mode >; , similar to chmod.

-p, –parents: create upper directories if needed; If the directory already exists, it is not considered an error.

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

–version: Displays the version information and exits.

4. Application examples

When creating a directory, you can set the permission of the directory by using -m. If you want to create a directory named “TSK” and give all users RWX (read, write, execute) permissions, you can use the following command:

$mkdir -m 777 TSK

grep

1. The role

The grep command can specify specific things to search for in a file and standard output lines containing those things. Grep is the Global Regular Expression Print version. All users are allowed to use it.

2. The format

grep [options]

3. Main parameters

[options] Main parameters:

-c: displays the count of matched rows.

-I: case insensitive (applicable only to single characters).

-h: The file names are not displayed when multiple files are queried.

-l: Only the file names containing matching characters are displayed when multiple files are queried.

-n: Displays the matching line and its number.

-s: does not display the error message that does not exist or has no matching text.

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

Main parameters of pattern regular expression:

: Ignores the original meaning of special characters in the regular expression.

^ : Matches the beginning line of the regular expression.

$: matches the end line of the regular expression.

< : Starts at the line that matches the regular expression.

>; : Ends at the line matching the regular expression.

[] : indicates A single character, such as [A].

[-] : range, for example, [A-Z], that is, A, B, C, and Z meet the requirements.

. : All single characters.

* : contains 0 characters.

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

4. Application examples

Querying DNS services is part of the daily routine, 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 at the NNN. NNN network address, but forget the rest of the second part, only know that there are two periods, such as NNN nn.. . NNN. NNN IP addresses are extracted using [0-9]{3}.[0-0{3}.

$grep ‘[0-9]{3}.[0-0{3}’ ipfile

Note that the grep family also includes fgrep and egrep. Fgrep is a fix grep that allows you to look up strings rather than a pattern; Egrep is an extended grep. It supports basic and extended regular expressions, but does not support the application of the range of Q mode and some corresponding more standard modes.

dd

1. The role

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

2. The format

dd [options]

3.[Opitions] Main parameters

Bs = bytes: forces ibs=< bytes >; And obs=< bytes >; .

CBS = bytes: < bytes > specified for each conversion; .

Conv = keyword: Converts files according to the way they are represented by comma-separated keywords.

Count = number of blocks: copy only specified < number of blocks >; Input data of.

Ibs = bytes: read the specified < bytes > each time; .

If = file: read < file >; Content, rather than standard input data.

Obs = bytes: write specified < bytes > each time; .

Of = file: write data to < file >; Is not displayed in standard output.

Seek = number of blocks: skip the specified < number of blocks > in obs; Output data of.

Skip = number of blocks: first skip < number of blocks > specified in unit of IBS; Input data of.

4. Application examples

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

$ rdev vmlinuz /dev/hda

$dd if = vmlinuz of = /dev/fd0

Use the rdev command to point the root device in the bootable kernel vmlinuz to /dev/hda. Replace “hda” with your own root partition. Then use the dd command to write the kernel to the floppy disk.

find

1. The role

The find command searches directories for files and is available to all users.

2. The format

find [path][options][expression]

Path Specifies the directory path from which the system searches for files down the directory tree. It is a list of paths, separated by Spaces, that default to the current directory if path is not written.

3. Main parameters

[options] parameters:

-depth: searches for file contents in a specified directory at a certain level.

-maxdepth levels: indicates that at most subdirectories at level 1 of the start directory are searched. Level is a non-negative number. If level is 0, the search is performed only in the current directory.

-mindepth levels: indicates that at least the level-1 subdirectory of the start directory is found.

-mount: does not search for files or directories in other file systems, such as Msdos and Vfat.

-version: indicates the printed version.

[expression] is a matching expression. It is an expression accepted by the find command. All operations of the find command are performed on expressions. It has a lot of parameters. Here are just some common ones.

– Name: Supports asterisks and? .

-atime n: searches for files read in the last n days.

-ctime n: searches for files that have been modified in the last n days.

-group grpoupname: searches for all files whose group is Grpoupname.

-user User name: searches for all files whose owner is a user name (ID or name).

-size n: Searches for files with n blocks in size.

-print: prints the search result.

4. Application skills

The find command can find a file in several ways:

(1) Search according to the file name

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

The find / -name lilo. Conf

The slash following the find command indicates the search for the entire hard disk.

(2) Find files quickly

One practical problem with finding files by filename is that it can take quite a while, especially if large Linux file systems and large-capacity hard disk files are in deep subdirectories. If we know that the file is in a directory, we can save a lot of time just looking down in that directory. Conf file is a configuration file in the /etc directory. In this case, you can run the following command:

The find/etc -name SMB. Conf

In this way, using the “quick find files” method can shorten the time.

(3) Search methods according to partial file names

Sometimes we know that only one file contains abvd, so to find all files in the system that contain abvd, we can type the following command:

The find / – the name ‘* abvd *’

After entering this command, Linux will search the/directory for all files containing the four characters abvd (* is the wildcard character), such as abvdrmyz, and display them.

(4) Use mixed search to find files

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

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

mv

1. The role

The mv command is used to change the name of a file or directory or move a file from one directory to another. All users are allowed to use the mv command. This command is like the combination of ren and move in the DOS command.

2. The format

Mv [options] Source file or directory Target file or directory

3.[options] Main parameters

-i: indicates an interactive operation. If the MV operation will overwrite the existing target file, the system asks the user whether to rewrite the file and asks the user to answer Y or N to avoid overwriting the file by mistake.

-f: Disables interactive operations. The mv operation overwrites an existing object file without giving any indication, and the I parameter is no longer in effect after specifying this parameter.

4. Application examples

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

$ mv /usr/cbu/ * .

(2) rename cjh.txt to wjz.txt:

$ mv cjh.txt wjz.txt

ls

1. The role

The ls command is used to display the contents of a directory. Similar to the dir command in DOS, the permission of the ls command is restricted to all users.

2. The format

ls [options][filename]

3. Options Main parameters

-a, –all: does not hide anything with “. The character starts the item.

-a, –almost-all: lists all but “. “and”..” Any other items.

— Author: Print the author of each file.

-b, –escape: Indicates unprintable characters as octal escape sequences.

–block-size= size: block to specify < size >; Is expressed in bytes.

-b, –ignore-backups: does not list any items ended with ~ characters.

-f: does not sort. -au takes effect and -lst takes effect.

– F – the classify, plus the symbol file type (* / = @ | one).

-g: like -l, but do not list owner.

-g, –no-group: Inhibit display of group information.

-i, –inode: lists the inode number of each file.

-i, –ignore= style: do not print any matching Shell multicharacter < style >; The project.

-k: –block-size=1K.

-l: Lists information in a longer format.

-l, –dereference: When displaying symbolic link file information, displays the object indicated by the symbolic link, not the symbolic link itself.

-m: All items are separated by commas and filled with the entire line width.

-n, –numeric- uID-gid: similar to -l, but lists the UID and GID number.

-n, –literal: Lists unprocessed project names, such as control characters that are not specifically processed.

– p – file – type: plus the symbol file types (/ = @ | one).

-q, –quote-name: puts the project name in double quotation marks.

-r, –reverse: in reverse order.

-r, –recursive: List all subdirectory layers simultaneously.

-s, –size: Indicates the block size in sequence.

4. Application Examples

The ls command is the most frequently used command in Linux and has the most parameters among Linux commands. The ls command has several different colors: blue for directories, green for executable files, red for compressed files, light blue for linked files, bold black for symbolic links, and gray for files in other formats. The most common use of LS is lS-L, as shown in Figure 1.

Figure 1 Using ls-l command

A file type begins with a string of 10 characters. The first character indicates the file type, which can be one of the following types: – (common file), D (directory), L (symbolic link), B (block device file), or C (character device file). The following nine characters indicate the access permission of the file, which is divided into three groups of three characters. The first group represents the permissions of the file owner, the second group represents the permissions of the same group of users, and the third group represents the permissions of other users. The three characters in each group represent read (r), write (W), and execute (x) permissions on the file. For directories, it indicates access permission. S indicates that when a file is executed, the UID or GID of the file is assigned to the UID (user ID) or GID (group ID) of the executing process. T indicates setting the flag bit (left in memory, not swapped out). If the file is a directory, files in the directory can only be deleted by the superuser, directory owner, or file owner. If it is an executable, a pointer to its body remains in memory after the file is executed. This will enable the system to load the file more quickly when it is executed again. The file size, generation time, and file or command name are displayed next.

diff

1. The role

The diff command is used to compare two files and point out the differences between them. It is available to all users.

2. The format

Diff [options] Source file Destination file

3.[options] Main parameters

-a: All files are processed as text files.

-b: Ignores the difference caused by Spaces.

-b: Ignores the difference caused by blank lines.

-c: Uses the outline output format.

-h: Uses heuristics to speed up the search for large files.

-i: ignores the case change.

-n — RCS: indicates the output RCS format.

cmp

1. The role

The CMP (short for “compare”) command is used to briefly indicate whether two files are different and is available to all users.

2. The format

CMP [options] file name

3.[options] Main parameters

-l: Outputs bytes in decimal form, and facilitates the output of octal numbers in two files.

cat

1. The role

The cat (short for “concatenate”) command is used to connect and display information about one or more specified files for all users.

2. The format

Cat [options] File 1 File 2……

3.[options] Main parameters

-n: indicates the number of all output lines starting from the first line.

-b: Similar to -n, except that blank lines are not numbered.

-s: Replaces a blank line with a blank line if there are more than two consecutive blank lines.

4. Application Examples

(1) One of the simplest uses of the cat command is to display the contents of text files. For example, if we want to view the contents of the README file from the command line, we can use the command:

$ cat README

(2) Sometimes it is necessary to process several files into one file and save the results of this processing to a separate output file. The cat command takes one or more files on its input and prints them as a single file to its output. For example, after appending the README and INSTALL file contents with line numbers (blank lines are not added), append the contents to a new text file File1:

$ cat README INSTALL File1

(3) Another important function of CAT is to number lines, as shown in Figure 2. This feature is handy for programming documentation, as well as for legal and scientific documents. The line numbers printed on the left make it easy to refer to certain parts of the document, which are important in programming, scientific research, business reports, and even legislation.

Figure 2. Use the cat command to number the /etc/named.conf file

The line numbering function has two parameters -b (only non-blank lines can be numbered) and -n (all lines can be numbered) :

$ cat -b /etc/named.conf

ln

1. The role

Using the ln command, you can create links between files. The ln command is available to all users.

2. The format

Ln [options] source file [link name]

3. The parameters

-f: deletes the source file before linking.

-d: allows the system administrator to hardlink their own directories.

-s: carries out soft Link (Symbolic Link).

-b: Backs up files that will be overwritten or deleted during linking.

There are two types of links, one is called Hard Link and the other is called Symbolic Link. By default, the ln command generates a hard link.

A hard connection is a connection made through an index node. In Linux file systems, files stored in disk partitions are assigned a number, called the Inode Index, regardless of their type. In Linux, it is possible to have multiple file names pointing to the same index node. In general, this connection is a hard connection. The purpose of hard links is to allow a file to have multiple valid pathnames, so that users can establish hard links to important files to prevent “accidental deletion” function. The reason for this is as described above, because there is more than one connection to the index node that should be the directory. Deleting only one connection does not affect the index node itself or other connections, and only when the last connection is deleted will the file’s data blocks and directory connections be released. In other words, the file is actually deleted.

In contrast to the hard connection, there is another kind of connection in the Lnux system called Symbilc Link, also known as soft connection. Soft link files are a bit like Windows shortcuts. It is actually a special file. In symbolic links, a file is actually a text file that contains information about the location of another file.

To contact

Above we introduced the Linux file processing commands. Here are a few examples that you can practice using.

1. Use symbolic links to quickly access key directories

Symbolic links are a very useful feature. Suppose you have some directory or file that you need to use frequently, but that file or directory is in a deep subdirectory because of Linux’s file and directory structure. For example, Apache Web server documents are in /usr/local/httpd/htdocs on your system, and you don’t want to have to go from your home directory to such a long path every time (in fact, this path is also very difficult to remember).

To solve this problem, you can create a symbolic link in your home directory so that when you need to go to that directory, you only need to go to that link.

To easily access the directory where the Web server (/usr/local/ HTTPD /htdocs) document resides, use the following command in the home directory:

$ ln -s /usr/local/httpd/htdocs gg

In this way, you can access documents on the Web server every time you enter the GG directory. If you do not need to access documents on the Web server in the future, you can delete the GG directory, but the documents on the real Web server are not deleted.

2. Run the dd command to import the root.ram content in init.rd format into the memory

dd if=/dev/fd0 of=floppy.fd

dd if=root.ram of=/dev/ram0 #

3. The grep command is invoked

Grep is one of the most widely used commands in Linux/Unix and can be called internally by many Linux systems.

(1) To query the directory in the directory list, do as follows:

$ls -l | grep ‘Sunday afternoon d’

(2) To query all files in a directory that do not contain a directory, do as follows:

$ls -l | grep ‘Sunday afternoon [Sunday afternoon d]’

(3) Call grep with find command, such as “Chinput” in all C source code, as follows:

$find /ZhXwin -name *.c -exec grep -q -s Chinput {} ; -print