Linux Basic Commands and Linux File Types

Linux file type

1. Ordinary file is the most commonly used type of file, which is characterized by not containing the structural information of the file system. Usually, files that users come into contact with, such as graphics files, data files, document files, sound files, etc., all belong to this kind of files. This type of file can be subdivided into text files and binary files according to its internal structure.

2. Directory file A directory file is a file used to store file names and related information. It is the basic node where the kernel organizes the file system. Catalog files can contain sub-directory files or ordinary files. In Linux, a directory file is a type of file. However, the concept of "directory" in Linux is different from that of "directory" in other operating systems. It is a kind of Linux file.

3. Linked file A linked file is a special file that actually points to a real file link, similar to a shortcut in Windows. According to different link files, it can be subdivided into hard link (Hard Link) files and symbolic link (SymbolicLink, also known as soft link) files.

4. Device files Device files are the most special files in Linux. It is precisely because of its existence that the Linux system can easily access external devices. The Linux system provides a standard interface for external devices, and treats external devices as a special file, allowing users to access any external device like ordinary files. Usually, the Linux system puts the device file in the "/dev" directory, and the device file uses the major device number and minor device number of the device to specify an external device. According to the different ways of accessing data, device files can be divided into block devices and character device files.

5. Pipeline file Pipeline file is a very special file, mainly used for information transfer between different processes. Pipeline files can be used when data or information needs to be transferred between two processes. One process writes the data or information to be transmitted into one end of the pipeline, and the other process obtains the required data or information from the other end of the pipeline. Usually the pipeline is built into the tuning cache.

6. Socket (s, socket): It is used for network communication between processes, and can also be used for non-network communication between local machines.

directory switching command

cd usr : switch to the usr directory under this directory.

cd .. : Switch to the previous directory.

cd / : Switch to the system root directory.

cd ~ : Change to the user's home directory.

cd - : Switch to the directory where the previous operation was located.

Directory operation commands (add, delete, check, modify)

1.mkdir directory name: add directory.

2. ls/ll: (ll is an alias of ls-l, the ll command can see the detailed information of all directories and files in this directory, ls only has directories): View directory information.

3. find path -option [-print] [-exec -ok command {}] : The search condition provided by the find command can be a compound condition composed of logical operators not, and, or.

The logical operators and, or, and not have the following meanings:

(1) and: Logical AND, represented by "-a" in the command, is the default option of the system, which means that the search condition is met only when the given conditions are met.

(2) or: logical or, represented by "-o" in the command. This operator means that as long as one of the given conditions is satisfied, the search condition is satisfied.

(3) not: Logical not, represented by "!" in the command. This operator means to find files that do not meet the given criteria.

The search method of the find command is mainly to search by name and file attributes, and the parameters are as follows:

(1) -name 'string': Find all files whose filename matches the given string, wildcards *, ? can be used in the string , [].

(2) -lname 'string': Find all symbolic link files whose file name matches the given string, wildcards *, ? can be used in the string , [].

(3) -gid n: Find all files belonging to the user group with ID number n.

(4) -uid n: Find all files belonging to the user whose ID number is n.

(5) -group string: Find all files belonging to the user group named the given string.

(6) -user string: Find all files belonging to the string given by the user name.

(7) -empty: Find directories or files with a size of 0.

(8) -path string: Find all files whose path name matches the given string, and wildcards *, ? can be used in the string , [].

(9) -perm permission: Find files and directories with specified permissions. Permissions can be represented as 711 (indicating that the owner of the file directory has read, write and execute permissions, and users in the same group and other users of the system only have execute permissions), 644 ( The owner of the file/directory has read and write permissions, and users in the same group and other users of the system have read permissions), etc. For details on how to set the digital permission form, readers please refer to the introduction to file/directory access permission management later in this chapter.

(10)-size n[bckw]: Find the file with the specified file size, the character after n represents the unit, the default is b, representing a 512-byte block.

(11) -type c: The file type is c file.

d: directory

c: font device file

b: block device file

p: named column

f: general file

l: symbolic link

s: socket

(12) -pid n : the file whose process id is n

(13)-amin n : has been read in the past n minutes

(14) -anewer file: A file that has been read later than the file file

(15) -atime n : Files read in the past n days

(16) -cmin n : Modified in the past n minutes

(17) -cnewer file: a file newer than the file file

(18) -ctime n : Files that have been modified in the past n days

This command also provides options for performing specific operations on the found files.

(1) -exec cmd{}: Execute the given Linux command for the qualified file without asking the user whether to execute the command. Indicates that the parameter of the command is the found file; the end of the command must end with "\;".

(2) -ok cmd{}: Execute the given Linux command for the qualified file. Unlike exec, it will ask the user whether to execute the command.

(3) -ls: List all the files found in detail.

(4) -fprintf file name: Write the found file name to the specified file.

(5) -print: Display the found file name on the standard output device.

example

List all files with extension c in the current directory and its subdirectories.

# find . -name "*.c"

List all general files in the current directory and its subdirectories

# find . -ftype f

List all files updated in the last 20 minutes under the current directory and its subdirectories

# find . -ctime -20

Find ordinary files in the /var/logs directory with a change time older than 7 days, and ask them before deleting:

$ find /var/logs -type f -ctime +7 -ok rm { } ;

Find files in the previous directory where the owner of the file has read and write permissions, and the user of the group to which the file belongs and other users have read permissions:

$ find . -type f -perm 644 -exec ls -l { } ;

To find all normal files with a file length of 0 on the system and list their full paths:

$ find / -type f -size 0 -exec ls -l { } ;

Find ordinary files in the /var/logs directory with a change time older than 7 days and ask them before deleting:

$ find /var/logs -type f -mtime +7 -ok rm { } ;

Find all files starting with main in the current directory and display the contents of those files.

$ find . - name 'main*' - exec more {} \;

Delete all a.out or *.o files that have not been accessed within a week in the current directory

$ find . \(- name a.out - o - name '*.o'\)\ > - atime +7 -exec rm {} \;

4. locate command:

locate [-d ][--help][--version][template style...]

parameter: 

  • -d or --database= Configure the database used by the locate command. The database preset by the locate command is located in the /var/lib/slocate directory, and the file name is locate.db. You can use this parameter to specify otherwise.
  • --help Online help.
  • --version Display version information.

Additional information

locate is different from find: find is to find on the hard disk, and locate is only found in the /var/lib/slocate database.

The speed of locate is faster than find. It does not really search, but checks the database. The general file database is in /var/lib/slocate/slocate.db, so the search of locate is not real-time, but based on the update of the database Generally, the system maintains itself, or you can manually upgrade the database. The command is:

locate -u 

Display file content commands - cat, more, less, head, tail

1. cat command

The main function of this command is to display the file, which can read the content of the file referred to in turn and output it to the standard output device. In addition, it can also be used to join two or more files to form a new file. The common form of this command is as follows:

cat [option] filename

The meanings of each option in the cat command are as follows:

(1) v: Display control characters in a special form, except for LFD and TAB.

(2) T: Display TAB as "^I". This option should be used together with the -v option, that is, if the -v option is not used, this option will be ignored.

(3) E: Display a "$" symbol at the end of each line. This option is to be used together with the -v option.

(4) u: The output does not go through the buffer.

(5) A: Equivalent to -vET.

(6) t: Equivalent to -vT.

(7) e: Equivalent to -vE.

example:

The content of the Readme.txt file is displayed on the screen.

# cat Readme.txt

Display the content of the Readme.txt file on the screen, if the file contains special characters, it will be displayed together.

# cat - A Readme.txt

Merge the contents of the file test1 and the file test2 and put them into the file test3. (At this time, the result of the command execution cannot be directly seen on the terminal screen, that is, the content of the file test3. If you want to see the content of the connected file, you can use the cat test3 command)

# cat test1 test2 > test3

2.more command

In the process of viewing the file, because some text is too large, the text flashes across the screen quickly, and the user has no time to see the content clearly. This command can display one screen of text at a time, and print "--more--" at the bottom of the terminal, and the system will also display the percentage of the displayed text to the total text at the same time. To continue displaying, press Enter or Spacebar.

more [option] filename

The meanings of some commonly used options in the more command are as follows.

(1) -p : Clear the screen before displaying the next screen.

(2) -c: The effect is similar to -p.

(3) -d: Display a more friendly prompt message as "--more-- (XX%) [Press space to continue,'q' to quit]" at the bottom of each screen.

(4) -s: Consecutive blank lines in the file are compressed into one blank line for display.

In addition, during the execution of the more command, users can use a series of commands to dynamically select the displayed part according to their needs. After more displays a screen of content, it will stop and wait for the user to enter a command. Several commonly used commands are listed below:

(1) n: n When multiple file names are specified in the command line, this command can be used to display the i-th file. If i is too large (out of bounds), the last file in the file name list will be displayed.

(2) p: p When multiple file names are specified in the command line, this command can be used to display the i-th file from the bottom. If i is too large (out of bounds), the first file will be displayed.

(3) f: f displays the file name and line number of the current file.

3. less command

The function of this command is basically the same as that of the more command, and it is also used to display files by page. The difference is that when the less command displays a file, the user can browse the file line by line both forward and backward, while the more command can only browse the file backward. Since the use of the parameters of this command is similar to that of the more command, details are not repeated here. If you want to display test files by page, execute the following command:

# less test

If you want to browse backwards, you can use the [Page Up] key on the keyboard; if you want to browse the file forward, you can use the [Page Down] key on the keyboard accordingly. Press the arrow keys to scroll line by line, and press the [Q] key to exit.

4.head command

This command is used to display the first few lines of data in a file. If the user wants to see what is actually saved in a file, he only needs to view the first few lines of the file instead of browsing the entire file, and he can use this command. The common form of this command is as follows:

head - number filename

This command is used to display the first n lines of each specified file. If no value for n is given, it defaults to 10. If no file is specified, head reads from standard input. For example, the following command displays the first 3 lines of the file test.c. # head - 3 test.c

5.tail command

Corresponds to the function of the head command. If you want to see the end of the file, you can use the tail command. This command displays the specified content of a file. He displays the contents of the specified display range of the specified file on the standard output. Likewise, if no filename is given, the standard input file is used. The common form of this command is as follows.

tail option filename

The meanings of each option in the tail command are as follows.

(1) +num: display from line num onwards.

(2) -num: display from num lines from the end of the file; if the num parameter is omitted, the system defaults to 10.

(3) 1: The counting unit of num is the text line; when used together with the parameter option +num or -num option, num represents the number of text lines to be displayed. (This is the default option of the system, that is, by row)

(4) c: The counting unit of num is byte; when used together with the parameter option +num or -num option, num represents the number of characters to be displayed.

File content query commands - grep, egrep, fgrep

1.grep

  The grep command is used to search for files whose content contains the specified template style. If the content of a file is found to match the specified template style, the default grep command will display the column containing the template style. If no file name is specified, or the file name given is "-", the grep command will read data from the standard input device.

grammar

grep [-abcEFGhHilLnqrsvVwxy][-A<number of display columns>][-B<number of display columns>][-C<number of display columns>][-d<action>][-e<template style>][- f<template file>][--help][template style][file or directory...]

Parameters :

  • -a or --text Do not ignore binary data.
  • -A<number of displayed columns> or --after-context=<number of displayed columns> In addition to displaying the column that conforms to the template style, and display the content after the column.
  • -b or --byte-offset Before displaying the column that conforms to the template style, mark the bit number of the first character of the column.
  • -B<number of displayed columns> or --before-context=<number of displayed columns> In addition to displaying the column that conforms to the template style, and display the content before the column.
  • -c or --count Calculate the number of columns that match the template style.
  • -C<number of displayed columns> or --context=<number of displayed columns> or -<number of displayed columns> In addition to displaying the column that conforms to the template style, and display the content before and after the column.
  • -d <action> or --directories=<action> This parameter must be used when it is specified that the directory to be searched is not a file, otherwise the grep command will report information and stop the action.
  • -e<template_pattern> or --regexp=<template_pattern> specifies a string as a template pattern for finding file content.
  • -E or --extended-regexp Use the template style as an extended common notation.
  • -f<template file> or --file=<template file> Specify the template file, its content contains one or more template styles, let grep find the file content that meets the template conditions, the format is one template style per column.
  • -F or --fixed-regexp Treat template styles as a list of fixed strings.
  • -G or --basic-regexp Use the template style as a normal expression.
  • -h or --no-filename Before displaying the column that conforms to the template style, do not indicate the file name to which the column belongs.
  • -H or --with-filename Before displaying the column that conforms to the template style, indicate the file name to which the column belongs.
  • -i or --ignore-case ignores differences in character case.
  • -l or --file-with-matches lists the file names whose content matches the specified template style.
  • -L or --files-without-match lists the file names whose contents do not match the specified template style.
  • -n or --line-number Before displaying the column that conforms to the template style, mark the column number of the column.
  • -q or --quiet or --silent do not display any information.
  • -r or --recursive The effect of this parameter is the same as specifying the "-d recurse" parameter.
  • -s or --no-messages do not display error messages.
  • -v or --revert-match reverse the lookup.
  • -V or --version Display version information.
  • -w or --word-regexp Only display columns that match whole words.
  • -x or --line-regexp Only display columns that match all columns.
  • -y The effect of this parameter is the same as specifying the "-i" parameter.
  • --help Online help.

example

1. In the current directory, search for the file containing the string "test" in the file with the suffix "test", and print out the line of the string. At this point, you can use the following command:

grep test *file 

The result looks like this:

$ grep test test* #Find files with the suffix "test" containing the string "test"  
testfile1:This a Linux testfile! #List the lines containing test characters in the testfile1 file  
testfile_2:This is a linux testfile! #List the lines containing test characters in the testfile_2 file  
testfile_2:Linux test #List the lines containing test characters in the testfile_2 file

2. Find qualified files recursively. For example, to search for files containing the string "update" in all files under the specified directory /etc/acpi and its subdirectories (if subdirectories exist), and print out the content of the line where the string is located, the command used is:

grep -r update /etc/acpi 

The output is as follows:

$ grep -r update /etc/acpi #Find "etc/acpi" recursively  
# file containing "update"  
/etc/acpi/ac.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.)  
Rather than  
/etc/acpi/resume.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of  
IO.) Rather than  
/etc/acpi/events/thinkpad-cmos:action=/usr/sbin/thinkpad-keys--update 

3. Reverse lookup. The previous examples are to find and print out the lines that meet the conditions, and the content of the lines that do not meet the conditions can be printed out through the "-v" parameter.

Find the line that does not contain test in the file that contains test in the file name. At this time, the command used is:

grep -v test*

The result looks like this:

$ grep-v test* #Find lines that do not contain test in files containing test in the file name  
testfile1:helLinux!  
testfile1:Linis a free Unix-type operating system.  
testfile1:Lin  
testfile_1:HELLO LINUX!  
testfile_1:LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM.  
testfile_1:THIS IS A LINUX TESTFILE!  
testfile_2:HELLO LINUX!  
testfile_2:Linux is a free unix-type opterating system.  

2.fgrep

This command is equivalent to executing the grep command plus the parameter "-F", see the description of the grep command for details .

The Linux fgrep command is used to find qualified strings in files.

grammar

fgrep [template pattern] [file or directory...]

3.egrep

The Linux egrep command is used to search for a specified string within a file.

The execution effect of egrep is similar to "grep-E". The syntax and parameters used can refer to the grep command. The difference from grep lies in the method of interpreting the string.

egrep is interpreted with extended regular expression grammar, while grep is interpreted with basic regular expression grammar. Extended regular expression is more standardized than basic regular expression.

grammar

egrep [template pattern] [file or directory] 

Parameter Description:

  • [Template pattern]: String rules to find.
  • [file or directory]: the target file or directory to search for.

example

Display the matching characters in the file. For example, to find all files in the current directory that contain the string "Linux", you can use the following command:

egrep Linux *

The result looks like this:

$ egrep Linux * #Find files containing the string "Linux" in the current directory  
testfile:hello Linux! #The following five lines contain Linux characters in the testfile  
testfile:Linux is a free Unix-type operating system.  
testfile:This is a Linux testfile!  
testfile:Linux  
testfile:Linux  
testfile1:helLinux! #The following two lines contain Linux characters in testfile1  
testfile1:This a Linux testfile!  
#The following two lines contain Linux characters in testfile_2  
testfile_2:Linux is a free unix-type opterating system.  
testfile_2:Linux test  
xx00:hello Linux! #xx00 Lines containing Linux characters  
xx01:Linux is a free Unix-type operating system. #The following three lines contain Linux characters in xx01  
xx01:This is a Linux testfile!  
xx01:Linux 

Text processing commands - sort, uniq

1.sort command

The function of this command is to sort the lines in the file (duplicate lines cannot be deleted and uniq can cover duplicate lines). This command sorts the contents of the file line by line. If the first characters of the two lines are the same, the command will continue to compare the next characters of the two lines. sort sorting is done based on the comparison of one or more keys extracted from the input rows. A sort key defines the smallest sequence of characters used for sorting. By default, keywords are sorted in ASCLL character order on the entire row.

The common format of the sort command is as follows:

sort [option] filename

The main options of this command to change the default settings are as follows:

(1) -m: If the given files are sorted, merge the files.

(2)-c: Check if the given files are sorted, if they are not all sorted, print an error message and exit with status value 1.

(3) -u: Keep only one of the rows that are considered the same after sorting.

(4) -o: The output file writes the sorted output to the output file instead of the standard output. If the output file is one of the input files, sort writes the contents of the file to a temporary file, and then sorts and writes the output result.

(5) -d: Sort lexicographically, only letters, numbers, spaces and tabs are meaningful when comparing.

(6) -f: Treat lowercase letters the same as uppercase letters.

(7) -I: Ignore non-printing characters.

(8)-M: As month comparison, "JAN"<"FEB"<?<"DEC".

(9) -r: Output sorted results in reverse order.

(10) +posl-pos2: Specify one or several fields as sorting keys, the field position starts from posl and ends at pos21 (including posl, excluding pos2). If pos2 is not specified, the keyword is from posl to the end of the line. Bits for fields and characters start at 0.

(11)-b: Ignore leading blanks (spaces and tabs) when looking for sort keys in each line.

(12) -t separator: Specify the character separator as the field separator.

2.unique 

Duplicate lines may appear in its output file after the file is processed. For example, after using the cat command to merge two files, and then use the sort command to sort, duplicate lines may appear. At this time, you can use the uniq command to delete these duplicate lines from the output file, leaving only a unique sample of each record.

The common format of the uniq command is as follows:

uniq [option] filename

The options of this command have the following meanings:

(l)-d: Only display duplicate lines.

(2) -u: Only display non-repeated lines in the file.

  (3) -c: In the display output, add the number of occurrences of this line in the file at the beginning of each line. It replaces the -u and -d options. (4) -n: The first n fields are ignored together with blanks before each field. A field is a non-space, non-tab string, separated from each other by tabs and spaces (fields are numbered starting from 0).

  (5) +n: The first n characters are ignored, and the previous characters are skipped (characters are numbered from 0).

  (6) -fn: Same as -n, where n is the number of fields.

(7) -sn: Same as +n, where n is the number of characters.

............. 

Guess you like

Origin blog.csdn.net/weixin_54401017/article/details/127438964