60 commands that Linux must learn

Source: Internet
Author: User
Tags control characters diff compressed file types egrep

Linux system information is stored in a file, and the file is similar to ordinary official documents. Each file has its own name, content, storage address, and other administrative information, such as the user of the file, the size of the file, and so on. A file can be a letter, an address book, or a source statement for a program, a program's data, or even an executable program or other non-body content. Linux file system has a good structure, the system provides a lot of file processing programs. This article mainly describes the commonly used file processing commands.
File 1. Action files determine the document type by probing the contents of the file, and the use rights are all users.
2. format file [options] file name 3. [Options] Main parameter-V: Displays the version information after the standard output, and exits.
-Z: Detects compressed file types.
-L: Allow the connection to be met.
-F Name: Reads the list of file names to parse from the file NameFile.
4. Use the file command to know whether a file is a binary (elf format) executable, a shell script file, or any other format. File types that can be identified include directories, shell scripts, English text, binary executables, C-language source files, text files, and DOS executable files.
5. Application example if we see a file with no suffix grap, you can use the following command:
$ file Grap grap:english text at this point the system shows that this is an English text file. Note that the file command is not able to detect multimedia file types including graphics, audio, video, and so on.
mkdir 1. The role of the mkdir command is to create a subdirectory named DirName, similar to the MD command under MS DOS, whose use rights are all users.
2. Format mkdir [options] Directory name 3. [Options] Main parameter-m,--mode= mode: Set permissions < mode, similar to chmod.
-P,--Parents: Creates a top-level directory when needed, and is not considered an error if the directory already exists.
-V,--verbose: Displays information each time a new directory is created.
--version: Show version information and leave.
4. The application instance can set permissions for the directory at the time of catalog creation, and the parameter used is "-M". Assuming that the directory name you want to create is "tsk", so that all users have rwx (that is, read, write, execute), you can use the following command:
$ mkdir-m 777 tsk grep 1. The function grep command allows you to specify the search for specific content in a file, and the standard output of the row containing the content. The grep full name is global Regular expression Print, which represents the globally regular expression version, and its use rights are for all users.
2. Format grep [options] 3. Main parameters [options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
<: Starts with the line that matches the regular expression.
: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.
Regular expressions are a very important concept in the Linux/unix system. A regular expression (also known as "regex" or "regexp") is a pattern (pattern) that can describe a class of strings. If a string can be described with a regular expression, we say that the character matches the regular expression (match). This and the DOS user can use the wildcard character "*" to represent any character similar. On Linux systems, regular expressions are often used to find patterns in text, and to perform "search-and-replace" operations and other functions on text.
4. Application instance querying the DNS service is one of the daily tasks, which means maintaining a large number of IP addresses that cover different networks. Sometimes there are more than 2000 IP addresses. If you want to see the NNN.NNN network address, but forget the rest of the second part, only know that there are two periods, such as nnn nn ... To extract all of the nnn.nnn IP addresses, use [0-9]{3}. [0-0{3}. The meaning is that any number appears 3 times, followed by a period, followed by any number 3 times, followed by a period.
$grep ' [0-9]{3}. [0-0{3} ' ipfile added that the grep family also includes Fgrep and Egrep. Fgrep is fix grep, which allows to find strings instead of a pattern; Egrep is an extended grep that supports both basic and extended regular expressions, but does not support the application of the Q-mode range and some of the more canonical patterns corresponding to it.
DD 1. The function DD command is used to copy files and convert and format data according to parameters.
2. Format dd [options] 3. [opitions] main parameter bs= byte: Force ibs=< byte > and obs=< byte >.
cbs= bytes: Each conversion of the specified < bytes >.
conv= Keyword: Converts a file based on a comma-delimited representation of a keyword.
count= number of blocks: only the input data of the specified < block number > is copied.
ibs= Bytes: Reads the specified < bytes > each time.
if= file: Read < file > content, not standard input data.
obs= Bytes: Writes the specified < byte > each time.
of= file: Writes data to the < file, but not the standard output display.
Number of seek= blocks: first skip the output data for the specified < block number > in OBS units.
Number of skip= blocks: first skip the input data for the specified < block number > in IBS.
4. Application examples
The DD command is often used to make Linux boot disks. Find a bootable kernel, point its root device 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 code above shows that using the Rdev command to point the root device in the bootable kernel vmlinuz to/dev/hda, replace "HDA" with its own root partition, and then write the kernel to a floppy disk using the DD command.
Find 1. The role of the Find command is to search for files in the directory, and its use rights are for all users.
2. Format find [path][options][expression] path Specifies the directory path from which the system starts to look down the file down the directory tree. It is a list of paths that are separated from each other by spaces, and if you do not write path, the current directory is assumed.
3. Main parameters [options] Parameters:
-depth: Use the Depth-level lookup process to prioritize file content in a specific level of the specified directory.
-maxdepth levels: Indicates that at most find the first level subdirectory of the start directory. Level is a non-negative number, and if level is 0, it is only found in the current directory.
-mindepth levels: Indicates that at least the level subdirectory of the start directory is found.
-mount: Not found in directories and files in other file systems (such as MSDOS, VFAT, etc.).
-version: Print version.
[Expression] is a matching expression and is an expression accepted by the Find command, and all operations of the Find command are for an expression. It has a lot of parameters, and here are just a few common parameters.
-name: Supports wildcard characters * and?.
-atime N: Searches for files that have been read in the last n days.
-ctime N: Searches for files that have been modified in the last n days.
-group Grpoupname: Search for all files with Grpoupname group.
-user User name: Searches for all files that belong to the primary user name (ID or name).
-size N: The file size of the search file is n blocks.
-print: Output search results, and print.
4. How to apply the Find command to locate a file in several ways:
(1) Search by file name For example, if we want to find a file with a filename of lilo.conf, you can use the following command:
The "/" after find/-name lilo.conf the Find command means searching the entire hard disk.
(2) Quick Find files finding files based on file names can have a real problem, which is to take a long time, especially when large Linux file systems and high-capacity hard disk files are placed in deep subdirectories. If we know that this file is stored in a directory, you can save a lot of time by looking down in that directory. such as the smb.conf file, from its file suffix ". conf" can be determined that this is a configuration file, then it should be in the/etc directory, at this time can use the following command:
Find/etc-name smb.conf This way, you can shorten the time by using the Quick Find file method.
(3) Based on partial file name lookup method Sometimes we know that only one file contains the ABVD 4 words, then to find all the files in the system that contain these 4 characters, you can enter the following command:
Find/-name ' *abvd* '
After entering this command, the Linux system will look in the/directory for all files containing the ABVD 4 characters (where * is a wildcard character), such as Abvdrmyz and other eligible files can be displayed.
(4) Find a file using a hybrid lookup method
The find command can use a hybrid lookup method, for example, if we want to find a file that is larger than 500000 bytes in the/etc directory and modified within 24 hours, you can use-and (and) to link the two lookup parameters together into a mixed lookup.
Find/etc-size +500000c-and-mtime +1
MV 1. The Action MV command is used to rename a file or directory, or to move a file from one directory to another, and its use rights are for all users. This command is like a combination of Ren and move in a DOS command.
2. Format Mv[options] source file or directory destination file or directory 3. [Options] Main parameter-I: interactive mode operation. If the MV operation will result in overwriting an existing target file, then the system asks whether to overwrite it, asking the user to answer "y" or "n", which avoids overwriting the file by mistake.
-F: Disables interactive operation. MV operation to overwrite an existing target file without giving any indication, after specifying this parameter the I parameter will no longer work.
4. Application example (1) Moves all files in the/USR/CBU to the current directory (with "." Representation) in:
$ mv/usr/cbu/*. (2) Rename the file Cjh.txt to Wjz.txt:
$ mv cjh.txt wjz.txt LS 1. The function ls command is used to display the contents of the directory, similar to the dir command under DOS, its use rights are all users.
2. Format ls [options][filename] 3.options main parameter-a,--all: do not hide any with "." The character starts the item.
-A,--almost-all: list except "." and ".." Other than the project.
--author: The author of each document is printed.
-B,--Escape: A non-printable character is represented in an octal overflow sequence.
--block-size= Size: The block is in bytes that specify < size >. -B,--ignore-backups: Do not list any items that end with the ~ character.
-F: Do not sort,-au parameter is valid,-LST parameter is invalid.
-F,--Classify: Add an indicator of the file type (*/[email protected]| one).
-g:like-l, but does not list owner.
-G,--no-group:inhibit display of group information.
-I,--inode: Lists the inode number for each file.
-I,--ignore= style: Do not print any items that match the shell character < style >.
-K: that is--block-size=1k.
-L: Lists information in a longer format.
-L,--dereference: When displaying the file information of a symbolic link, displays the object indicated by the symbolic link, not the information of the symbolic link itself.
-M: All items are separated by commas and fill the entire row width.
-N,--numeric-uid-gid: Similar to-l, but lists UID and GID numbers.
-N,--literal: Lists the names of the unprocessed items, such as control characters that are not handled specifically.
-P,--file-type: plus an indicator of the file type (/[email protected]| one).
-Q,--quote-name: Enclose the project name in double quotation marks.
-R,--Reverse: in reverse order.
-R,--Recursive: Lists all subdirectory tiers at the same time.
-S,--size: In order of block size.
Diff 1. The diff command is used for comparisons between two files and indicates the difference between the two, and its use rights are for all users.
2. Format diff [Options] source file destination file 3. [Options] Main parameter-A: treats all files as text files.
-B: Ignores the difference caused by whitespace.
-B: Ignores the difference caused by empty rows.
-C: Use the schema output format. -H: Use heuristics to accelerate searches of large files.
-I: Ignores case changes.
-N--rcs: Output RCS format.
CMP 1. The function cmp (abbreviation for "compare") command is used to briefly indicate whether there are differences in two files, and its use rights are for all users.
2. Format Cmp[options] File name 3. [Options] Main parameter-L: The byte is output in decimal mode, and it is convenient to output two different files in eight binary mode.
Cat 1. The role of the cat ("concatenate" abbreviation) command is used to connect and display information about one or more files specified, and its use rights are for all users.
2. Format cat [options] file 1 file 2 ...
3.[options] Main parameter-N: The number of rows for all outputs starts at the first line.
-B: Similar to-n, except that blank lines are not numbered.
-S: A blank line that is substituted for a row when there are more than two consecutive lines of blank rows.
4. Application examples (1) The simplest use of a cat command is to display the contents of a text file. For example, we want to take a look at the contents of the Readme file at the command line and use the command:
$ cat README (2) Sometimes you need to process several files into a file and save the results of this processing to a separate output file. The cat command accepts one or more files on its input and prints them as a separate file to its output. For example, after adding a line number (blank line) to the file contents of the Readme and install, append the contents to a new text file File1:
$ cat README INSTALL File1 (3) Another important feature of cat is the ability to number rows, as shown in Figure 2. This functionality is convenient for programming documentation, as well as legal and scientific documentation, and printing on the left line number makes it easy to reference a portion of a document, which is important in programming, scientific research, business reporting, and even legislative work.
The line numbering function has a-B (can only be numbered for non-blank lines) and-n (can be numbered for all rows) two parameters:
$ cat-b/etc/named.conf
ln 1. The function of the LN command is used to create a link between files, and its use rights are for all users.
2. Format ln [options] source file [link name] 3. Parameter-F: The source file is first deleted when the link is closed.
-D: Allows system administrators to hard-link their own directories.
-S: Soft link (symbolic link).
-B: Files that will be overwritten or deleted at the time of the link are backed up.
There are two types of links, one called hard links, and the other called Symbolic links (symbolic link). By default, the LN command produces a hard link.
A hard join refers to a connection made through an index node. In a Linux file system, a file saved in a disk partition, regardless of the type, assigns a number to it, called the index node number (Inode index). In Linux, multiple file names point to the same index node that exists. In general, this connection is a hard connection. The purpose of a hard connection is to allow a file to have multiple valid pathname, so that users can establish a hard connection to important files to prevent "accidental deletion" of the function. The reason for this is as 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 and other connections, and the connection to the file's data block and directory will be released only if the last connection is deleted. In other words, the file is actually deleted.
In contrast to a hard connection, there is another connection in the Lnux system, called a symbolic connection (SYMBILC link), also known as a soft connection. A soft link file is a bit like a shortcut to Windows. It is actually a kind of special file. In a symbolic connection, a file is actually a text file that contains location information for another file.
Hands-on exercises
Above we introduced the Linux file Processing command, here are a few examples, you can practice the command you just said.
1. Quick access to key directories with symbolic links
Symbolic links are a very useful feature. Suppose there are directories or files that need to be used frequently, but because of the file and directory structure of Linux, this file or directory is in a very deep subdirectory. For example, the Apache Web server document is in the/usr/local/httpd/htdocs of the system and does not want to enter such a long path every time from the home directory (in fact, this path is very difficult to remember).
To solve this problem, you can create a symbolic link in the home directory so that you only have to enter the link when you need to enter the directory.
For easy access to the directory where the Web server (/usr/local/httpd/htdocs) document resides, you can use the following command in the home directory: $ ln-s/usr/local/httpd/htdocs GG
This way, every time you enter the GG directory, you can access the Web server's documents, and if you no longer have access to the Web server's documents, delete GG, and the real Web server's documentation is not deleted.
2. Use the DD command to import root.ram content in INIT.RD format to memory dd if=/dev/fd0 OF=FLOPPY.FD DD if=root.ram of=/dev/ram0 # 3.GREP Command system call
grep is one of the most widely used commands in Linux/unix, which can be called inside many Linux systems.
(1) If you want to query the directory in the directory list, the method is as follows:
$ ls-l | grep ' ∧d '
(2) If you are querying a directory for all files that do not contain a directory, the method is as follows:
$ ls-l | grep ' ∧[∧d] '
(3) Use the Find command to invoke grep, such as "Chinput" in all c source code, by doing the following:
$find/zhxwin-name *.c-exec grep-q-s Chinput {};-print

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.