In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Basic introduction of Linux system commands
For a long time, Linux operating system has been widely used in the field of enterprise servers because of its stability, efficiency and reliability.
Most of the management and maintenance of the Linux server is done by remote login.
Most of the management and maintenance operations are based on the Linux command, so the Linux command is very important in the work.
Before you can use the Linux command, you need to understand how it works
The principle is very simple, in fact, we users enter a string of character commands, shell will "translate" this command into a "language" that the system kernel can understand, and then the system kernel will perform the relevant operations according to this command.
After explaining the principle, let's take a look at the types of Linux commands.
In fact, it is very simple. There are two kinds of Linux commands, internal commands and external commands.
The specific differences are as follows
Now that we know the type of Linux command, let's take a look at the structure of the command
A Linux command, generally composed of three parts, namely: command word, option, parameter
In these three parts, the command word is the most important part, and it is indispensable, and the other two parts can be avoided according to the specific situation.
Options, divided into long format options and short format options, long format options are guided by "-" symbols, generally followed by words to represent a function; short format options are guided by "-" symbols, usually followed by a single letter as a function.
Parameter, which is the processing object of the command word, can be a file name, directory name, user name, etc., and the number can be zero to more than one
Before introducing the basic commands, let's learn about some auxiliary operations on the Linux command line. Mastering these operations can help us to better use Linux commands.
Tab key: automatically complete the backslash "\": force line wrap shortcut key Ctrl+U: clear to the beginning of the line shortcut key Ctrl+K: clear to the end of the line shortcut key Ctrl+L: clear screen shortcut key Ctrl+C: cancel the editing of this command
The automatic completion function of the tab key is my favorite auxiliary operation. When you enter the parameters, you find that the directory name is too long. At this time, you only need to enter the first few letters and press the tab key to automatically complete the directory name for you. For example, if I want to enter the directory name "sysconfig", I can automatically complete it by typing the tab key directly after "sysco".
Backslash "\" is used to force line breaks. If I want to enter the command vim / etc/sysconfig/network-scripts/ifcfg-ens33, I can use "\" anywhere in the middle to force line breaks.
Ctrl+U empties to the beginning of the line, which simply deletes the characters in front of the cursor
Ctrl+K empties to the end of the line, which simply deletes all characters behind the cursor
Ctrl+L clears the screen, not to delete previously entered commands, but when you enter too many commands that occupy all the user interface, it allows you to start typing again from the first line of the user interface
Ctrl+C cancels the editing of this command to terminate the command. For example, when I use the ping command to check whether the network is interconnected, it will continue to send packets, and I can use Ctrl+C when I want to stop
How to get command help:
In addition to these auxiliary operations, when we forget a command, or do not know what a command means, we can get command help, as follows:
Internal command help
Options:
-L prints the value of the $PWD variable, if it names the current working directory-P prints the current physical path without any symbolic links to view help for Bash internal commands. You only need to type "help" to see some commands and their functions, but not all of them.
The "--help" option of the command applies to most external commands. When you suddenly forget the function of a command, or forget the function of the option, you can directly add "--help" to the command to see the specific function of the command.
Use the man command to read the manual page, use the up Arrow and Down Arrow to scroll the text, use the Page Up and Page Down keys to turn the page, press Q or Q key to exit the reading environment, press the "/" key to find the content
Because when I install Linux, I install a graphical interface, and many servers will not install a graphical interface when I install it, so you can't scroll through the "--help" query, and you can't see a lot of content.
So you can use the "man" command to make up for the shortcomings of the "--help" command. It should be that the "man" command can enter a reading environment and can scroll and turn pages.
Basic operation of directories and files
When we enter the command, there will be a string of characters [root@localhost ~] #. What exactly do these characters represent? I will explain them one by one below.
Root: current system login user, you can use su plus user name to switch users localhost: host name, you can use hostname command to view, use hostnamectl set-hostname plus a new host name to change the host name ~: current location ("~" represents home directory) #: represents current administrator identity $: represents current ordinary user identity
With all this in mind, let's start with the basic Linux commands.
Command: "pwd" to view the current working directory (Print Working Directory)
"pwd" is used to view the current directory. Remember to check the absolute path. For example, if I use "pwd" to view the directory under the sysconfig directory, it will show the absolute path / etc/sysconfig.
When it comes to absolute paths, let's say that paths are divided into absolute paths and relative paths. The absolute path is relatively simple, starting with the root directory "/" and going all the way to the current directory.
There are several forms of relative path expression:
Command: "cd" switch working directory (Change Directory)
"cd" is used to switch working directories. For example, I want to enter the / etc/sysconfig/network-scripts directory.
You can enter this directory by typing "cd / etc/sysconfig/network-scripts" directly.
In addition to switching to the specified working directory, the "cd" command has several options:
"cd ~" or "cd": switch to the current user's host directory
"cd -" changes to the directory where the "cd" command was last executed
"cd... Return to the directory one level above the current directory
When representing the location of a directory (or file) in the Linux operating system, depending on the starting directory it references, you can use two different forms, called absolute path and relative path, respectively.
Absolute path: this approach starts with the root directory "/". For example, "/ boot/grub" represents the grub directory in the boot subdirectory under the root directory. To indicate exactly that grub is a directory (not a file), you can also add a directory delimiter at the end, such as "/ boot/grub/". Because there is only one root directory in the Linux operating system, using an absolute path can very accurately indicate the location of a directory (or file), no matter which directory you are currently in. However, if the path is long, it will be more tedious to enter.
Relative path: this approach generally starts with the current working directory and does not use the "/" symbol at the beginning, so it is shorter and easier to use. The relative path mainly includes the following forms: the directory name or file name is directly used to indicate the location of the subdirectories and files in the current working directory. For example,
"grub.conf" can represent the grub.conf file in the current directory.
Use a period. The beginning can be clearly indicated with the current working directory as the starting point. For example, ". / grub.conf" can also represent a grub.conf file in the current directory. Use two periods ".." The beginning means starting with the directory one level above the current directory (the parent directory). For example, if the former is in the / boot/grub/ catalog, ".. / vmlinuz" is equivalent to "/ boot/vmlinuz". Starting with the form "~ user name" means that the specified user's host directory is used as the starting point, and the current user is the default when the user name is omitted. For example, "~ teacher" represents the host directory of the teacher user, while "~" can represent the host directory of the current user.
Command: "ls" displays the contents of the current directory ("dir" command is similar to its function, but rarely used, generally using "ls"). The commonly used options are "- l", "- a", "- A", "- d", "- h", "- R", "--color"-l: display a list of files and directories in long format (Long), including permissions, size, last update time and other details. When the-l option is not used, the ls command displays directory name or file name information in a short format by default. -a: displays information about all (All) subdirectories and files, including names with a period. Hidden directories and hidden files at the beginning. -A: the effect is basically similar to that of the-an option, but two special hidden directories are not displayed, that is, the "." And the ".." that represents the parent directory. -d: displays the properties of the directory (Directory) itself, rather than the contents of the directory. -h: displays the size of the directory or file in a more user-friendly (Human) way. The default size unit is bytes (B). When you use the-h option, it will be displayed as KB, MB, and so on. This option needs to be used with the-l option. -R: displays all the contents of the specified directory and its subdirectories Recursive. -- color: distinguishes different files by color in character mode. If "--color=tty" is used, the table shows the use of the terminal predefined color scheme. In general, dark blue represents a directory, white or black (white if the background color is black, otherwise interchangeable) represents a general file, green represents an executable file, yellow color represents a device file, and red represents a compressed file.
"ls" simply displays the contents of the current directory, and when you add different options, you can achieve different functions. I will describe it in detail below:
"ls-a" lists all files in the directory (showing hidden files), including ".", ".." The first hidden file, and "ls-A" also shows hidden files. The only difference between "ls-a" and "hidden files" is that they do not show "." The hidden file at the beginning.
"ls-d" displays the directory like a file, not the files under it
"ls-l" displays the details of the files in the directory, such as creator, creation time, list of read and write permissions for the file, and so on.
"ls-h" shows the size of the files under the directory in the form of units. If only the "ls-l" file size is unified in "bytes", we have to convert the units ourselves.
"ls-R" lists the files of all the subdirectories under the directory, which is equivalent to the "recursive" implementation in our programming.
"ls-color" distinguishes files in different formats with different colors. This command is generally executed by default. Here is a description of the file types represented by common colors:
Black-data file green-executable red-compressed file dark blue-directory (folder) light blue-soft link file (shortcut)
In addition to these common options, there are wildcard characters "?" and "*"
"?" Is an unknown character that matches the file name. For example, if you want to view a file with the "ls" command, you can use "?" if you are not sure how many letters the file name contains. Replace
"*" matches any number of characters in a file. For example, if you want to view a file with the "ls" command, but you only remember one of the letters, you can use this letter plus "*" to find it.
Command: "du" statistics directory and file space occupancy (Disk Usage)
Common options:
-a: statistics of disk space usage include all files, not just directories. -h: the statistical results are displayed in a more humane way (KB counts by default, but no units are displayed). Using the-h option, KB, MB, and other units are displayed. -s: only the total Summary of each parameter is counted, not the size of each subdirectory and file.
"du-a" counts disk space usage including all files, not just directories
"du-h" displays the statistical results in a more humane way, using KB as the count by default, but not in units.
"du-s" only counts the total size of the space occupied, but not each subdirectory and file size.
Of course, these options can also be combined. For example, if you want to count the total size of the / var/log/ directory, you can enter it if you show the specific units.
"du-sh / var/log/" for statistics
Command: "alias" set alias
When you find it too troublesome to enter a command that you need to use frequently, you can give it a simple alias to make it easy to enter.
For example, if you want to set "ls-alh" to "myls" individually, you can type "alias myls='ls-alh'".
Create directories and files
Command: "mkdir" create a new directory (Make Directory)
If you want to create a new directory aaa under the current directory, you can type "mkdir aaa"
Create multiple directories at the same time, just add a space between the directories, such as "mkdir 1112222333"
"mkdir-p" can create nested multi-tier directories at one time, otherwise you can only create a directory first, and then enter this directory through the "cd" command to create subdirectories.
Command: "touch" create an empty file
The "touch" command when you do not specify the directory in which to create files, it will be created by default in the current directory, and when creating multiple files at the same time, you only need to separate the files with spaces.
The file created by the "touch" command is an empty file. When we want to add the contents together when we create the file, we can use the "echo" command. For example, I want to create a file bbb01.txt in the / root/aaa/bbb/ directory with the content "this is bbb01". I can type "echo" this is bbb01 "> / root/aaa/bbb/bbb01.txt".
Command: "ln" to create a linked file
* * format: * * ln [- s] Source file or directory link file or destination location
Before you talk about the "ln" command, take a look at the linked file, which is a shortcut similar to the Windows system. There are two kinds of links, soft links (also known as symbolic links) and hard links. Here are two kinds of links:
When using the ln command, hard links are created by default. If you need to create a soft link, you must combine the "- s" option
Be sure to specify. The basic usage format of the ln command is as follows:
Ln [- s] Source file or directory "link file or destination location"
When the target location is an existing directory, the newly created link file is saved to that directory and is used with the
The source file or directory has the same name. For example, the following will create a soft link file for the file / bin/bzip2
/ bin/soft/bzip2 (use the same file name as the original file when the link name is not specified).
[root@localhost] # mkdir / bin/soft [root@localhost] # ln-s / bin/bzip2 / bin/soft/ [root@localhost] # ls-lh / bin/soft/bzip2 lrwxrwxrwx. 1 root root 6 02:10 / bin/soft/bzip2-> / bin/bzip2 copy, delete, move directories and files
Command: cp copy (Copy) file or directory
The cp command is used to copy a file or directory, recreate a copy of the file or directory (source) that needs to be copied and save it as a new file or directory (which can be saved to another directory). When you need to copy multiple files or directories, the target location must be a directory, and the target directory must already exist.
Format: cp [option] source file or directory destination file or directory
Common options:
-f: Force replication without reminding when overwriting the target file or directory with the same name. -I: remind the user to confirm when overwriting the target file or directory with the same name (Interactive, interactive). -p: keep (Preserve) the permissions, ownership and time stamp of the source file unchanged when copying. -r: you must use this option when copying directories, which means that all files and subdirectories are copied recursively.
For example, the following two actions will copy the / bin/touch command program to the current directory and name it mytouch;. Make an additional copy of the / etc/yum.conf file as a backup, add the. bak extension, and still store it in the / etc directory.
[root@localhost ~] # cp / bin/touch./mytouch [root@localhost ~] # cp / etc/yum.conf / etc/yum.conf.bak
If the data you need to copy includes a complete directory, you need to combine the "- r" option to execute successfully, otherwise the directory will be ignored. For example, the following action will copy the directory / boot/grub, file / etc/host.conf to the public_html folder under the current directory.
[root@localhost ~] # cp-r / boot/grub/ / etc/host.conf public_html/ [root@localhost ~] # ls public_html/ grub host.conf
Command: rm delete (Remove) file or directory
The rm command is used to delete specified files or directories. In the Linux command line interface, deleted files are difficult to recover, so you need to be very careful when using the rm command to delete files. The rm command takes the name of the file or directory to delete as a parameter.
Several common options are as follows (the meaning of the corresponding option of the cp command is basically similar):
-f: delete files or directories without reminder, but force deletion directly. -I: remind the user to confirm when deleting a file or directory. -r: this option must be used when deleting a directory, which means that the entire directory tree is recursively deleted (should be used with caution)
Data that has been determined to be no longer used (including directories, files) is usually deleted directly with the "- rf" option without prompting. For example, to delete the grub directory tree that has just been copied to the public_html directory without prompting the user for confirmation (direct deletion), you can execute the "rm-rf public_html/grub/" command as follows:
[root@localhost ~] # rm-rf public_html/grub/ [root@localhost ~] # ls public_html/ host.conf
If you need to confirm before performing the delete operation, you can use the "- I" option (do not use the-f option at the same time). The rm command prompts the user for each file or directory to be deleted, and you need to enter y (for deletion) or n (for no deletion) for confirmation. For example, the following action will delete the host.conf file in the public_html directory by prompting for confirmation (enter y to confirm deletion based on the prompt).
[root@localhost ~] # rm-I public_html/host.conf rm: do you want to delete ordinary file "public_html/host.conf"? Y
Command: mv move (Move) file or directory
The mv command is used to transfer the specified file or directory location, and if the destination location is the same as the source location, the effect is equivalent to renaming the file or directory.
Format: mv [option] source file or directory destination file or directory
When you need to move multiple files or directories, the target location must be a directory, and the target directory must already exist. If you move a file or directory in the same directory, it is equivalent to a rename operation. For example, the following action will rename the mytouch program file in the current directory to mkfile.
[root@localhost ~] # mv mytouch mkfile [root@localhost ~] # ls-lh mytouch mkfile ls: unable to access mytouch: there is no file or directory-rwxr-xr-x. 1 root root 62K April 11 10:42 mkfile
If you move a file or directory to an existing folder, you can specify only the destination folder location. For example, the following action will move the mkfile file to the public_html directory (which already exists) and the file name will still be mkfile.
[root@localhost] # mv mkfile public_html/ [root@localhost] # ls-l public_html/mkfile-rwxr-xr-x. 1 root root 62488 April 11 10:42 public_html/mkfile looks for directories and files
Command: which looks for the directory where the command file executed by the user is stored.
The which command is used to find and display the specific location of the Linux command program, and its search scope is mainly determined by the user's environment variable PATH (which can be viewed by executing the "echo $PATH" command), which is also the default search path of the Linux operating system when executing commands or programs.
The which command takes the command or program name you want to find as an argument. By default, the search will not continue when the first target is found. If you want to find it in all search paths, you can add the "- a" option. For example, after executing the "which ls" command, you can find a command program file named ls located in / usr/bin/ls, as follows:
[root@localhost ~] # echo $PATH / usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@localhost ~] # which ls alias ls='ls-color=auto' / usr/bin/ls
Note: because Shell built-in commands do not have a corresponding program file, when you use which to find internal commands, you will not find the corresponding program. For example, when you look up the location of the internal command history, you will be prompted that the program file is not in the search path, as follows:
[root@localhost ~] # which history / usr/bin/which: no history in (/ usr/local/sbin:/usr/local/bin:/usr/sbin:/usr / bin:/root/bin)
Command: find to find files or directories
Find command is a very powerful search command in Linux operating system, which can be finely searched according to different attributes such as name, type, size and so on. The find command is used recursively when looking up, and its use is quite flexible and complex. Only the most commonly used uses are introduced here.
Format: find [look in] [find conditional expression]
Among them, the search scope corresponds to the directory location in which the file or subdirectory is found (there can be multiple), and the search condition determines which attributes and characteristics the find command looks for. Several of the more common types of search criteria are described below.
Find by name: the keyword is "- name", based on the name of the target file, allowing the use of "*" and "?" wildcards. Search by file size: keyword is "- size", search according to the size of the target file, generally use "+" and "-" to set more or less than the specified size as the search condition. Commonly used units of capacity include kB, MB, and GB. Search by file owner: the keyword is "- user", depending on whether the file belongs to the target user. Search by file type: the keyword is "- type", which refers to ordinary file (f), directory (d), block device file (b), character device file (c), and so on. Block devices refer to devices that read data in blocks (such as hard disk, memory, etc.), while character devices refer to devices that read data by a single character (such as keyboard, mouse, etc.).
When setting the search criteria for the find command, if you need to use "*" or "?" wildcards, it is best to enclose the file name in double quotation marks to prevent qualified files in the current directory from interfering with the search results. For example, the following action recursively looks for files whose names begin with "resol" and end with ".conf" in the / etc catalog.
[root@localhost ~] # find / etc-name "resol*.conf" / etc/resolv.conf
Use the "- type" search criteria to filter out files of a specified type. For example, the following action finds all folders (corresponding to type d) in the / boot directory and ignores other types of files.
[root@localhost ~] # find / boot-type d / boot/ boot/grub
When you need to use multiple lookup criteria at the same time, you can use the logical operators "- a" and "- o" between expressions to denote and (and), or (or), respectively. For example, the following two operations use two lookup criteria, "more than 1024kB" and "name begins with vmlinuz", but use "- a"- o" to combine two conditions, the former indicating that both conditions must be met at the same time, and the latter indicating that only either of these conditions needs to be met.
[root@localhost ~] # find / boot-size + 1024k-a-name "vmlinuz*" / boot/vmlinuz-3.10.0-514.el7.x86_64 / boot/vmlinuz-0-rescue-8d265977c0c0459d98aff332d2544503 [root@localhost ~] # find / boot-size + 1024k-o-name "vmlinuz*" / boot/grub2/fonts/unicode.pf2 / boot/System.map-3.10.0-514.el7.x86_64 / boot/vmlinuz-3.10.0-514.el7.x86_64 Linux directory structure
The following will explain some directory, file-related commands, before explaining the directory structure of Linux to facilitate your understanding. The Linux operating system uses a slash "/" to separate the location of a file or directory (different from the backslash "\" in the Windows operating system). In the whole tree structure, the root directory is represented by a separate "/". The root directory is the starting point for Linux to operate the file system, and its partition is called the root partition. Under the root directory, the Linux operating system will establish some special subdirectories by default, each for different purposes. The following is a brief introduction to the common subdirectories and their functions.
/ boot: this directory is not only the directory where the system kernel is stored, but also the directory where the files needed to boot the system, such as vmlinuz and initrd.img, are stored. When you install Linux, create a partition for the boot directory to facilitate backup of the system. / bin:bin is the abbreviation of binary. This directory stores commands that can be executed by all users and are frequently used, such as cp, ls, and so on. / dev: this directory stores the interface device files, such as / dev/hda1, / dev/cdrom, etc. / etc: this directory holds files about system settings and management. / home: the default working folder (host directory, home directory) where all ordinary system users are stored, such as the host directory corresponding to the user account "teacher" is located in "/ home/teacher/". If the server needs to be provided to a large number of ordinary users, it is recommended that the "/ home" directory be divided into separate partitions to facilitate the backup of user data. / root: this directory is the host directory for the Linux operating system administrator (superuser) root, and by default only the root user's host directory is in the root directory and not in the "/ home" directory. / sbin: store the most basic management commands in the Linux operating system, and the general administrator user has the right to execute them. / usr: stores other user applications, usually divided into subdirectories for different types of applications. / var: store some files that often need to change in the system, such as system log files, user mailbox directories, etc. In practical application systems, the "/ var" directory is usually divided into separate partitions. The above list is only the subdirectories frequently used by users in the Linux operating system, and there are other subdirectories that users need to get familiar with in the process of using the Linux operating system. View the contents of the file
Command: cat displays and connects the contents of (Concatenate) files
The cat command is originally used to Concatenate the contents of multiple files, but it is more often used to view the contents of files in practice. The cat command is the most widely used command for viewing file contents. When using this command, you only need to take the document path you want to check as a parameter. For example, you can look at the contents of the / etc/sysconfig/network-scripts/ifcfg-ens33 configuration file and learn about the first piece of Ethernet by doing the following
Configuration information for the card.
[root@localhost ~] # cat / etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=noIPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=918487c2-e150-422b-acdd-8b7b61d0239b DEVICE=ens33 ONBOOT=yes IPADDR=192.168.10.114 NETMASK=255.255.255.0 GATEWAY=192.168.10.1
If you need to view the contents of multiple files at the same time, you can add multiple file paths as viewing objects. For example, the following actions will show the contents of the / etc/redhat-release file, which records the release information of the CnetOS series, and / proc/version, which records the system kernel and development environment, time, and so on.
[root@localhost ~] # cat / etc/redhat-release / proc/version CentOS Linux release 7.3.1611 (Core) Linux version 3.10.0-514.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)) # 1 SMP Tue Nov 22 16:42:41 UTC 2016
Command: more and less paging to view the contents of the file
Using the cat command, it is very simple to display the contents of the entire file directly, but when there are more contents in the file, you will probably only see the last part of the information, and most of the contents in front of the file will not have time to see. The more and less commands display the file in full-screen pagination, making it easy for us to read the contents of the file carefully from beginning to end.
The more command is an earlier paging display command, indicating that there is more (more) in the file content, and the less command is a later paging display command that provides more extended functions than the earlier more command. The usage of the two commands is basically the same.
When you use the more command to view the contents of a file with more than one screen, it will be split up and the percentage currently contained in the entire file will be displayed in the lower left corner. In the reading interface, you can press Enter key to scroll down line by line, press Space key to flip down one screen, press b key to flip up one screen, press Q key to exit and return to the original command environment. For example, the following operation will allow you to view the contents of the / etc/httpd/ conf/httpd.conf (site profile) file on a split screen.
[root@localhost ~] # more / etc/httpd/conf/httpd.conf # # This is the main Apache HTTP server configuration file. It contains the # configuration directives that give the server its instructions. # See for detailed information. # In particular, see # # for a discussion of each configuration directive. More-- (1%)
In addition to viewing the contents of the file on a split-screen basis, the more command can also view the output information when the command is executed on a split-screen basis with the pipe symbol "|", which is especially useful when there is a lot of output of the command. For example, the following actions can be paged to view the configuration file with the extension ".conf" in the / etc/ directory.
[root@localhost ~] # ls-lh / etc/*.conf | more-rw-r--r--. 1 root root 55 November 5 00:29 / etc/asound.conf-rw-r--r--. 1 root root 14K November 6 02:19 / etc/autofs.conf-rw-. 1 root root 232 November 6 02:19 / etc/autofs_ldap_auth.conf-rw-r--r--. 1 root root 22K November 6 01:31 / etc/brltty.conf. / / some contents are omitted-- more--
The method of using the less command is basically similar to that of the more command, but better than more, when the less command is combined with the pipe symbol "|" to view the information output during the execution of the command, you can turn the page down or up. The more command can only turn the page down, not up.
Command: head and tail view the beginning or end of the file
Head and tail are opposite commands, the former used to display part of the beginning of the file, and the latter to display part of the end of the file. You can use the "- n" option (n is the specific number of rows) to specify how many lines you want to display, and if you do not specify the number of rows, only ten lines are displayed by default. By executing the "head-4 / etc/passwd" command, you can view the first through fourth lines of the user account file / etc/passwd.
[root@localhost] # head-4 / etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin
The tail command, on the other hand, is used to view the contents at the end of the file. The tail command is usually used to view system logs (because newer log records are always added to the end of the file) to observe information about network access, service debugging, and so on. When used with the "- f" option, you can also track the dynamic updates of the contents at the end of the file, making it easy to monitor changes in the contents of the file in real time. For example, the following action can view the last ten lines of the system public log file / var/log/messages and trace the contents of the new records in the file at the end (press Ctrl+C to terminate).
[root@localhost] # tail-f / var/log/messages "" / / omit the display May 3 22:17:22 localhost PackageKit: uid 0 is trying to obtain org.freedesktop. Packagekit.system-sources-refresh auth (only_trusted:0) May 3 22:17:22 localhost PackageKit: uid 0 obtained auth for org.freedesktop. Packagekit.system-sources-refresh May 3 22:17:23 localhost kernel: TCP: lp registered statistics and retrieval of file contents
Command: wc statistics file content of the number of words (Word Count), the number of lines and other information
The wc command is used to count the number of lines, words, bytes and other information contained in the file content. Using the file name as a parameter, you can count multiple files at the same time.
Common options:
-c: counts the number of bytes in the contents of the file. -l: counts the number of lines in the contents of the file. -w: count the number of words in the contents of the file (separated by spaces or tab stops).
When the number of lines, words, or bytes of a file has a specific meaning, you can skillfully get some special information using the wc command. For example, if the user account data in the Linux operating system is saved in the / etc/passwd file, where each row of records corresponds to one user, the following operations can count the number of user accounts in the current Linux operating system, as shown below:
[root@localhost] # wc-l / etc/passwd 46 / etc/passwd
If you use the wc command with pipe symbols, you can also count the output of the command. For example, to count the total number of files with a ".conf" extension in the / etc/ directory, you can first find out the qualified file locations by using the "find / etc-name" * .conf "command. Since the output of the find command is also one file record per line, you only need to perform the" wc-l "operation with the pipe symbol to get the number of files that meet the criteria, as shown below:
[root@localhost ~] # find / etc-name "* .conf" | wc-l 432
When using the wc command without any options, by default, the specified file will be counted for the number of bytes, lines, and words (equivalent to using the "- c"- l"- w" option at the same time). For example, the following operation counts the contents of 2 lines, 10 words and 158 bytes in the / etc/hosts file, and then lists the contents of the file through the "cat / etc/hosts" command to check whether the statistical results are correct.
[root@localhost ~] # wc / etc/hosts 2 10158 / etc/hosts
Command: grep retrieves and filters file contents
The grep command is used to find and display lines containing the specified string in a file, either by directly specifying a key string as a search condition, or by using complex conditional expressions (for example, "^ word" begins with word, "word$" ends with word, and "^ $" represents a blank line).
Format: grep [options] to find conditional object files
Common options:
-I: ignore case (Ignore Case) when looking for content. -v: reverse lookup (Invert), that is, output lines that do not match the search criteria.
For example, by executing the "grep" ftp "/ etc/passwd" command, you can find the line containing the "ftp" string in the account file / etc/passwd, which actually outputs the information of the user account named ftp, as follows:
[root@localhost ~] # grep "ftp" / etc/passwd ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
In the process of maintaining the Linux operating system, we often encounter configuration files that contain a lot of content, which often contain a lot of blank lines and comment text that begins with "#". When you only need to analyze the valid configuration information, the existence of these blank lines and comment text is not conducive to fast browsing. Use the grep command to filter out this extraneous information. For example, the following action can display something other than the line starting with "#" and the blank line in the / etc/yum.conf file.
[root@localhost ~] # grep-v "^ #" / etc/yum.conf | grep-v "^ $" [main] cachedir=/var/cache/yum/$basearch/$releasever keepcache=0 debuglevel=2.. / / use the compression tool to back up and restore documents
Gzip and bzip2 are the two most commonly used compression tools in the Linux operating system, both of which can compress specified files or extract files that have already been compressed. The compression algorithms used by the two are different, but the format of the command is basically similar, and generally speaking, the compression efficiency of bzip2 is better.
Command: gzip and gunzip compression and decompression
Compressed files made using gzip have a default extension of ".gz". When making a compressed file, using the "- 9" option can increase the compression rate, but it will take more time when the file is larger. For example, the following action will compress the mkfile file in the current directory, resulting in a compressed file mkfile.gz (the original file mkfile is no longer retained), and the compressed text size becomes 30KB (62KB when uncompressed).
[root@localhost] # ls-lh mkfile*-rwxr-xr-x. 1 root root 62K May 4 01:32 mkfile [root@localhost] # gzip mkfile [root@localhost] # ls-lh mkfile*-rwxr-xr-x. 1 root root 30K May 4 01:32 mkfile.gz
When you need to extract gzip-compressed files, simply use the gzip command with the "- d" option, or use the gunzip command directly. For example, if you unzip the compressed file mkfile.gz, you can do the following.
[root@localhost ~] # gzip-d mkfile.gz or [root@localhost ~] # gunzip mkfile.gz
Command: bzip2 and bunzip2 compression and decompression
The use of the bzip2 and bunzip2 commands is basically the same as the gzip and gunzip commands, and compressed files made using bzip2 have a default extension of ".bz2". For example, the following action compresses the mkfile file in the current directory with a high compression ratio, resulting in a compressed file mkfile.bz2 (the original file mkfile is no longer retained), and the compressed file size is changed to 30KB (62KB when uncompressed).
[root@localhost] # ls-lh mkfile*-rwxr-xr-x. 1 root root 62K May 4 01:32 mkfile [root@localhost] # bzip2-9 mkfile [root@localhost] # ls-lh mkfile*-rwxr-xr-x. 1 root root 30K May 4 01:32 mkfile.bz2
Like the gzip and gunzip commands, when you need to extract files compressed by bzip2, simply use the bzip2 command with the "- d" option, or use the bunzip2 command directly. For example, to extract the compressed file mkfile.gz, you can do the following.
[root@localhost ~] # bzip2-d mkfile.bz2 or [root@localhost ~] # bunzip2 mkfile.bz2 use the tar archiving and release tool
The tar command is mainly used to archive directories and files. In actual backup work, the package files are usually compressed at the same time as the archive (you need to call the previous gzip or bzip2 command) to save disk space. When using the tar command, the "-" sign before the option can be omitted. Several common options are described below.
-c: create a package file in Create. Tar format -C: specify the target folder to release when unzipping. -f: indicates the use of archived files. -j: call the bzip2 program to compress or decompress. -p: keep the permissions of files and directories when packing. -P: keep the absolute path of files and directories when packing. -t: list to view the files in the package. -v: output details (Verbose). -x: unlock the package file in .tar format. -z: call gzip program to compress or decompress.
Make archived backup files
Basic format: tar [option] Archive and compress the name of the source file or directory to be archived
If you need to compress the produced archive files, you can use the "- z" or "- j" options to automatically call the compression tool (corresponding to the gzip and bzip2 command programs, respectively) for compression. For example, the following action will back up the / etc and / boot directories and generate an archive package called sysfile.tar.gz under the current directory, and you can see the list of archived files during execution. It should be noted that when backing up a recovery directory or file, it is best to change the work path to the path of the target directory or file, and then use the relative path when executing the backup command to prevent damage to the important files in the root directory "/" caused by manual error when entering the command.
[root@localhost ~] # cd / [root@localhost /] # tar zcvf sysfile.tar.gz etc boot etc/ etc/login.defs etc/cron.hourly/ etc/fstab "" / / omit part [root@localhost /] # ls-lh sysfile.tar.gz-rw-r--r--. 1 root root 125m April 10 11:46 sysfile.tar.gz
If you need to make an archive package in ".tar.bz2" format, change the "- z" option to "- j" option. For example, the following action will back up the / etc/passwd and / etc/shadow files and generate an archive package called usershome.tar.bz2 in the / tmp directory.
[root@localhost ~] # cd / etc [root@localhost etc] # tar jcvf / tmp/usershome.tar.bz2 passwd shadow etc/passwd etc/shadow [root@localhost etc] # ls-lh / tmp/usershome.tar.bz2-rw-r--r--. 1 root root 2.2K April 10 11:50 / tmp/usershome.tar.bz2
Recover data from an archive file
Basic format: tar [option] Archive and compress file name [- C target directory]
Similarly, when recovering data from an archive package in ".tar.gz" format, you need to invoke the compression tool automatically with the "- z" option, while for ".tar.bz2" format archive package, it corresponds to the "- j" option. By default, the recovered data is released to the current directory, and if you need to restore to the specified folder, you also need to combine the "- C" option to specify the target directory. For example, the following action will restore the data from the backup file usershome.tar.bz2 and release it to the / etc directory (which will overwrite the existing file).
[root@localhost] # tar jxf / tmp/usershome.tar.bz2-C / etc
In most backup and recovery work, only using the tar command can work well with gzip and bzip2 tools, and there is no need to execute gzip or bzip2 commands. Note, however, that the command programs for these two compression tools must exist.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.