Pick a non-996 weekend and fix Linux. Follow me!

Original: Little sister taste (wechat public ID: XJjdog), welcome to share, reprint, please keep the source.

This is a detailed, hand-held tutorial.

There are many students wechat asked me, Linux commands so many, how to master from entry? Actually, it’s very simple, and this article will fly you through it. The article is very long, to pick a sunny sunshine weekend, patience to get it done.

This article begins with an overview of Linux commands, illustrating the various aspects of Linux commands. If you’re still confused after reading this section, it’s time to follow this tutorial, which covers how to pick a Linux distribution, how to install a Linux system, and how to learn Linux commands by following the topic.

Start learning: Tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap, tap. Among them, there are also some difficult to bite the bone, pay attention to the little sister taste wechat public number, we together with sharp teeth, to chew it.

1. Overview of Linux commands

This part is for those who have a little Linux experience, if you are a beginner, please skip this part and go to part 2.

1.1 Directory Operations

In the work, most often dealing with the operation of directories and files. Linux provides corresponding commands to operate on it, and abstracts and abbreviates these commands.

1.1.1 Basic Operations

It may be that these commands are used so often that typing even one more character is a sin. So they’re short enough to be counted with a scissors hand without using Arabic numbers.

See the command. Mkdir Create a directory make dir cp Copy files copy mv Move files move rm Delete files remove

Example:

# create directory and parent directory a,b,c,d
mkdir -p a/b/c/d

# Copy folder a to/TMP
cp -rvf a/ /tmp/

# Move file a to/TMP and rename it b
mv -vf a /tmp/b

Delete all files on the machine
rm -rvf /
Copy the code

1.1.2 roaming

Linux is a dark command line, still faced with life’s three questions: who am I? Where I am? Where am I going?

Using the ls command, you can view all the contents of the current directory. Ls-l can see more information and determine who you are. Using the PWD command, you can view the directory where the current terminal resides. Tell you where you are. CD If you go to the wrong directory, the CD command switches to the right directory. Find The find command filters some conditions to find files that have been forgotten.

As to where to go, it may be the will of the master.

1.2 Text Processing

It’s a very, very plus skill. Once you get it, you can also save more time studying object orientation. Little Sister Flavor has exported the “Most Commonly used Vim, SED, AWk Techniques series”.

1.2.1 Viewing files

The most common cat command is the cat command. Note that if the file is large, the output of the cat command will be frantically printed on the terminal, which can be terminated by pressing CTRL + C several times.

Du -h file du -h file cat fileCopy the code

Less Since cat has this problem, for larger files, we can use less to open a file. Like Vim, less can enter search mode after entering /, and then press n(n) down (up) to find. There are a number of operations that are similar to Vim, so you can look at the analogy.

Tail Most of you who do server-side development know this command. For example, look at the nginx scrolling log.

tail -f access.log
Copy the code

The tail command is used to statically view the last N lines of a file, and the corresponding head command is used to view the n lines of the file header. But the head has no rolling function, just like the tail is growing out, not backward.

tail -n100 access.log
head -n100 access.log
Copy the code

1.2.1 statistics

Sort and UNIq are often used together. Sort can use -t to specify the delimiter and -k to specify the columns to sort.

The following command outputs the IP of the nginx log and the PV of each IP, the top 10 pv

# The 2019-06-26 T10:01:57 + 08:00 | nginx001. Server. Ops. Pro. Dc | 100.116.222.80 | 10.31.150.232:41021 | | | | 0.000 0.011 0.014 200 | 200 | 273 | | / visit | sign = 91 cd1988ce8b313b8a0454a4bbe930df | | - | - | HTTP POST | 112.4.238.213

awk -F"|" '{print $3}' access.log | sort | uniq -c | sort -nk1 -r | head -n10
Copy the code

1.2.3 other

Grep Grep is used to filter the content. With the –color parameter, the color can be printed on supported terminals. With the parameter n, the number of lines can be displayed for quick positioning. For example, check the NGINx log for POST requests.

grep -rn --color POST access.log
Copy the code

It is recommended to use this parameter every time.

If I want to see the context of an exception, I can use the ABC parameter. They are abbreviations of several words and are often used. A after B before C? N lines before and after the content would look like this:

grep -rn --color Exception -A10 -B2   error.log
Copy the code

diff

Using the diff command, you can compare the difference between two files. Of course, this functionality is provided in the IDE, and diff is just the original tradeoff on the command line. Diff and Patch are also ways to patch some platform source code. If you don’t use diff and Patch, pass.

1.3 compressed

In order to reduce the size of the transferred files, compression is usually turned on. Linux common compressed files are tar, bzip2, zip, rar, and so on. 7z is relatively less used.

Tar Use the tar command to compress or decompress. Bz2 Use the bzip2 command to operate. Gz Use the gzip command to operate

The most common is the.tar.gz file format. It is tar packed and then gzip compressed.

Creating a compressed file

tar cvfz  archive.tar.gz dir/
Copy the code

Unpack the

tar xvfz. archive.tar.gz
Copy the code

Go and find out what the relationship is.

1.4 Routine Operation and maintenance

Boot is to press the start button, shutdown is not long press the start button. Yes, it is shutdown command, but generally also do not have permission -.-! . The passwd command is used to change the password, but this permission is still available.

mount

The mount command can be used to mount some external devices, such as USB flash drives, ISO files, and newly applied SSDS. It’s safe to watch a little movie.

mount /dev/sdb1 /xiaodianying
Copy the code

The chown chown command is used to change the owning user and owning group of a file. Chmod is used to change the access permission of a file.

Both commands are related to the Linux file permission 777. Example:

# Destructive command
chmod 000 -R /

# change user and group from a to XJJ
chown -R xjj:xjj a

Add execute permission to a.sh file (this is too common)
chmod a+x a.sh
Copy the code

If you are using centos, the package management tool is yum. If your system does not have the wget command, use the following command to install.

yum install wget -y
Copy the code

Of course, centos also has some techniques for managing backend services. The service command is. Systemctl is compatible with the service command. Let’s see how to restart the mysql service. I recommend this one.

service mysql restart
systemctl restart  mysqld 
Copy the code

For a normal process, you can use the kill command for more detailed control. The kill command has a lot of signals, so if you’re using kill-9, you want to know the difference between kill-15 and kill-3.

Su The su command is used to switch users. For example, if you are root and want to do something with the XJJ user, you can su.

su xjj
su - xjj
Copy the code

– Can let you clean and pure advent of another account, no accident, recommendation.

1.5 System Status Overview

Log into a Linux machine, and there are several commands that can help you quickly find problems. These commands cover memory, CPU, network, IO, disk, and so on.

Uname The uname command outputs the current kernel information, giving you an idea of what machine you are using.

uname -a
Copy the code

Ps The ps command displays the process/thread status. Top, top, top, top

# Find the Java process
ps -ef|grep java
Copy the code

Top Displays the system status. CPU Load Indicates the load and CPU usage. Processes that use the highest amount of memory or CPU. The following command displays the thread status in a process.

top -H -p pid
Copy the code

Free Top can also look at memory, but not in a friendly way. Free is dedicated to looking at memory. This includes physical memory and virtual memory swap.

Df Using the df command, you can view the disk usage in the system and check whether the disk usage has reached the upper limit. The parameter H can be presented in a friendly manner.

df -h
Copy the code

Ifconfig displays the IP address. The replacement is the IP addr command.

Ping You can use ping to detect whether the network is connected. (Excluding sites that ban ping)

Netstat although the ss command can replace netstat, netstat is still used more widely in reality. For example, view all current TCP connections.

netstat -ant
Copy the code

This command is useful in finding out what port is being used locally.

1.6 Common Tasks

There are also some commands that are often used at work, and they appear very frequently. These are familiar faces.

Export For those of you who have JDK installed and can’t find Java commands, export will do that for you. The export command is used to set some environment variables. The env command displays all environment variables in the current system. For example, the following setup is the JDK.

export PATH=$PATH:/home/xjj/jdk/bin
Copy the code

Sometimes you want to know the exact path of the command being executed. You can then use the whereis command, assuming you have multiple versions of the JDK installed.

Crontab This is the Linux native job tool. It’s not distributed. If you’re not operating, you’re not using it. For example, drinking tea and going to the bathroom every 10 minutes.

*/10 * * * * /home/xjj/wc10min
Copy the code

Using the date date command, you can output the current system time. You can use the -s parameter to specify the output format. But setting the time involves setting up the hardware, so there is another command called hwclock.

Xargs Xargs reads the input source and processes it line by line. This command is very useful. For example, delete all class files in a directory.

find . | grep .class$ | xargs rm -rvf

Copy all RMVB files to directory
ls *.rmvb | xargs -n1 -i cp {} /mount/xiaodianying
Copy the code

1.7 the network

Linux is a multi-job network operating system, so there are many, many network commands. At work, you deal with them most often.

SSH, I won’t go into this. You’ll want to know what an SSH tunnel is. If you want a detailed output, use the -v parameter.

SCP The SCP is used for file transfer. It can also be used to transfer directories. There are also more advanced SFTP commands.

SCP a.txt 192.168.0.12:/ TMP /a.txt SCP -r a_dir 192.168.0.12:/ TMP /Copy the code

Wget If you want to install JDK on the server, you don’t download it locally and then use SCP to upload it to the server (sometimes you have to). The wget command lets you download files directly from the command line and supports breakpoint continuation.

wget -c http://oracle.fuck/jdk2019.bin
Copy the code

Mysql is widely used and not everyone has access to Navicat. You need to know how mysql connects and basic operations to be able to handle exceptions.

Mysql -u root -p -h 192.168.1.2Copy the code

Don’t feel complicated, the command is limited, but the passion is infinite; Will not be proud, a Vim is enough to toss about a lifetime. The shortcut is to sum up, deep only to explore. White horse gap, will eventually flow, easy to catch. Things change, you get old easily. Only time, will not live up to.

2. Pick a Linux distribution

Linux is similar to Unix, but if you’re in your 20s or 30s, you’re exposed to the world of Linux. Linux is everywhere, from mobile phones to centos, which is widely used on servers, to the slick desktop distribution Ubuntu, and even the worldwide popularity of Raspberry PI.

2.1 You need to know this Linux history

Knowing a little about the history of the operating system can edify sentiment. GNU/Linux is to resist the monopoly of some commercial companies and developed, condensation of a generation of Internet people yearning for freedom.

Linux is actually quite young compared to other UniXes. It wasn’t until 1991 that a young Finnish man named Linus Torvalds started developing the Linux kernel as we know it today.

The mascot for Linux is penguin, which wasn’t decided on until 1996, so you’ll often see some funny pictures. If you were born in the 1990s, this little penguin is almost your age and still a young guy.

Linux has a more complicated history. Linux has come a long way from where it is today. For more information on its history, you can check out the link below to see hd images. Twenty years is a very long time for the software industry, and how much of the scenery has changed.

For high definition, see image (1T. click/aUnx). As you can see, Linux takes a tiny slice of that. It is like the appearance of man, a small but qualitative leap in the long course of life.

You may have noticed that in the previous description, GUN/Linux was mentioned, not just Linux. Linux itself is only a kernel, limited role, only combined with GNU, with a complete ecosystem will play its role.

The reason for the above distinction is to remember the GNU Project initiated by Richard Stallman in 1983. He was also the inventor of The Smalltalk language, recognized as the second object-oriented language. I worked on it for a while in the early years. Oh, and he wrote a big MAC editor, Emacs.

Only when a man is made a god can he have the power to torment you.

We won’t go into much of the history of Linux. Here are a few classic distributions.

2.2 Selected versions

There are thousands of Linux distributions out there, and you can make one if you like and if you have enough money. How to find the most suitable version in this, is a need to go through a toss. Many distributions are actually very niche.

It’s not like something in philosophy where the truth is in the hands of a few. Only a well-developed and recognized Linux distribution has its value, which is downright pragmatic.

But this thing is like a girlfriend, just beginning to feel different style, each has its own advantages and shortcomings, to finally understand that is the same vulgar. But some people just love working on Linux for the rest of their lives…

I can tell you a little bit about my own journey. At the beginning, I was in contact with redhat, and there was no enterprise edition at that time. After a while, it switched to the more stable Slackware. But the program updates on Slackware were too slow, so we switched to ReadHat-bred Fedora, which kept the software fresh. In the meantime, several other Linux versions were tried, and finally, around 2013, it was switched to the rolling archLinux, where it remains today.

For personal (technical) users, use Ubuntu => ArchLinux for the desktop. 2. Enterprise users, servers, use centos.

2.3 Main Origins

With so many Linux versions, there are actually two main lines. Debian series and redhat series. Many distributions, in fact, are reworks, and many take the two basic series directly. Is the so-called: thousands of operating systems, are hats and shit.

debian

This shite chart below is Debian. Uh, uh, uh, it’s just one letter away from shit.

Debian planIs a cooperative organization dedicated to creating a free operating system. It is characterized by stability and security and has been developed for more than 20 years now. Ubuntu, as we know it, is an improvement on Debian.

redhat

Red Hat is a commercial company that got into Linux early and now offers some certificates like Red Hat certification to individuals. Today’s cloud hosts, which use more centos, including Red Hat’s RHEL, account for most of the server market. Recently, centos 8 released a rolling version of centos Stream, which looks more like a normal operating system.

2.4 Typical Version

Let’s look at a few typical versions at different levels. On the application side, Linux has desktop, server, research, etc.

Against 2.4.1, ubuntu

The emergence of Ubuntu has contributed indelibly to the promotion of Linux. It is an easy to install desktop version (there is also a server version) with a very nice interface. Ubuntu is based on the Unstable branch of Debian. The package management software is apt-get.

It was founded by Mark Shuttleworth, a South African entrepreneur and the world’s second self-funded space tourist. I think, whether it’s space or Ubuntu, it’s a dream.

2.4.2, centos

Centos is currently the most popular server version. It is the product of a recompilation of RHEL source code, primarily to circumvent some legal issues. In terms of package management and even stability, it is no different from Red Hat Enterprise edition.

2.4.3, archlinux

Archlinux is distributed in a rolling upgrade mode, doing everything possible to provide the latest stable release. At first install, ARCH only provides a basic system and doesn’t even have an interface, so it’s not very beginner friendly.

However, ArchLinux is a very clean system. A lot of software is only installed when you need it. Its software and ideas are often up to date and highly customizable, which is a favorite of many Linux enthusiasts.

2.4.4, gentoo,

Archlinux, above, provides a compiled package. When installing software, users only need to download and decompress the software. Gentoo takes this process one step further, and arguably more perverted. It downloads the source code of the software, compiles it locally, and installs it.

This is usually a pain in the ass because it takes a long time to download and compile, but it has the great advantage of being stable.

This system is lower level, requires more skill and is not highly recommended.

2.4.5 、LFS

LFS stands for “Linux from Scratch,” which means to build a Linux system from zero. It has a very detailed installation documentation that shows you how to build the kernel, build the boot program, and compile and configure the necessary software.

It’s a crazy and necessary process. If you want to take your Linux to the next level, it’s a great benefit to follow the documentation. You need to cross compile a lot, and eventually use the chroot command to switch to the new system for the rest of the operation.

Want to make your own distribution? Let’s start here.

2.4.6, kali

Kali Linux is a very professional distribution. If you are doing infiltration work, it is a very good choice.

The installation package for the distribution is very large and contains the common cracking tools, penetration tools, and attack tools. This is very dangerous, I have used it to brute force crack a lot of wifi passwords, successfully peep into the privacy of neighbors. It’s very useful.

3. Install a clean Linux system

He that would do a good job must sharpen his tools. You might want to buy a cloud host to practice, but that will cost you money. We can install one ourselves. As mentioned above, the most widely used is centos. Whether you build your own machine room or use a cloud environment such as Aliyun, most will provide centos installation.

You may find a variety of ways to install your virtual machine. In this section, we will prepare a pure environment using virtual dual network cards. There are a lot of pictures in this section.

In Aliyun, for example, the first digit is CentOS by default, providing multiple images from version 7.6 to older versions.

3.1 download

In the following article, we will use CentOS 7 as the base environment. Centos is popular, so there are many mirrors. Domestic, we make big download from Shanghai, the speed should be a few quicker.

http://ftp.sjtu.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1908.iso
Copy the code

If the day does not maintain. You can find it here:

http://centos.mirror.ndchost.com/7/isos/x86_64/CentOS-7-x86_64-Minimal-1908.iso
Copy the code

To help you learn more, we use a minimal system ISO. The minimal ISO is less than 1GB, while a DVD with a lot of software preloaded is 4.3GB. We’ll use this diet version.

Installing Linux 3.2

The easiest way to quickly learn and experience Linux is to install it on a virtual machine. At present, the most popular virtual machine, one is VMware, one is VirtualBox. There is also a Parallels Desktop on MacOS.

VirtualBox, which is free and cross-platform, meets our needs. Next, we’ll walk you through the installation step by step.

(1) Click New to start the installation journey.

(2) Fill in the name and version. Then click Continue.

(3) According to your machine configuration, select memory

My machine has 8GB of memory, so I allocated 2GB to the virtual machine, which is more than enough.

(4) Create a virtual disk

When you click Continue, a dialog box will pop up. Let’s just keep clicking until the dialog disappears. It’s very rough.

(5) Next, click Settings.

(6) Switch to the Storage option and select the ISO we downloaded

(7) Click Start to start the installation.

Use the arrow keys toggle to highlight Install CentOS 7. Click OK to start the installation.

(8) An installation window is displayed

The next step is a bit much, and if we don’t have a special introduction, then continue is ok.

(9) Next, configure the disk partition

Keep the default and press the Done button to exit.

(10) Configure users

On Linux, the default user name is root. Next we set the root user’s password to 123456. Since this is a weak password, you need to click twice to exit.

(11) Wait until the installation is complete and restart

(12) The installation is successful

3.3 networking

At this time, we installed the virtual machine, can not be connected to the Internet, can not convey their ideas out. Since we did not set anything for the virtual machine, the default NAT mode was used.

Focus the cursor on the command line window and enter the command dhclient. Wait for a few seconds and run the ping Baidu.com command to test the following networks. The network can be accessed.

The dark window above is our current Linux interface. Some people feel very ugly, like playing DOS, but like me this kind of hopeless people, but feel particularly kind.

For the following commands, instead of taking screenshots, we use highlighted code blocks. In order not to confuse people, please take a look at the following picture.

3.4 External Access to VMS

Due to the NAT mode, our VMS can access the Internet, but cannot be discovered by external users. A good wine fears a deep alley. To solve this problem, we need to add another network card.

Before you can make these changes, you need to shut down the virtual machine first. You can force the machine to shut down, or you can type:

shutdown -h now
Copy the code

After the virtual machine is shut down, click Settings again to switch to the Network Adapters TAB. Add a new host-only Adapter as shown in the figure. Through this network card, our host machine can access it.

Start the VM again, run the dhclient command, and run IP addr to view the IP address of the host. As you can see, we now have two network cards, two IP addresses.

Record the network address starting with 192. We will use external programs such as XShell, SecureCRT, etc., to connect. For example, my IP address here is 192.168.99.100. No nonsense, look at the picture.

Tip: The network segment of the virtual NIC is different. You can change it to the same as mine in the global Settings.

3.5 Remote Connection

As you may have experienced, typing through the virtual machine’s own command-line interface is extremely limited. Through a remote connection, the terminal interface can be switched to the familiar operation mode, if you can display the color of the terminal, that would be great. Here are a few tools, generally, using Xshell in the majority.

Windows

  • You’ve probably seen your SRE colleagues in the workplace, their fingers flying, their command characters flying across the screen. Even complicated, hard-to-remember passwords can be typed instantly. He’s probably using Xshell.

  • SecureCRT is an older product that is also used more frequently.

  • MobaXterm MobaXterm is a single file pure green software, download the exe package to run directly, do not need any installation process.

There are free and professional versions. If you can’t buy it, look for a cracked version. However, be aware that there are many times when people with ulteriormotives will plant Trojan horses in the software to steal your passwords and certificates.

MacOS

For MacOS users, it’s easy. Use iTerm directly and enter the command line. For example, use the following command to connect our machine.

SSH [email protected]Copy the code

Linux

Well, you are already a Linux environment, what is the virtual machine? Just use it.

You are advised to use tools such as XShell, SecureCRT, and iTerm for remote connection over SSH. For some command copy, verification, more convenient and fast.

4. Have a preliminary understanding of Linux command line

All things are difficult before they are easy. Take the first step in a dark Linux window. Don’t be afraid to type something wrong, the system is very robust. The command line is often more efficient than a graphical interface, and more importantly, it can be used to automate widgets, which makes a huge leap in productivity.

You have now installed centos and connected to it remotely. We have it, but we don’t know its temper. Next, let’s enter the world of the Linux command line. Sign a contract with me, lad.

This section takes a look at how a command is generated and executed, using a very detailed evolutionary approach.

4.1. Simple try

All right, we’re in the terminal now. What is a terminal? The dark interface that you see in a lot of hacker movies is that it provides an interactive interface where you can input strings, and the flashing, scanner-like stuff, it doesn’t exist.

Try typing something in. For example, JDSJF.

[root@localhost ~]# jdsjf
-bash: jdsjf: command not found
Copy the code

Let’s put this picture up again. What’s going on? Command output translated into Chinese means “command not found”. What is an order? It’s the arbitrary string that we typed up here, JDSJF.

Then, let’s look at some other useful things in the hints.

Left left left left left left down down down down down

Bash stands for the shell we use, which can be thought of as an interpreter that interprets our input into a series of executable instructions. The bash interpreter is one of the most popular Linux distributions today, and almost every one comes pre-installed with it.

Command not found, proving that our string bash cannot be interpreted. However, there are certain directories on Linux that have files that can be found by default, and the set of these directories is called PATH. PATH is also an environment variable that we can command to see what it looks like.

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Copy the code

To find out what commands are on the system, look at what files are in the folders above. There are lots and lots of files, but most of them we don’t touch. That’s why XJjDog writes this — focusing on the most commonly used, most useful commands, most commonly used arguments, and most useful scenarios.

After the command output, there’s something extra, like [root@localhost ~]. This part is called the prompt, and the cursor keeps bouncing around, waiting for your input. This part is customizable, even beautifully.

4.2, Hello World

So far, we’ve got nothing. From the programmer’s point of view, you need to implement a Hello World program. In a terminal shell, this process becomes much simpler than writing a Java program.

[root@localhost ~]# echo "Hello World"
Hello World
Copy the code

As you can see above, echo means to output something. The Hello World that follows is called an argument, separated by a space, and can take multiple arguments.

[root@localhost ~]# echo "Hello World" , "Fuck 996"
Hello World , Fuck 996
Copy the code

The above command works properly, which proves that echo is a command that our terminal can recognize. So where exactly is this order? You can find it using the whereis command.

[root@localhost ~]# whereis echo
echo: /usr/bin/echo /usr/share/man/man1/echo.1.gz
Copy the code

Command display. The full PATH of our echo command, /usr/bin/echo, is recognized because it is in the PATH directory.

4.3 Add the command to PATH

Next, let’s put the above command into a script. And then put the script in the PATH directory. But first, we need to give the order a name.

First you need to create a file. On Linux, create files using the touch command.

[root@localhost ~]# touch jdsjf
Copy the code

After the command is executed, nothing happens, it just creates an empty file. Next, let’s add something to it.

[root@localhost ~]# echo "echo 'Hello World'" > jdsjf
Copy the code

Notice the symbol >, which means to redirect the preceding output to the following file. After executing the above command, the contents of JDSJF will become echo ‘Hello World ‘.

Next, we try to execute the command we just generated.

[root@localhost ~]# ./jdsjf
-bash: ./jdsjf : Permission denied
Copy the code

We execute the command we just generated using a relative path. As a result, the terminal displays that we do not have the permission to execute the command.

In fact, Linux is very detailed in this aspect of permission control. A file has three properties: readable, writable and executable. If you want a file to be executable, you need to give it execution permission, which is done with the chmod command.

[root@localhost ~]# chmod u+x jdsjf
[root@localhost ~]# ./jdsjf
Hello World
Copy the code

We will discuss permissions in more detail in a later section. As you can see above, the command is printed properly. Next, we move the command to a directory in PATH.

[root@localhost ~]# mv jdsjf /usr/local/bin/
[root@localhost ~]# jdsjf
Hello World
Copy the code

You don’t need to add any relative paths. Now, just type JDSJF and you can output a string of numbers normally. We’ve managed to get a string that doesn’t make any sense to express its idea. Although we are still the masters of it.

Here are three questions you can think about:

1. Can I customize a directory, such as /root/mybin, and add it to PATH?

2. Can I skip the touch command above and just use the redirection to generate the file?

3. Is there any other way to execute the command besides PATH and relative PATH?

5. Linux roaming mode

To understand the basic use of Linux, you need to understand the basic fact that everything on a Linux system is a file.

Commands, documents, even devices, directories, and sockets are treated the same on Linux. Many drivers will find themselves using functions that are identical to those used to read and write files (open, close, read, write, ioctl). Today’s basic operations are for ordinary files and directories, and this section explains the commands in detail.

5.1. Current path

So far, we don’t know where we are in the system. In the browser, we can know our coordinates on the Internet by the URL in the navigation bar. Similar functionality is provided by the PWD command, which outputs the current working directory.

The PWD command is a very, very common command, especially on machines where command prompt Settings are not very friendly. In addition, it is often used in shell scripts to determine whether the current running directory meets the requirements.

There are many online accidents caused by not confirming the current directory. For example, the rm -rf * command is dangerous. It is a good practice to always check the current directory when executing risky commands.

[root@localhost ~]# pwd
/root
Copy the code

After logging in as root by default, we stay in /root. The directory hierarchy in Linux is divided by /.

5.2 File System User Standards

Linux’s file system has had a specification from the beginning. The Filesystem Hierarchy Standard (FHS) is also a proper abbreviated noun. As FHS has evolved over the years, the directory structure has become clearer. In addition to some standard requirements, there are user conventions.

Next, let’s take a look at the default directory on Linux to get a basic feel for it.

Level 1 The second floor introduce
/bin Soft link to the /usr/bin directory
/sbin Soft link to the /usr/sbin directory
/lib Soft link to directory /usr/lib
/usr /bin Store some commonly used commands
/usr /sbin Stores some commands commonly used by administrators
/usr /lib Used to store dynamic libraries and some module files
/sys A visual interface to data structures in the kernel
/proc Memory image
/run Memory image
/boot Store the boot program, kernel related files
/dev For someDevice fileSuch as optical discs
/etc Used to store global and application configuration files
/var As with /var/run, it stores the files needed by the system to run, such as the pid of mysql, etc
/tmp Very special temporary folder, lost in power failure
/home / * * User directories, for example, my directory is /home/xjjdog
/root The home directory of user root
  • The most common directory we use is our user directory, where we can do anything, such as /root. Some of their own data, such as video, audio, downloaded files, or do some test data, you can plan in these directories. The root user is a special user. The private directory of common users is under /home.

  • The /etc etc directory is a common directory that contains global system configuration files and application configuration files. For example, if you have PHP installed, or nginx installed, their configuration files are in a folder in the /etc directory.

  • /var The var directory holds some running data, either required or not. After some hackers break in, they leave traces in some of these files, and they clean them up. The var directory is also the default location for some applications, such as mysql data files.

  • The/TMP directory is a special temporary directory where files disappear after a power failure. But this directory, which all users have access to, is usually used for file swapping.

  • The /proc and /sys directories are two magical directories. These are pseudo file systems, and you can control the behavior of a program by modifying the state and content of some of these files (which are flushed directly into memory, cool). In the beginning, there was only the proc directory, but because of the clutter, the SYS directory was planned to control some of the behavior of the kernel. If you are tuning some system parameters, you will spend a lot of time working with these files.

  • There are also several empty directories that we did not list on the table above. The/SRV directory, for example, usually contains web services such as nginx. However, this is not mandatory, so the/SRV directory I’ve seen is usually empty all the time. Similarly, the /opt directory exists, so you can ignore it. This all belongs to the category of user planning, customization is very strong.

  • When using A Linux system, you can also create your own directory. For example, I like to create a directory called /data to hold database-related content. For example, /data/mysql holds mariadb data, and /data/es/ holds elasticSearch index content.

There are many types of files on Linux, and most of them are stored in corresponding directories. For example, in /dev, there are some device files. Under the /bin file, some commands can be executed. It’s usually easy to remember.

5.3 Viewing the File list

So, the table above, how do I see it, by memory? The ls command is used to list information about files in related directories. Can be rated as Linux under the most industrious command pacesetter.

Now the terminal, can output color information, very intuitive. Oh-my-zsh and oh-my-bash can make your terminal more beautiful. Add it to your research list.

[root@localhost /]# ls /
# Note: ls accepts path arguments, so you can output information without first jumping
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@localhost /]# ls -l /
Use the -l parameter to see more detailed information.total 20 lrwxrwxrwx. 1 root root 7 Nov 3 20:24 bin -> usr/bin dr-xr-xr-x. 5 root root 4096 Nov 3 20:34 boot drwxr-xr-x. 19 root root 3080 Nov 3 21:19 dev drwxr-xr-x. 74 root root 8192 Nov 3 20:34 etc drwxr-xr-x. 2 root root 6 Apr 11 2018 home lrwxrwxrwx. 1 root root 7 Nov 3 20:24 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Nov 3 20:24 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 Apr 11 2018 media drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt drwxr-xr-x. 2 root root 6 Apr 11 2018 opt dr-xr-xr-x. 108 root root 0 Nov 3 21:19 proc dr-xr-x---. 2 root root 135 Nov 4 07:53 root drwxr-xr-x. 24 root root 740 Nov 3 21:20 run lrwxrwxrwx. 1 root root 8 Nov 3 20:24 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Apr 11 2018 srv dr-xr-xr-x. 13 root root 0 Nov 3 21:19 sys drwxrwxrwt. 9 root root 4096 Nov 4 03:40 tmp drwxr-xr-x. 13 root root 155  Nov 3 20:24 usr drwxr-xr-x. 19 root root 267 Nov 3 20:34 varCopy the code

The most common way to use ls is to add the parameter L or the parameter A.

5.3.1 Details

Add parameter L, you can see some file permission information has been updated date and so on. But we also saw something even more interesting. Such as:

lib -> usr/lib
Copy the code

The above is the soft link information.

As shown in the table above, the lib directory, which is a shortcut to /usr/lib, has the same contents.

For more detailed information on the lS-L presentation, please refer to my diagram below. We’ll review this diagram again when we see what happens in the next section.

5.3.2 Hiding a File

Go directly to your /root directory, run ls -al, and you’ll see more. These extra hidden files, they’re all based on. Start with a configuration file. That’s what parameter A does.

[root@localhost ~]# ls -al
total 28
dr-xr-x---.  2 root root  135 Nov  4 07:53 .
dr-xr-xr-x. 17 root root  224 Nov  3 20:28 ..
-rw-------.  1 root root 1273 Nov  3 20:28 anaconda-ks.cfg
-rw-------.  1 root root  246 Nov  4 11:41 .bash_history
-rw-r--r--.  1 root root   18 Dec 28  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 28  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 28  2013 .bashrc
-rw-r--r--.  1 root root  100 Dec 28  2013 .cshrc
-rw-r--r--.  1 root root  129 Dec 28  2013 .tcshrc
Copy the code

For those of you who are careful, you will notice two special catalogues. . And… . The former represents the current directory, while the latter represents the upper directory.

Using the CD command, you can navigate freely through these directories.

Tip: If you have trouble reading dates in English, use ls-al –full-time to see the dates you can read.

5.4 switching directories

Run the CD command to switch the working directory to the target folder. To show the effects of the CD command. Run the following command as user root, which will create a 7-tier directory.

cd
mkdir -p a1/b2/c3/d4/e5/f6/{g7,g8,g9,g10}
Copy the code

We use the CD command to switch to the last layer. Then, we use.. Switch to the upper-layer directory.

[root@localhost ~]# cd a1/b2/c3/d4/e5/f6/g7
[root@localhost g7]# pwd
/root/a1/b2/c3/d4/e5/f6/g7

[root@localhost g7]# cd ..
[root@localhost f6]# pwd
/root/a1/b2/c3/d4/e5/f6
Copy the code

So, to switch to the n levels above, just use multiple levels of.. / can. There are a few special variables that need to be explained.

  • ../The upper level directory
  • . /.. /This refers to the upper two levels of directories
  • . /Refers to the current directory
  • ~Refers to the current user directory, which is an abbreviation
  • -Using it, you can switch back and forth between the last two directories

Let’s use the command to verify the above special variables.

Jump to the user root directory
[root@localhost tmp]# cd ~
[root@localhost ~]# pwd
/root

# Go to the third level directory
[root@localhost ~]# cd a1/b2/c3/
[root@localhost c3]# pwd
/root/a1/b2/c3

Jump back to the first three levels
[root@localhost c3]# cd .. /.. /..
[root@localhost ~]# pwd
/root

Jump to the last directory you visited
[root@localhost ~]# cd -
/root/a1/b2/c3
[root@localhost c3]# pwd
/root/a1/b2/c3

Enter the current directory: = do nothing
[root@localhost c3]# cd ./
[root@localhost c3]# pwd
/root/a1/b2/c3
Copy the code

That’s the common use of the CD command. Now, let’s go back and look at mkdir. As the name suggests, it means to create a directory, but generally in the work, the -p parameter is added, so that you can create multiple directories at once. Note the curly braces {} at the end of mkdir. You can specify multiple directories to create at once, which usually saves a lot of time.

5.5 File Operations

Using the command line to manipulate files is very convenient.

  • touchThe new file
  • cpCopy the file
  • mvMove files
  • rmDelete the file

These four coquettish orders dominated the direction of the documents. We continue with the directory we created above.

Create three files
[root@localhost ~]# touch long-long-long.txt
[root@localhost ~]# touch 996.txt
[root@localhost ~]# touch icu.txt
[root@localhost ~]# ls
996.txt  a1  anaconda-ks.cfg  icu.txt  long-long-long.txt

Copy a file
[root@localhost ~]# cp 996.txt 007.txt
[root@localhost ~]# mv long-long-long.txt short.txt
[root@localhost ~]# ls
007.txt  996.txt  a1  anaconda-ks.cfg  icu.txt  short.txt

Move 996.txt to a1 directory, icu. TXT to A1 /b2 directory
# remove short. TXT
[root@localhost ~]# mv 996.txt a1/
[root@localhost ~]# mv icu.txt a1/b2/
[root@localhost ~]# rm short.txtRm: remove regular empty file 'short-.txt'? y# recursively delete a1 directory
[root@localhost ~]# rm -rvf a1/Removed directory: 'a1/b2/c3/d4/e5/f6/g7' 'a1/b2/ C3 /d4/e5/f6/g9' removed directory: 'A1 /b2/ C3 /d4/e5/f6/g10' removed directory: 'a1/b2/ C3 /d4/e5/f6' removed directory: 'a1/b2/ C3 /d4/e5' removed directory: 'A1 /b2/ C3 /d4' removed directory: 'A1 /b2/ C3 / D4' removed directory: 'a1/b2/c3' removed 'a1/b2/icu' removed directory: 'a1/b2' removed 'A1/996.txt' removed directory: 'a1 / / root @ localhost ~'# ls
007.txt   anaconda-ks.cfg
Copy the code

After some manipulation, there was only 007 left. Beyond the basic operations above, I’d like to introduce some more important features.

You can see that when you delete files using RM, you are prompted once. This is to avoid deleting things by mistake, but sometimes, you don’t need to display this prompt, so you can add -f. The f parameter also applies to commands such as cp and mv. It means force.

rm -f file
cp -f file1 file2
mv -f file1 file2
Copy the code

In addition, there is an argument -r, which is what recursion means. Our directories and files usually have multiple levels, and recursion can be applied to all of them, such as the recursion above to delete directory A1.

# Warning: The following command can cause serious consequences rm -rf /Copy the code

This command above, you must see it often. This is not a joke, many users have lost data because of this, this is the legend of the root deletion, eventually you will have nothing. What does the parameter V do? When you add it, you can see the detailed execution of the command. In normal operations, I usually add.

6. Start file operations

As you probably already know, the first column of the ll -l command displays the file types in Linux. Give you an idea of what this is all about, because a lot of the commands that follow will use this knowledge.

  • -Represents a common file
  • dRepresent directory files
  • lRepresents a linked file, such as a shortcut
  • sSocket files
  • cCharacter device files, such as/dev/Many of the files in
  • bRepresents a block device file, such as some disks
  • pPipeline file

Files on Linux can have no suffixes, and you can create files that are counterintuitive. For example, the suffix is PNG, but it is a compressed file (which is usually not done). When I was in college, there were smart classmates who hid small movies like this, and the effect was very good.

To see the specific type of file, you can use the file command, which is smart enough to recognize many file formats.

[root@localhost ~]# file /etc
/etc: directory
[root@localhost ~]# file /etc/group
/etc/group: ASCII text
[root@localhost ~]# file /dev/log
/dev/log: socket
[root@localhost ~]# file /dev/log
/dev/log: socket
[root@localhost ~]# file /bin
/bin: symbolic link to `usr/bin'
Copy the code

The operations in this section are for plain text files of type ASCII Text. Next, we’re going to create some files. Then write something to the file for further action.

Create a file

6.1.1 Number sequence

With the redirection character, the file can be generated directly. Next, I’m going to generate the numbers 10 to 20, each on a single line, into a file called Spring. Coincidentally, the seq command does this.

seq 10 20 >> spring
Copy the code

We mentioned earlier that > means to redirect the output of the previous command elsewhere. In this case, we’re using two >’s, which still means redirect, but it means append to the original file.

That’s what w+ and A + mean in programming languages.

6.1.2 Viewing Content

To see how the files are generated, use the cat command to detect. The cat command will print the contents of the file to the terminal. If you add the parameter n, you can even print the line number. The effect is as follows:

[root@localhost ~]# cat spring
10
11
12
13
14
15
16
17
18
19
20
[root@localhost ~]# cat -n spring
1	10
2	11
3	12
4	13
5	14
6	15
7	16
8	17
9	18
10	19
11	20
Copy the code

The cat command is often used for more than just looking at the contents of a file. Only in conjunction with other commands can it find meaning in life.

Merge file a and file b into file c
cat a  b>> c

# Pipe the contents of file a as input. We'll talk about that later
cat a | cmd

# Write to the specified file. Very common in shell scripts. And we're going to use that a lot
cat > index.html <<EOF     EOF
Copy the code

Since our file is small, the cat command does no harm. But if the file is a few gigabytes long, it’s much more dangerous to use cat. This little command, called cat, will output like crazy on the terminal, and you can terminate it by pressing CTRL + C several times.

6.2. View files peacefully

Since the cat command is not suitable for large files, there must be an alternative. Less and more are. Since less loads faster than more, less is generally used now. Its primary use is for paging through file contents and providing some quick way to find them. Less is an interactive command that you need to use a few shortcuts to control.

This time we use SEq to generate 10 million rows, a full 76MB size, and then open it with less.

[root@localhost ~]# seq 10000000 > spring
[root@localhost ~]# du -h spring
76M	spring
[root@localhost ~]# less spring
Copy the code

The general operations for less are as follows:

  • The blank spaceScroll down to turn a page
  • bScroll up to turn a page
  • /Go into search mode, for example/ 1111The words 1111 will be looked up
  • qExit the less
  • gTo the beginning
  • GGo to the end
  • jScroll down
  • kScroll up, and these two buttons act very much like vim

6.3 File header and tail

Head displays the head of the file, and tail displays the tail of the file. Both of them can be specified with the -n argument.

[root@localhost ~]# head -n 3 spring
1
2
3
[root@localhost ~]# tail -n 3 spring
9999998
9999999
10000000
Copy the code

For some programmers, tail -f is probably one of the most commonly used commands. It can be in the control terminal, real-time monitoring of file changes, to see some rolling logs. Such as viewing nGINx or Tomcat logs and so on. Generally, too fast log scrolling still causes some problems. You need to use the grep command to filter logs.

Scroll to view system logs
tail -f /var/log/messages

Scroll to view the log information containing info
tail -f /var/log/messages | grep info
Copy the code

For the tail command, there is also a capital F argument. This parameter can monitor the recreated file. For example, some logs such as Log4J scroll by day and tail -f cannot monitor this change.

6.4. Find files

Consider the following scenario. We need to find a file called decorator.py, which is a ghost that can exist anywhere on the system. The find command is up to the task of ghost hunting.

We use the find command, starting from the root directory, which may take a while because the system is overloaded with files.

[root@localhost site-packages]# find / -name decorator.py -type f/ usr/lib/python2.7 / site - packages/decorator. PyCopy the code

You can run the time command to view the specific execution time. Execution is still pretty fast! Seconds!

[root@localhost site-packages]# time find/name decorator.py -type f /usr/lib/python2.7/site-packages/decorator.py Real 0m0.228s user 0m0.098s sys 0m0.111sCopy the code

The find command finds a collection of paths. This is usually done after the query has been queried, with additional processing, usually in conjunction with the xargs command (xargs reads the input and processes it line by line). Forget it, it doesn’t work!

Delete all class files in the current directory
find . | grep .class$ | xargs rm -rvf

# Find files accessed by /root the day before next
find /root  -atime 1 -type f

# Find files that have been updated within 10 minutes
find /root  -cmin  -10

# Find the files that belong to the root user
find /root -user root

# Find files larger than 1MB and clean them up
find /root -size  +1024k  -type f | xargs rm -f
Copy the code

Find has a lot of parameters. In fact, you can use the man command to view all except the commonly used ones. The operation of man is very similar to that of vi. Enter /EXAMPLES and you will see many EXAMPLES. But I think the commands listed above are more applicable.

6.4.1 Data Sources

In the picture above, you will see words like mtime,ctime,atime. Where does the data come from? Next, let’s take a logical look at the stat command.

[root@localhost ~]# stat springFile: 'spring' Size: 78888897 Blocks: 154080 IO Block: 4096 regular File Device: FD00h /64768d Inode: 8409203 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2019-11-04 18:01:46.698635718-0500 Modify: 2019-11-04 17:59:38.823458157-0500 Change: 2019-11-04 17:59:38.823458157-0500 Birth: -Copy the code

Isn’t that a file property? Everything from the file size, to the file type, and even to the last modification, access time, can be obtained from here. The Linux file system stores information in blocks. To find out where a file resides in storage space, each file is indexed by an I node (inode), which you can think of as a file pointer.

  • Number of bytes in the file
  • File owner user
  • Group that the file belongs to
  • Permissions to read, write, and execute files
  • The timestamp of the file
    • Ctime indicates the time when the inode last changed
    • Mtime Indicates the time when the file content was changed for the last time
    • Atime indicates the time when the file was last opened.
  • Link count, that is, how many file names point to the inode (ln command)
  • Location of file data block (specific data location)

Inode is a big topic and an important knowledge point. Those who are interested can search by themselves. We just need to know where the information came from.

6.4.2. Small exercises

If I just wanted to get Modify, I could use a combination of the commands I learned above. First get the last three rows, then get the first row. The effect is as follows:

[root@localhost ~]# stat spring | tail -n 3 | head -n 1The Modify: the 2019-11-04 17:59:38. 823458157-0500Copy the code

The following commands have the same effect, and the output is exactly the same. As the so-called all roads lead to Rome, next, we first introduce the high frequency of grep. In addition, we in the above these commands, use | for many times, this is very important in Linux pipeline concept, the following will also be introduced.

stat spring | head -n 7 | tail -n 1
stat spring | grep Modify
stat spring | sed -n '7p'
stat spring | awk 'NR==7'
Copy the code

6.5. String matching

Grep is used to filter the content, with the –color parameter to print color on supported terminals, and the n parameter is used to output the specific line number for quick positioning. This is a command that must be skillfully used.

For example, check the NGINx log for POST requests.

grep -rn --color POST access.log
Copy the code

It is recommended to use this parameter every time.

If I want to see the context of an exception, I can use the ABC parameter. They are abbreviations of several words and are often used.

  • A after n lines
  • A before B before
  • C

Something like this:

Look at the first 2 lines and the last 10 lines of the Exception keyword
grep -rn --color Exception -A10 -B2   error.log

# find all import keywords in /usr/and the number of files and lines in which they are located
grep -rn --color import /usr/
Copy the code

6.6, pipes,

In the above command, we use many times to |, it seems can do some amazing things. | is the meaning of the pipe, it can relate multiple commands. In general, commands have the following associations:

  • ;Sequential execution, such asmkdir a; rmdir a
  • &&Conditional execution, such asmkdir a && rmdir a
  • ||Conditional execution, such asmkdir a || rmdir a, the following command will not be executed
  • |The pipe, the output of the preceding command, is used as the input to the following command

The first three command associations are very simple, logical and easy to understand. And the pipeline, however, has its own characteristics.

Those of you who have touched programming languages know the concepts of STdin, STdout, and STderr. Let’s reorganize the definition for pipes: the output of the previous command (stdin) will be the input of the following command (stdout).

Let’s illustrate with a line of command.

seq 20 100 | head -n 50 | tail -n 1
Copy the code

The above command outputs 69. 69 is a magic number. How does it work? Let’s do a little picture, and everything will be clear.

For input/output and error, Linux uses a numeric abbreviation, which is often used in some scripts, and even in some installation files.

  • 0 indicates the stDIN standard input
  • 1 indicates stDOUT standard output
  • 2 indicates stDERR standard error

Error messages can be directed to standard output with syntax like 2>&1. Let’s prove it with a command.

Error message cannot be output to file
[root@localhost ~]# cat aaaaaaaaa > b
cat: aaaaaaaaa: No such file or directory
[root@localhost ~]# cat b

# Error message redirected
[root@localhost ~]# cat aaaaaaaaa > b 2>&1
[root@localhost ~]# cat b
cat: aaaaaaaaa: No such file or directory
Copy the code

6.7, sorting,

Now that you understand how pipes work, you can introduce the sort command. It can usually be combined with the UNIq (de-duplicate) command to perform some sort and de-duplicate operations. First, use the cat command to generate a file with the following content.

cat > sort.txt <<EOF
1 11
3 22
2 44
4 33
5 55
6 66
6 66
EOFBash then put these two commands on stage. Sort can use -t to specify the delimiter and -k to specify the columns to sort. But Spaces, you don't have to do any of these things. ```bash# Sort by the first column in reverse order
[root@localhost ~]# cat sort.txt | sort -n -k1 -r
6 66
6 66
5 55
4 33
3 22
2 44
1 11

# Count the number of occurrences of each row and sort it in reverse order
# Now the number of rows goes from 7 to 6
[root@localhost ~]# cat sort.txt | sort | uniq -c | sort -n -k1 -r2 6 66 1 5 55 1 4 33 1 3 22 1 2 44 11 1 11Copy the code

Note: The uniq command is typically used on sorted result sets. Therefore, in many cases, the sort command is used first and then the uniq command is used. Novices often forget the first step, causing the command to fail to work properly.

6.8. Small exercises

In this section, we started with file properties and looked at several common commands for manipulating files. The concept of pipeline is also introduced incidentally. Now, let’s practice.

Find all of them in the systemgrub.cfgFile, and prints the number of its lines.

Analysis: First you need to use the find command to find these files. Then use Xargs to process it line by line. Finally, use the WC command to count the exact number of lines.

[root@localhost grub2]# find / | grep grub.cfg | xargs wc -l
141 /boot/grub2/grub.cfg
Copy the code

Output a list of system groups

cat /etc/group | awk -F ':' '{print $1}'
Copy the code

The following command outputs the IP of the nginx log and the PV of each IP, the top 10 pv

# The 2019-06-26 T10:01:57 + 08:00 | nginx001. Server. Ops. Pro. Dc | 100.116.222.80 | 10.31.150.232:41021 | | | | 0.000 0.011 0.014 200 | 200 | 273 | | / visit | sign = 91 cd1988ce8b313b8a0454a4bbe930df | | - | - | HTTP POST | 112.4.238.213

awk -F"|" '{print $3}' access.log | sort | uniq -c | sort -nk1 -r | head -n10
Copy the code

6.9. Think & Expand

1, Linux terminal, how to achieve color text? How do I output a green Hello World?

2. What are the differences between soft links and hard links?

3. Know a few partial but not very partial orders.

  • cutWith AWk, it’s almost impossible to use cut, right
  • tr
  • col
  • paste
  • join
  • split

7. Regular and advanced usage

You might run into some tough problems and search for the results you want, but next time you have to search for the solution, which is not the inefficient way to do it. Typically, an online operations engineer doesn’t leave much time for onsite learning when problems arise.

In order to achieve more effective training, we need to do two things: first, summarize; Second, by analogy. The same is true of Linux commands. There are often multiple solutions to a problem, and you need to find the commonalities by changing them.

This involves some designers adhering to the conventions of the specification. Generally, you only need to master a small number of commands, and then know a large number of commands to be able to navigate the command line world. For example, if you know that ls lists file directories, you would think that LSCPU lists CPU information. Lsmem lists memory information. LSBLK indicates disk information. There are many commonalities, such as the TOP series, stat series.

7.1 Auxiliary Information

7.1.1 Linux File Format

Working on Linux, you’re very, very averse to binary, and almost everything is text that you can read and write. The result of most commands is also a text file. These files are characterized by columns that are usually separated by a space or a

key. Such as the following lsMEm results, such a regular, rules-based file, is very easy to process.

[root@localhost ~]# lsmem  
RANGE                                  SIZE  STATE REMOVABLE BLOCK
0x0000000000000000-0x0000000007ffffff  128M online        no     0
0x0000000008000000-0x000000000fffffff  128M online       yes     1
0x0000000010000000-0x0000000017ffffff  128M online        no     2
0x0000000018000000-0x0000000027ffffff  256M online       yes   3-4
0x0000000028000000-0x000000004fffffff  640M online        no   5-9
0x0000000050000000-0x000000005fffffff  256M online       yes 10-11
0x0000000060000000-0x000000007fffffff  512M online        no 12-15

Memory block size:       128M
Total online memory:       2G
Total offline memory:      0B
Copy the code

There is a large number of commands for line operations, as well as for column operations. Then, there are two aggregators, called SED and AWK. Because these two commands are so extensive, they are listed in a separate section.

7.1.2. What should I do if I can’t remember commands?

In general, Linux commands are fairly simple, but some are somewhat complex. Commands such as find and ps can be very bulky if they are to be used in all cases. But what if it comes to an off-door situation like this?

It is necessary to have a comprehensive understanding so that when using it, it can evoke the most superficial impression in memory. The rest can then be handed over to a command like man. Every command on Linux has a help file that goes with it, and that’s far more accurate than all the information that goes around on the web.

Formally introduce the following two commands:

  • manDisplays the document information about a command. Such as:man ls
  • infoYou can think of it as the same as man, although there are some complementary elements. They will indicate it in the content
  • --helpMany commands take arguments--helpProvide very brief help information. This is usually the most useful and quickest use case presentation. If you can’t remember a very difficult word, look for these places

Note: This help information is focused only on the scope of the command itself. There is not much information about its combined use scenarios. In other words, it teaches you how to use it, but it doesn’t tell you what you can do with it.

These help commands, typically by highlighting keywords, add to the reading experience. But we can go one step further and turn the help files into color. Run the following command as user root. Then, log in to the VM again.

cat >> ~/.bashrc <<EOF function man() { env \\ LESS_TERMCAP_mb=\$(printf "\e[1;31m") \\ LESS_TERMCAP_md=\$(printf "\e[1;31m") \\ LESS_TERMCAP_me=\$(printf "\e[0m") \\ LESS_TERMCAP_se=\$(printf "\e[0m") \\ LESS_TERMCAP_so=\$(printf "\e[1;44;33m") \\ LESS_TERMCAP_ue=\$(printf "\e[0m") \\ LESS_TERMCAP_us=\$(printf "\e[1;32m") \\ man "\$@" } EOF
Copy the code

Run the man command again and you can see the color information.

1.3 TAB completion

Now, in the terminal, type ca and press TAB twice quickly. The command line goes into completion mode and displays all commands beginning with CA.

[root@localhost ~]# ca
cacertdir_rehash     cache_dump           cache_repair         cache_writeback      ca-legacy            capsh                case                 catchsegv
cache_check          cache_metadata_size  cache_restore        cal                  caller               captoinfo            cat                  catman
Copy the code

This is great if you have only a vague idea of a command, remembering only the first few letters, and the scope of the command is gradually reduced.

7.2 regular expressions

To get started, let’s first introduce regular expressions. You can also use these regular expressions in the previous commands, such as less, grep, and so on.

There are books that can write a book about regular expressions, and we are only going to cover them briefly here, but they will suffice. In general, regular expressions can be used for matching, and can also be used to reuse the matched content. The latter is covered in the sed command.

mark meaning
^ The beginning of a line
$ End of each line
. Any single character
* Matches zero or more preceding characters
+ One or more matches
? Zero or one matches
{m} The previous match is repeated m times
{m,n} The previous match is repeated m to n times
[] Matches characters in a specified range
(^) Matches any single character outside the specified range
\ Escape character
[0-9] Matches any character in parentheses, the effect of or
` `
\b Matches a word. Such as\blucky\bOnly the word lucky is matched

Using the following command to create a file, let’s practice the regular representation of the grep command with the E argument.

cat > 996 <<EOF
996: 996 is a funcking thing . which make woman as man , man as ass .
we all on the bus , bus bus on the way . 996
way to icu. icuuuuuu......
The greedy green boss rides on the pity programmer
EOF
Copy the code

Run the following command on the terminal, noting that the highlighted part is the matched string.

# match lines beginning with 996
[root@localhost ~]# cat 996 | grep -E ^996
996: 996 is a funcking thing . which make woman as man , man as ass .

# match line ending in 996
[root@localhost ~]# cat 996 | grep -E 996$
we all on the bus , bus bus on the way . 996

# match to ICU and icuuuuuu
[root@localhost ~]# cat 996 | grep -E icu+
way to icu. icuuuuuu......

# match to 996 again
[root@localhost ~]# cat 996 | grep -E [0-9]
996: 996 is a funcking thing . which make woman as man , man as ass .
we all on the bus , bus bus on the way . 996

[root@localhost ~]# cat 996 | grep -E ^[\^0-9]
we all on the bus , bus bus on the way . 996
way to icu. icuuuuuu......
The greedy green boss rides on the pity programmer

# match all lines that do not contain 996, conscience command, tears
[root@localhost ~]# cat 996 | grep -E -v [0-9]{3}
way to icu. icuuuuuu......
The greedy green boss rides on the pity programmer

# Match boss and ICU
[root@localhost ~]# cat 996 | grep -E boss\|icu
way to icu. icuuuuuu......
The greedy green boss rides on the pity programmer

# match all lines
[root@localhost ~]# cat 996 | grep -E .
996: 996 is a funcking thing . which make woman as man , man as ass .
we all on the bus , bus bus on the way . 996
way to icu. icuuuuuu......
The greedy green boss rides on the pity programmer
Copy the code

Regular expressions are important, and in some sed scripts, AWk scripts, and even vim editors, they make things easier for you. The above should be memorized to the point where you don’t need to look for documentation.

Here are six quick questions to think about.

1. Go back and execute man cat. Do you find a command called tac? What does it do?

2. The stat series mentioned above, can you imagine what iostat is generally used for?

3. What is the meaning of grep -v?

4. Check out the rename command, which is very similar to mv, to batch modify files. See if you can use the above re.

5. How to quickly correct some commands if they are misspelled? By search? Learn about the fuck command. I’m not wrong.

6. Which of the following indicates that cmd2 is executed if cmd1 is executed successfully?

  • A. cmd1&&cmd2
  • B. cmd1|cmd2
  • C. cmd1; cmd2
  • D. cmd1||cmd2

8. Compression under Linux

Compression, it’s an amazing thing.

Long, long ago, there were some 64KB movies that you couldn’t watch in half an hour. In fact, the actual capacity of these animations was 15GB, which the Warez organization compressed by a factor of 250,000.

If you have Windows, you can download it here and try it out. But we’re talking about Linux here, so it’s a slap in the face, isn’t it?

Link: HTTPS://pan.baidu.com/s/12YJQ4jsbtRr7RxoLpARTyQ Extract code: R7SPCopy the code

Compression is an amazing thing. It can be big or small, it can stretch or shrink, and it’s hard to find anything like that in real life.

In order to reduce the size of the transferred file, or for the convenience of transfer, it is common to turn on compression. Linux common compressed files are tar, bzip2, zip, rar, and so on. 7z is relatively less used. Compressed files, very suitable for transmission on the network environment. Even, you can think of ISO files as a special compression method.

. Tar Use the tar command to compress or decompress. Bz2 Use the bzip2 command to operate

Preparation: Copy 1000 files using the following command.

cd ~
mkdir files
cd files
seq 1000 | xargs -I {} cp  /etc/group  {}
Copy the code

Using ls, you can see the 1000 files that we just created. Next, we use the compress command to package it into one.

Check the total size of 1000 files
[root@localhost files]# du -h .
4.0M	.

Switch to the root directory
cd ~

Use tar to compress the file. The compressed file is less than 1MB
[root@localhost ~]# tar cvf files.tar files

[root@localhost ~]# du -h files.tar
1012K	files.tar

Use gizp to increase the compression ratio. The compressed file is only 12KB
[root@localhost ~]# gzip files.tar
[root@localhost ~]# du -h files.tar.gz
12K	files.tar.gz
Copy the code

Tar and gzip are generally used in combination. The tar command provides a special feature that allows you to call other compressors, such as gzip, bzip2, and so on, while unpacking.

The following command is the same as the previous command executed twice. Therefore, the general use of the following way to operate.

[root@localhost ~]# tar cvfz files2.tar.gz files
[root@localhost ~]# du -h files2.tar.gz
12K	files2.tar.gz
Copy the code

The corresponding operation is decompression. We only need to change one letter on the command line: C ->x. But in fact, the parameters V and z can also be omitted.

[root@localhost ~]# tar xvfz files2.tar.gz
Copy the code

The more common way is to add a parameter C to specify a directory to decompress. For example, the following command decompresses the compressed contents into the /opt directory.

[root@localhost ~]# tar xvfz files2.tar.gz -C /opt/
Copy the code

What if I just want to see what files are in the zip file? That’s going to use the parameter T.

  • cThe compression
  • xUnpack the
  • tCheck the list

Install others

Let’s see if the usual ZIP and RAR decompression programs are installed.

[root@localhost ~]# which unzip
/usr/bin/which: no unzip in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]# which unrar
/usr/bin/which: no unrar in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
Copy the code

Therefore, neither of these applications is installed on our system. You can use the package management tool yum to install it. The JAR command in Java is similar to zip and can be explored by yourself.

[root@localhost ~]# yum install -y zip unzip rar unrar
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.aliyun.com
...
Copy the code

Rar cannot be installed successfully, so rar files cannot be decompressed. That’s ok, we’ll install it in a later section.

Now, will you install Tomcat on Linux?

Next, consider:

1, after zip compressed files, and then use gZIP compression, the capacity will be reduced?

To verify this process, use the dd command to generate a random file of 69MB size. The DD command is also magical.

[root@localhost ~]# dd if=/dev/urandom of=test bs=1M count=69
69+0 records in69+0 Records out 72351744 bytes (72 MB) copied, 0.446161s, 162 MB/s [root@localhost ~]# du -h test
69M	test
Copy the code

So, going back to the top of the article, we can generate a random batch of files to make the compression a little more meaningful.

seq 1000 | xargs -i dd if=/dev/zero of={}.xjj bs=1k count=256
Copy the code

2. How does the tar command overwrite a file if it already exists?

9. Linux permission system

Right at the very top, when we first touch the command line, we use the chmod command to give execution permission to plain text files. This section takes a look at the related concepts of user permissions and file permissions,

9.1 Adding a User

Up to now, our system, still only this one user alone, it is time to learn nuwa, knead a few small clay figures.

First create two users: Zhang3 and Li4.

[root@localhost ~]# useradd zhang3
Copy the code

View the three outputs of the following command.

# There is an extra group named zhang3 in the system. The group file saves the system group information
[root@localhost ~]# tail -n1 /etc/group
zhang3:x:1000:

# system has an extra user named zhang3, and the shadow file saves their password. A lot of security penetration is just to get it for brute force cracking
[root@localhost ~]# tail -n1 /etc/shadowzhang3:!! : 18207:9999-7:0:9: :In the # home directory, there is an extra directory called zhang3
[root@localhost ~]# ll /home --full-timeTotal 0 DRWX ------. 2 zhang3 zhang3 83 2019-11-06 22:09:33.357165082-0500 zhang3Copy the code

Next, set a password for the user we just created using passwd. Enter the password twice for confirmation. To change the password, use the chpasswd command.

[root@localhost ~]# passwd zhang3
Changing password for user zhang3.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
Copy the code

So how do you delete an existing user? This is done through the userdel command. If f is added, other users will be forced to log out of the system.

userdel -f zhang3
Copy the code

9.2 Description of File Permissions

There are two interesting things about the results of the above command execution. After adding the user, in addition to adding some content in the password file shadow, we also added information in the group file. This involves two attributes of the user: the user name and the group name.

A user has only one name code, but can have multiple groups. Create user li4 with password 123 and append a group named zhang3 to it. You can see the changes in the /etc/group file.

[root@localhost ~]# useradd -G zhang3 -p 123 li4
[root@localhost ~]# tail -n 2 /etc/group
zhang3:x:1000:li4
li4:x:1001:
Copy the code

Ok, let’s switch to our file permissions. To validate the following command, we first create a script file named confirm777.sh. To make the script visible to all users, we created it in the/TMP directory.

cat > /tmp/confirm777.sh <<EOF
echo $USER
id
EOF
Copy the code

Run the ll command to view the file information.

[root@localhost ~]# ll /tmp/confirm777.sh --full-time-rw-r--r-- 1 root root 13 2019-11-07 04:25:55.418254935-0500 confirm777.shCopy the code

The ll command shows that the owner of the file is user root, and the group to which the file belongs is also the root group, and its permission is rw-r–r–. File permissions are divided into three parts.

  • Owner permission, abbreviated asu. Permissions owned by the owner of the file. That is, the permission of the root user. Yesrw-
  • Group User Rights, abbreviated asg. Permissions of all users in the owning group of the file. Because there is only one user root in the root group, the group user permission isr--.
  • Other User rights, abbreviated aso. The permissions of other unrelated users, such as the newly created zhang3 and li4 users, arer--.
  • all, abbreviated asa, indicating the collective operation on the three types of users.

So what does RW — – What do these things mean?

  • rIndicates the read permission. The read.
  • wIndicates the write permission. The write.
  • xIndicates the executable permission. The execute.
  • -Permission placeholder, indicating that there is no current permission.

Note: just because a user has w permission on a file does not mean he can delete it. W is only for the content of the file.

A file, there are 3 types of users, each type of user, has 3 kinds of permissions. Using the simplest elementary multiplication, we can conclude that the permission bits of a file require 3×3=9 flag bits.

The name of our file is confirm777.sh, is the name random? Of course not. 777 has a special meaning in Linux. It means that the file is readable, writable, and executable for all users. Imagine that if every file had such permissions, the system would be insecure. So where does this string of numbers come from? You can see the comparison table below.

  • r 4
  • w 2
  • x 1perform

By any combination of the above three attributes, we can get:

  • 4 r-- 4 + 0 + 0
  • 6 rw-4 + 2 + 0
  • 5 r-x4 + 0 + 1
  • 2 -w-0 + 2 + 0
  • 3 -wx0 + 2 + 1
  • 1 --x0 + 0 + 1
  • 7 rwx4 + 2 + 1

9.3. File Permission change

Here are three file permissions related commands. The most common ones are chown and chmod.

Chown changes the owner of the file. CHGRP Changes the group of files. Chmod changes file permissions.

Next, we change the owner and group of confirm777.sh to the newly created user zhang3.

cd /tmp
[root@localhost tmp]# chown zhang3:zhang3 confirm777.sh
[root@localhost tmp]# ll confirm777.sh
-rw-r--r--. 1 zhang3 zhang3 13 Nov  7 04:25 confirm777.sh
Copy the code

Add execute permission to the file owner. Then switch to zhang3 and li4 respectively.

You can run the su command to switch to another user. Generally, you can run the su – command to clear environment variables. If you run the id command, you can view the information about the current user.

[root@localhost tmp]# chmod u+x confirm777.sh
[root@localhost tmp]# su li4
[li4@localhost tmp]$ ./confirm777.sh
bash: ./confirm777.sh: Permission denied
[li4@localhost tmp]$ exit
exit

[root@localhost tmp]# su zhang3
[zhang3@localhost tmp]$ ./confirm777.sh
root
uid=1000(zhang3) gid=1000(zhang3) groups=1000(zhang3) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Copy the code

As you can see, zhang3, the owner of the file, can execute the file, but the unrelated li4 prompts that there is no permission. Next, we verify the permission bits associated with the user group.

Delete zhang3's execution permission
root@localhost tmp]# chmod u-x confirm777.sh
[root@localhost tmp]# ll confirm777.sh
-rw-r--r--. 1 zhang3 zhang3 13 Nov  7 04:25 confirm777.sh

# add execution permission to group zhang3, because li4 is in group Zhang3, it has permission
[root@localhost tmp]# chmod g+x confirm777.sh
[root@localhost tmp]# ll confirm777.sh
-rw-r-xr--. 1 zhang3 zhang3 13 Nov  7 04:25 confirm777.sh

# Switch to zhang3 for execution
[root@localhost tmp]# su - zhang3
[zhang3@localhost tmp]$ ./confirm777.sh
bash: ./confirm777.sh: Permission denied
[zhang3@localhost tmp]$ exit
exit

Switch to li4 for execution
[root@localhost tmp]# su - li4
[li4@localhost tmp]$ ./confirm777.sh
root
uid=1001(li4) gid=1001(li4) groups=1001(li4),1000(zhang3) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Copy the code

As can be seen from the execution result of the command. This time, li4 can execute the file, conversely, Zhang3 cannot.

We use the chmod command to change file permissions, using English letters like a+x. In the case of the first script, the initial permission is rw-r–r–, which is 644. In this case, the following two scripts are equivalent.

chmod u+x confirm777.sh
chmod 744 confirm777.sh
Copy the code

As you can see, the second command, which uses numeric style permission bits, is an extra step in the human transformation process. This is very inconvenient in daily use. Therefore, using the symbol method of representation, can be more intuitive, very recommended.

To make this process more intuitive, I created a diagram.

9.4 directory permissions

Here’s a very interesting point. To set files to executable, you can turn ordinary files into scripts, what the hell is executable permission on directory files? What’s the point? For folders:

  • rIndicates that the file name in the directory can be read, but the directory cannot be accessed
  • wAllows users to create, migrate, delete, and rename files in a directory
  • xYou can obtain a list of files in the directory, and enter the directory and run the CD command

For the difference between r and x, look at the following command results to get a feel for the difference. Generally, almost all directories have execution permissions. Do not set them arbitrarily.

[root@localhost tmp]# su - li4
[li4@localhost ~]$ cd /tmp

[li4@localhost tmp]$ mkdir nox
[li4@localhost tmp]$ touch nox/{a,b,c,d}
[li4@localhost tmp]$ chmod a-x nox
[li4@localhost tmp]$ ls nox
ls: cannot access nox/a: Permission denied
ls: cannot access nox/b: Permission denied
ls: cannot access nox/c: Permission denied
ls: cannot access nox/d: Permission denied
a  b  c  d
[li4@localhost tmp]$ cat nox/a
cat: nox/a: Permission denied

[li4@localhost tmp]$ chmod a+x nox
[li4@localhost tmp]$ chmod a-r nox
[li4@localhost tmp]$ ls nox
ls: cannot open directory nox: Permission denied
Copy the code

9.5, sticky bit

Next, let’s introduce the sticky bit.

If you want to delete a file, you may not have write permission for the file, but you must have write permission for the parent directory of the file. How to create a directory that lets anyone file some files, but can’t delete other users’ files? That’s what the Stick Bit is for. Paste bits are generally used only on directories and are not useful for files. The paste bit is usually denoted by T.

We can look at a typical directory/TMP

[root@localhost tmp]# ls -dl /tmp
drwxrwxrwt. 9 root root 4096 Nov  7 06:27 /tmp
Copy the code

As you can see, the last bit, instead of an X, it’s a T, which means regular users can’t delete other users’ files. All users in the/TMP directory can create files at will, but they cannot delete other people’s files, even if the file is granted 777 permission.

[root@localhost tmp]# touch /tmp/stick
[root@localhost tmp]# chown li4:li4 /tmp/stick
[root@localhost tmp]# chmod 777 /tmp/stick
[root@localhost tmp]# su - zhang3[zhang3@localhost ~]$rm/TMP /stick rm: cannot remove '/ TMP /stick' : Operation not permittedCopy the code

We created two users zhang3 and li4, tested the chown and chmod commands with them, and finally introduced the paste bit. The reason Linux is more secure is because it has more detailed permission division. But permission is a double-edged sword, super power user a command can bring down the system, many hidden Trojan, through the right to run in unknown places.

Several permissions related commands are often used. Here are a few examples.

Set /var/lib/mysql user and group to mysql
chown -R mysql:mysql /var/lib/mysql

Set the directory to be readable and writable, and upload files
chmod  777 /var/www/uploads

Add permission to execute all sh files in this directory
chomd a+x *.sh

Change file to read, write, and execute
chmod u=rwx,g=rwx,o=rwx file
Copy the code

Again, time to think:

1. What happens when the following command is executed? Warning: Do not execute even if you change 000 to another number.

# R traversal subdirectory
chmod -R 000 /
Copy the code

2, one day, I saw a command chmod u+s file, the text does not introduce the meaning of s, what does it mean?

3. How do I delete a user group?

10. How do I perform operations on disks?

The following scenario is terrifying and can be a nightmare for some programmers.

Just go to work in the early morning, just eat half of jianbing fruit, several armed police broke into the company. Without saying a word, he controlled the staff and waited for the fish to come to work.

The reason: the company is suspected of storing and distributing illegal documents, and all its servers need to be seized and thoroughly investigated.

Some of these files were simply placed on disks, some were placed on file storage servers, and some were sliced up and placed on different cheap machines.

What happens next depends on the level of the technicians, but the results are not expected to be very good.

In the previous section, we created two regular users who are incompetent and have far fewer permissions than the default user root. It has no permission to modify almost anything except files in its own directory.

These files, they must be on disk. There are a lot of commands for disk management, and this section is often used by system administrators. But for development, the requirements are lower. Because developers only need to know where the movies are, not how.

So when it came to conviction, who was more to blame, the operator or the programmer? It’s a paradox. Operation and maintenance personnel in a daze, the brain recalled the following knowledge.

10.1. Adding a New Hard Disk

If you’re a system administrator, even a system administrator in the cloud, and you buy an AWS extension disk right now, it’s not going to be used. It must be formatted and mounted before it can be put into production.

Remember when you installed the system? In one of the steps, we need to divide the disk of the virtual machine, and we will use the default method. But I can’t change it now. It’s in the past.

To simulate disk management, we need to add a new virtual disk to the virtual machine first. First, use the shutdown -h now command to shutdown the machine.

1. Go to Settings, switch to Storage, and add disks

2. Click Create a disk

3. Select VDI

4, dynamic expansion, how much to expand

5. We create a 2GB disk called Disk2

Start the machine. To remotely connect to IP address 192, do not forget to run the dhclient command.

First, use fdisk to take a look at the current state of the disk.

root@localhost ~]# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c2410

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    16777215     7339008   8e  Linux LVM

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-root: 6652 MB, 6652166144 bytes, 12992512 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 859 MB, 859832320 bytes, 1679360 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Copy the code

From the result of the command, we see that there are two disks. /dev/sda and /dev/sdb, where sda has already been allocated and is already occupied by our file system. Now, remember the purpose of the /dev directory. This is where the device files are stored. If we were to add another disk, its handle would be SDC (sd*).

We need to perform three operations on the entire disk before it can be used.

  1. Disk partition
  2. Disk formatting
  3. Disk mount

10.2. The partition

Still use the fdisk command for the disk partition, the following command will enter interactive mode. In interactive mode, enter n to create a partition. Since our disk is only 2GB, just create one partition. Follow the wizard to go all the way down, and finally, type w to confirm writing to the partition table while exiting the command interaction.

Run fdisk -l again and you can see that you have an extra 2GB partition.

[root@localhost ~]# fdisk /dev/sdb. [root@localhost ~]# fdisk -l. Device Boot Start End Blocks Id System /dev/sdb1 2048 4194303 2096128 83 Linux ...Copy the code

10.3. The formatting

On the command line, type MKFS and then press

to complete. A batch of commands will be displayed.

[root@localhost ~]# mkfs.
mkfs.btrfs   mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.minix   mkfs.xfs
Copy the code

These commands can format the disk. Currently, the most common disk format is ext4. We didn’t find Windows FAT and NTFS, but they are conceptually equivalent.

Here are some common disk formats for Linux.

  • btrfsThe GPL license. Is initiated as a replacement for the Ext system. Not familiar, but more evaluation.
  • cramfsGate A read-only compressed file system designed for flash memory with a maximum capacity of 256M, zlib compressed and rarely used
  • ext2An earlier version of Ext.
  • ext3Improvements to ext2.
  • ext4Most used. If you are not familiar with the others, the old honest use ext4 bar.
  • minixIt’s older and not used very often.
  • xfsThe XFS file system is an extension of the extended file system and is a 64-bit high performance journaling file system. The default file system starting with centos7.0.

We’ll do as the Romans do and format the disk into XFS.

[root@localhost ~]# mkfs.xfs /dev/sdb1
Copy the code

Note: If you want to format the disk to FAT32 format, you need to install a software.

 yum install dosfstools -y
Copy the code

10.4. The mount

The last step is to mount the disk using the mount command. We mount it to the /data directory.

Using the df command, you can view the disk usage of the system. Parameter h stands for human and displays the information in an easy-to-read way. LSBLK displays the system disk mounting status from another perspective.

[root@localhost ~]# mkdir /data
[root@localhost ~]# mount /dev/sdb1 /data
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 908M     0  908M   0% /dev
tmpfs                    920M     0  920M   0% /dev/shm
tmpfs                    920M  8.6M  911M   1% /run
tmpfs                    920M     0  920M   0% /sys/fs/cgroup
/dev/mapper/centos-root  6.2G  1.4G  4.9G  22% /
/dev/sda1               1014M  149M  866M  15% /boot
tmpfs                    184M     0  184M   0% /run/user/0
/dev/sdb1                2.0G   33M  2.0G   2% /data

root@localhost ~]# lsblk -fNAME 'LVM2sd'.pdf ├─sda1 XFS 'LVM2SD'.PDF '.PDF '.PDF 3gzmod-tuc1-p2zn-wt5q-ttky-pf495o ├─centos-root XFS 9f86f663-060a-44450-90f9-3005ad9c5d92 / sci.0000.pdf 7609b0-b0ab-4d44-a23e-ad76f760efad6 [SWAP] SDB ├ ─sdb1 XFS 0a7c8601-1a31-45b3-bf37-c72229f7704 /dataCopy the code

In order to load the disk at startup, we need to modify the /etc/fstab file. Be careful when modifying such files, otherwise the system will fail to start.

[root@localhost ~]# echo "/dev/sdb1 xfs defaults 0 0" >> /etc/fstab
[root@localhost ~]# cat /etc/fstab
/dev/mapper/centos-root / xfs defaults        0 0
UUID=ac3a3ce8-6ab1-4c0b-91c8-b4e617f0dfb4 /boot  xfs     defaults        0 0
/dev/mapper/centos-swap swap    swap    defaults        0 0
/dev/sdb1                       xfs     defaults        0 0
Copy the code

10.5. Swap partitions

Due to limited memory capacity, today’s operating systems use disk to simulate a virtual memory region for caching some data. Due to the disk speed and memory are not the same, often cause applications to stall. The cards stay on the cards, but the application process can survive.

The swap partition is the swap partition. When the physical memory is insufficient, the system swaps with the swap partition. That is, when the physical memory of the system is not enough, a part of the space in the hard disk is released for the use of the current running program. When those programs are ready to run, the data saved from swap is restored to memory.

Modern Internet companies have high requirements on the response time of interfaces. Swap is disabled. There are several related commands for swap.

Create swap partition
[root@localhost ~]# mkswap /dev/sdb1

# Disable all swap partitions
[root@localhost ~]# swapoff -a

# Enable swap partition
[root@localhost ~]# swapon
Copy the code

10.6 Using the dd Command to Back up data

The following command will directly back up disk A to disk B.

# dd if=/dev/sda of=/dev/sdb
Copy the code

In the above command, if represents the input file and of represents the output file. According to the principle that everything is a file under Linux, the file here refers to the device.

The dd command can also package the entire disk into a single image file. Take the following command for example.

# dd if=/dev/hda of=~/hdadisk.img
Copy the code

Of course, the recovery of the disk, is also quite simple, we only need to reverse the operation can be.

# dd if=hdadisk.img of=/dev/hda
Copy the code

End

This article has 60,000 words and has been processed in many versions. Some friends have taken it as a training material for the company. By now, you’ve got a fairly objective view of the Linux command line. But I have many more articles that can take you to the next level, such as the use of Vim, sed, and awk. Here are a few extended readings, also using xJJDog’s dedicated command line three-paragraph parsing, that you’ll find helpful.

The most commonly used series, quickly master the three base guest ↓

The most common set of Vim techniques the most common set of Sed techniques and the most common set of AWK techniques

Cast away series

Cast away series, quick grasp troubleshooting ↓

I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O

Welcome to add my friend Xjjdog0, a group to increase the buff value.

Author introduction: Little sister taste (XJjdog), a programmer is not allowed to detour the public number. Focus on infrastructure and Linux. Ten years of architecture, 10 billion traffic per day, and you discuss the high concurrency world, give you a different taste. My personal wechat xJJdog0, welcome to add friends, further communication.