Not every detail.

The first step to learning Linux is to have a Linux environment. Here we run CentOS using a virtual machine (VirtualBox is recommended, it is free).

For details, see installing CentOS7 using VirtualBox.

After installation, the picture is as follows:

So INSTEAD of using VirtualBox, I’m using Paralles. It’s as good a virtual machine as VirtualBox. The difference is that Paralles only runs on MacOS, and it’s not free.

This is a graphical interface that can be switched to the full command line interface by using the shortcut keys Ctrl + Alt + F2:

In fact, f2-F6 can be switched.

Enter the user name, press Enter, enter the password to log in the system.

Switch back to the graphical interface using Ctrl + Alt + F1.

Some systems may need to press FN at the same time to trigger the function keys F1 and F2.

Learn the first command

date

The date command is used to print the current time.

[qe@centos-7 ~]$date 2020 10:41:47 CSTCopy the code

In this command, [qe@centos-7 ~]$is the command prompt. Qe indicates the current user name, centos-7 indicates the host name, and ~ indicates the current directory.

~ is a special directory that represents the current user’s home directory. So this means that the current directory is the home directory of the QE user.


The first command is simple. Let’s look at one of the most common commands:

ls

Ls is short for list and is used to list files and directories in the current directory.

[parallels@centos-7 ~]$ ls
Desktop    Downloads   Music     Public     Videos
Documents  fontconfig  Pictures  Templates
Copy the code

Different commands have different parameters. For example, ls -a is used to list all files and directories in the current directory.

[parallels@centos-7 ~]$ ls -a
.              .bashrc  Documents      .local      .pki
..             .cache   Downloads      .mozilla    Public
.bash_history  .config  .esd_auth      Music       Templates
.bash_logout   .dbus    fontconfig     .parallels  Videos
.bash_profile  Desktop  .ICEauthority  Pictures    .viminfo
Copy the code

-A indicates all, indicating all files and directories, including hidden files and directories. In Linux, it precedes the file name. Indicates hiding.

Note that Linux is case sensitive.

For example, ls -l lists detailed information.

Multiple short parameters can also be combined, such as ls-la:

— represents a long parameter. So ls –all, for example, has the same effect as LS -a.

Practical skills

1. On the CLI, use the ↑ and ↓ keys to switch between the previous command and the next command, and use the Tab key twice to complete the command.

2. Using the history command, you can view all commands that have been executed.

Each history command is preceded by a number, by using! You can execute corresponding history commands.

[parallels@centos-7 ~]$ ! 6 ls Desktop Downloads Music Public Videos Documents fontconfig Pictures TemplatesCopy the code

3. Shortcut key Ctrl + A to the beginning of the current line, Ctrl + E to the end of the current line.

Catalogue organization

Linux has a root directory, /, which is the topmost directory. The direct subdirectories of the root directory are shown in the figure above. Unlike Windows, Linux does not have the concept of a C or D disk, and Linux uses/as a path separator, such as /home/parallels/Desktop.

pwd

PWD Displays the current directory.

[parallels@centos-7 Desktop]$ pwd
/home/parallels/Desktop
Copy the code

which

Which is used to print the location of a command.

[parallels@centos-7 ~]$ which pwd
/usr/bin/pwd
Copy the code

ls

Ls was introduced earlier to list all files and directories. Ls -l is used to list details, as shown in the following figure:

In the figure above, the file size is in bytes and can be displayed in KB, MB, and GB using ls -lh.

Ls-lt is sorted by last modified time.

cd

CD is short for change directory. It is used to change directories.

[parallels@centos-7 ~]$ cd /
Copy the code

. Indicates the upper-level directory,. Indicates the current directory, and ~ indicates the current user home directory.

[parallels@centos-7 ~]$ cd ..
Copy the code

du

Du is short for Disk Usage and shows the size of files contained in a directory, in bytes by default. Du -h can be displayed in KB, MB, and GB units.

Du displays only directory sizes by default. You can use du -a to display directory and file sizes. Du -s displays only the total size.

Operation file

File operations mainly include browsing files and creating files.

catless

Cat and less are used to display file contents. Cat can display all the contents of a file at once:

[parallels@centos-7 log]$ cat Xorg.0.log
Copy the code

You can use cat-n filename to display the line number.

Less displays file contents in pages.

[parallels@centos-7 log]$ less Xorg.0.log
Copy the code

The less command displays the contents of a page. Use the Enter key to view the next line, the space key to view the next page, the B key to view the previous page, the Y key to move back a line, and the Q key to exit.

headtail

Head and tail are used to display the beginning and end of a file. The default display is 10 lines. You can use tail -n to specify the number of rows to display.

[parallels@centos-7 log]$ head Xorg.0.log
......
[parallels@centos-7 log]$ tail -n 5 Xorg.0.log
......
Copy the code

touchmkdir

Touch and mkdir are used to create files and directories.

[parallels@centos-7 Desktop]$ touch test.txt
[parallels@centos-7 Desktop]$ mkdir test
Copy the code

Mkdir -p creates directories recursively.

[parallels@centos-7 Desktop]$ mkdir -p test1/test2/test3
Copy the code

cpmv

Cp is short for copy. It is used to copy files or directories.

[parallels@centos-7 Desktop]$ cp test.txt test_copy.txt
Copy the code

Cp -r is used to copy a directory.

Mv is short for move, used to move files or directories.

[parallels@centos-7 Desktop]$ mv test.txt test1/
Copy the code

Mv can also be used to rename files.

[parallels@centos-7 Desktop]$ mv test_copy.txt test.txt
Copy the code

rm

Rm is used to delete files or directories.

[parallels@centos-7 Desktop]$ rm test.txt
Copy the code

You can delete a directory by running the rm -r command.

[parallels@centos-7 Desktop]$ rm -r test.txt
Copy the code

Rm -f forcibly deletes the file.

link

Next we’ll look at links in Linux.

In Linux, file names and file contents are not stored together, but separately.

Hard links

Hard links cause two file names to point to the same piece of file content. Modifying file1 is equivalent to modifying File2.

To create a hard link:

[parallels@centos-7 Desktop]$ ln test.txt test_hard.txt
Copy the code

Note that when a file is deleted, the file name is deleted. Therefore, deleting test_hard. TXT does not delete test.txt.

Hard links are not used much.

Soft links

Soft links are equivalent to shortcuts in Windows.

If you delete file2, you have no effect on file1, but if you delete file1, file2 becomes a “dead link”.

Create a soft link as follows:

[parallels@centos-7 Desktop]$ ln test.txt test_soft.txt
Copy the code

permissions

Many users can be created in Linux. To ensure system security, each user has different permissions.

Common users can use su to switch to root.

You are not advised to log in as the root user because the user has too many permissions.

The user

You can only create a user as root.

Useradd can add new users. A new directory Tom is created under the /home directory to represent the home directory of the Tom user.

[root@centos-7 Desktop]# useradd tom
[root@centos-7 Desktop]# ls /home/
parallels  tom
Copy the code

You can change the password by passwd.

[root@centos-7 Desktop]# passwd Tom Change the password of user Tom. New password: Re-enter the new password: passwd: All authentication tokens have been updated successfully.Copy the code

Userdel is used to delete a user.

[root@centos-7 Desktop]# userdel -r tom
Copy the code

The -r argument causes Tom’s home directory to be deleted as well.

group

In Linux, each user belongs to a group. If you do not set a group for a user, a group with the same name as the user is created by default and the user is assigned to this group.

Run the groupadd command to create a group.

[root@centos-7 ~]# groupadd friends
Copy the code

You can run the usermod -g command to modify the user group.

[root@centos-7 ~]# usermod -g friends tom
Copy the code

You can use the groups command to view the group to which a user belongs.

[root@centos-7 ~]# groups tom
tom : friends
Copy the code

You can use the usermod -g command to add users to multiple groups.

[root@centos-7 ~]# usermod -G friends,family tom
[root@centos-7 ~]# groups tom
tom : family friends
Copy the code

You can use the usermod-ag command to add a group to a user.

[root@centos-7 ~]# usermod -aG lover tom
[root@centos-7 ~]# groups tom
tom : family friends lover
Copy the code

Use groupdel to delete groups.

[root@centos-7 ~]# groupdel lover
[root@centos-7 ~]# groups tom
tom : family friends
Copy the code

Modify the owner and group of a file

Chown, short for change Owner, is used to change the owner of a file and requires root to run.

[root@centos-7 ~]# chown tom test.txt
Copy the code

Chown -r is used to recursively change the owner of a file or directory.

CHGRP is short for change group. It is used to change the group of files.

[root@centos-7 ~]# chgrp friends test.txt
Copy the code

Modify the permissions

As mentioned earlier, LS-L lists the details of the file, including permissions.

The first letter d indicates that this is a table of contents. The corresponding L represents a link and – represents a file.

R indicates that read, w indicates that write, x indicates that execute, and – indicates that there is no corresponding permission.

The permissions of the owner, group users, and other users on the file (or directory) are grouped together.

Linux defines a number for each permission.

Therefore, the read and write permission is 4 + 2 = 6. 6 indicates that you have read and write permission.

File permissions can be changed using chmod:

[root@centos-7 Desktop]# chmod 755 test.txt
Copy the code

The preceding command indicates that the owner has the RWX permission, the users in the group have the R-x permission, and other users have the R-x permission.

Use chmod -r to recursively modify permissions.

repositories

CentOS typically uses YUM to manage software packages.

For yum, see the Linux yum command.

To find the

locate

Locate is used for quick lookup.

[root@centos-7 Desktop]# locate test.txt
/home/parallels/Desktop/test.txt
/home/parallels/Desktop/test1/test.txt
Copy the code

The newly created file is not included in locate. You can update the database with updatedb:

[root@centos-7 Desktop]# updatedb
Copy the code

find

The find command can also be used for lookups, but more powerful.

Format:

The second parameter, “target to find,” is required.

Such as:

[root@centos-7 Desktop]# find /home -name test.txt
/home/parallels/Desktop/test1/test.txt
/home/parallels/Desktop/test.txt
Copy the code

For more uses of find, see:

Linux find command description

Linux find command: find files in a directory.

grep

Grep stands for Globally Search a Regular Expression and Print.

The basic uses of grep are:

Such as:

[root@centos-7 Desktop]# grep path /etc/profile
pathmunge () {
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
unset -f pathmunge 
Copy the code

The command above looks for path in /etc/profile. Using the -i argument ignores case, the -n argument shows line numbers, the -v argument shows lines where text is not present, and the -r argument recursively looks up.

Using the -e argument uses a regular expression:

[root@centos-7 Desktop]# grep -E ^[Pp]ath /etc/profile
pathmunge () {
Copy the code

sort

The sort command is used to sort file contents.

[root@centos-7 Desktop]# cat test.txt Tom jack jemmy Marry Lucky smith [root@centos-7 Desktop]# sort test.txt jack jemmy  Lucky Marry smith TomCopy the code

The sort command doesn’t really change the contents of the file. You can run -o to write the sorted content to a new file.

[root@centos-7 Desktop]# sort -o test_sorted.txt test.txt 
[root@centos-7 Desktop]# cat test_sorted.txt 
jack
jemmy
Lucky
Marry
smith
Tom
Copy the code

-r arguments are sorted in reverse order, -r is sorted randomly, and -n is used to sort numbers.

wc

Wc is short for Word count and is used for file statistics.

[root@centos-7 Desktop]# wc test.txt 
 6  6 33 test.txt
Copy the code

uniq

Uniq Deletes duplicate content from a file.

[root@centos-7 Desktop]# cat repeat.txt 
Tom
Tom
Jack
Tom
Jack
Smith
[root@centos-7 Desktop]# uniq repeat.txt 
Tom
Jack
Tom
Jack
Smith
Copy the code

Uniq can only delete consecutive duplicate lines and does not change the original file.

redirect

You can redirect the output of the command to a file. > overwrites the contents of the file.

[root@centos-7 Desktop]# ls / > test.txt 
Copy the code

You can also redirect the output of a command to a file, but >> appends it at the end of the file.

The pipe

A pipe can take the output of one command as the input of another.

[root@centos-7 Desktop]# ls | sort
Parallels Shared Folders
repeat.txt
test1
test_sorted.txt
test.txt
Copy the code

The above command takes the output of ls as the input to sort.

process

pstop

Ps and TOP list running processes.

The difference is that the processes listed in PS are not updated over time and are static. Ps-ef lists all the processes.

The top command lists processes that change dynamically.

The kill command is used to end a process.

[root@centos-7 Desktop]# kill 671
Copy the code

671 is the process id.

Halt is used to shut down the machine, and reboot is used to restart it.

&

The & can switch the process to the background by putting the & at the end of the command.

[parallels@centos-7 Desktop]$ ls &
[1] 4175
Copy the code

nohup

[root@centos-7 Desktop]# nohup ls nohup: ignore input and append output to "nohup.out"Copy the code

Ctrl + Z moves the process to background pause. The bg command will rerun it in the background. The fg command converts the background process to foreground.

The compression

Let’s start with the concepts of packaging and compression.

Packaging is to archive multiple files into a single file, and compression is to compress a large file into a small file.

In Linux, multiple files are first packaged into a single file using tar, and then compressed using gzip or bzip2.

Tar-cvf package:

[root@centos-7 Desktop]# tar -cvf archive.tar test_copy.txt test.txt
test_copy.txt
test.txt
Copy the code

You can also package a directory:

[root@centos-7 Desktop]# tar -cvf test_archive.tar test1
test1/
test1/test2/
test1/test2/test3/
test1/test.txt
Copy the code

Unpack the archive:

[root@centos-7 Desktop]# tar -xvf archive.tar 
test_copy.txt
test.txt
Copy the code

Gzip compression:

[root@centos-7 Desktop]# gzip archive.tar 
Copy the code

Gunzip unzip:

[root@centos-7 Desktop]# gunzip archive.tar.gz
Copy the code

(To be continued…)