Linux | Create | Delete | View | Detailed explanation of basic naming

Linux | Create | Delete | View | Detailed explanation of basic naming

Preface

  • This course teaches Linux system programming. You may ask where Linux comes from? How did it develop? Here is a brief introduction to the development history of Linux. When talking about Linux, we have to start with UNIX.

The following content comes fromWikipedia

Linux is a free and open source UNIX-like operating system. The operating system's kernel was first released by Linus Torvalds on October 5, 1991, and after adding user space applications, it became the Linux operating system. Linux is also the most famous example of free software and open source software development. As long as they follow the GNU General Public License (GPL), any individual or institution can freely use all the underlying source code of Linux, and can also modify and redistribute it freely. Most Linux systems also include programs like X Window that provide a GUI. With the exception of a few experts, most people just jump into a Linux distribution without choosing every component or setting it up themselves.

  • We won’t introduce other concepts. You can search them on Baidu and Bing. Interested students can search by themselves~~
  • Let’s start our main topic~~

1. Install Linux

1.1 Method 1: Cloud server method

  • We can use the cloud server to purchase or白嫖. This student is very good at free prostitution. We can open Alibaba Cloud Student Rights Official Website a>Carry out student certification, everyone will understand next~~
  • After purchasing the cloud server, we need to install Centos7 to learn~~

Insert image description here

1.2 Method 2: Virtual machine method

  • Here we can openthe official website to download VMware Workstation 17 Pro. Everyone knows the secret key. It is online...

  • Next, after we have installed it, we start to install our virtual machine. Then the question arises, where is the mirror?

  • Here we need to open ClearHua University Open Source Software Mirror Station to find the mirror~~

  • We usually just download this

Insert image description here

  • There is no need to go into details about the installation method here. There are many methods online~~

We will use cloud services for learning below. Cloud services are more convenient~~


  • Let’s start our main topic!
  • Learn the first command~~

二、ls

Syntax: ls [options][directory or file]
Function: For directories, this command List all subdirectories and files in this directory. For files, the file name is listed along with other information.
Common options:

-a List all files in the directory, including implicit files starting with .
-d displays the directory as a file, rather than displaying the files below it. For example: ls –d specifies the directory
-i outputs the index information of the i node of the file. For example, ls -ai specifies file
-k indicates the size of the file in k bytes. ls –alk Specify file
-l List file details.
-n Replace the name with a numeric UID or GID. (Introducing UID, GID)
-F Attach a character after each file name to indicate the type of the file. "*" indicates an executable ordinary file; "/" indicates a directory; "@" represents
symbolic link; "|" represents FIFOs; "=" represents sockets. (Directory type identification)
-r Sort the directory in reverse order.
-t Sort by time.
-s outputs the size of the file after the l file name. (Size sorting, how to find the largest file in a directory)
-R lists files in all subdirectories. (Recursive)
-1 Only output one file per line.


  • After seeing the above, I have a headache. What should I do if I can’t remember?
    • No need to remember! You will naturally remember it if you use it more later. Let’s use a few basic commands first~~
ls

Insert image description here

  • After we run it, we find that nothing is displayed here. That’s because there are no files or folders in this directory. Let’s create a few first~~
  • Note that there are spaces between commands! ! !
mkdir test
touch test.txt

Insert image description here

  • Proceed againls and you can see that there are already files displayed~~

Insert image description here

  • Some students will say, heretestask what is the color? What does it mean?
  • Let’s ignore it for now and continue reading~~

2.2 ll

  • Next we will learn a new commandll

Insert image description here

  • This command is quite special when you first learn it. Rather than being a command, it is an alias of ls -l, which means it is named after ls -l The name is ll, so what you say is unfounded, how can you prove it?

3. which

  • Execute the following command
which ll
  • Don’t worry about the which command here. We will learn more about it in a moment~~

  • Herealias means an alias, ll='ls -l`, that is, ll is equal to ls -l~~

Insert image description here

  • Let's use which ls again, and we can see that the same thing~~

Insert image description here

  • Back to the topic just now, why are there colors?
  • Pay attention, what we have herell=ls -l --color=auto is It's colored, so how to prove it? --color=auto
alias ls='ls'
  • Here we give it an alias like the one in the picture, but do not specify a color. Once I execute it, it will print out, but without color.

Insert image description here

  • So what should I do if I want to add this color back?
    • It's easy to handle, just create another alias and change it back.
alias ls='ls --color=auto'
  • Run ls again and you will see the color~~

Insert image description here

  • The ls here can also view the folders or files in the specified directory. Let’s demonstrate it.

  • We have created a file herea, we can use the following command to view~~


3.1 ls -ld

ls -ld a
  • Among them-l lists the detailed information of the file, -d displays the directory as a file, rather than displaying the files under it.
  • We can just write it herels -ld and that’s it~~
  • Heremkdir -p We will explain it in detail later, so don’t worry about it here~~

Insert image description here

  • I didn’t go into detail about which here just now, nor did I explain the following paths after which. Next, we will explain them one by one~~

4. pwd

Syntax: pwd
Function: Display the directory where the user is currently located

This command is very simple, just execute it directly

pwd

Insert image description here

  • So what is displayed here?
    • In fact, it is the absolute path of the current user. Then I enter a directory and take a look.

Insert image description here

  • You can see that changes have occurred. This command ends here. Next we learncd

5. cd

In the Linux system, the files and directories on the disk are organized into a directory tree, and each node is a directory or file.

Insert image description here

  • As shown in the picture above, our Linux system is a tree file system. We also used this command just above, cd 文件夹, and entered this file

Syntax: cd directory name
Function: Change the working directory. Change the current working directory to the specified directory.

cd … : Return to the upper directory
cd /home/litao/linux/ : Absolute path
cd …/day02/ : Relative path a>
cd ~: Enter the user's home directory
cd -: Return to the recently visited directory


5.1 cd ..

Let’s look at the first instruction first

cd ..

Let’s first understand that each folder has hidden files, so how to see it? We can check it throughls -al~~


5.2 ls -al

ls -al

Insert image description here

  • When we execute this command, we will find that the files preceded by . are also displayed~~

  • So is the file preceded by , a hidden file?

  • Let's verify it

  • We create a file and a folder here, but each is preceded by.

  • Let’s do it againls -l, and we can see that it cannot be printed, but ls -al can be printed, which once again confirms that this command is Show hidden files~~

Insert image description here


Let’s look at the second command

cd /root/a/b/

Insert image description here

  • Herecd is followed by an absolute path. What is an absolute path?
    • That is, write the path from the root directory
  • There is another concept:Relative path, which means writing from a relative position. So how to write it?
cd c

Insert image description here

  • Here you enter the c directory directly. We have created these directories before, usingmkdir -p a/b/c/d/f, we will not demonstrate them here
  • If you don’t write it here/, you will find this folder from the current directory by default~~

5.3 Reunderstanding commands

  • Next, let's go back and learn about these commands again. We learned the which command earlier and can check the aliases, but there is such a line below it. What is this?

Insert image description here

  • In fact, this is a path, an absolute path. We can enter the bin directory and have this command. Let’s take a look.

  • Let’s look at this ls command

Insert image description here

  • You can see that it is there~~, and other commands are also there. It seems to be a file. It is actually an executable file. We can directly enter this path to execute it.

Insert image description here

  • Here you can see that it will be displayed after we execute it, but there is no color.
  • As we said before, we are actually giving aliases here.
  • We can also add colors~~
/usr/bin/ls --color=auto

Insert image description here

  • You can also see the color, which once again proves that it is an alias~~

  • Let's go back and look at this relative path
  • We just executed thels -al command, and the command we just executed was in an empty folder, so why are there two files, one. and one < What about /span>..?

Insert image description here

  • In fact, this. represents the current path, and .. represents the previous path

5.4 . and ..

  • Here. represents the current path. Let’s take a look at the following demonstration
  • We will discuss the commands involved here in detail in later chapters~~

Insert image description here

  • Here you can see that we use this./ to execute this executable file, which is to find the file from the current path and execute it~~

  • So can I execute this executable program when I enter another directory?

    • Of course it’s possible~~
cd ../a.out

Insert image description here

  • This means to find this filea.out from the upper directory and execute it~~

  • Then cd .. can we return to the previous directory? Let’s verify it~~

Insert image description here

  • We can see that it has been rolled back~~

  • We continue to retreat until we reach the point where we cannot retreat.

Insert image description here

  • You can see that we cannot go back to the position/, which proves that/ is the root directory of the system~~

Insert image description here

  • We also have another concept here, which is the concept of user

  • I am currently using the root user here.rootWhat is the user? >>> means super administrator~~ which means最高permissions

  • Since there are users, there are ordinary users. Ordinary users are in the/homedirectory

  • Here we switch to ordinary users to take a look~~

  • Let’s understand the concept of users here. We will talk about it in detail later~~

Insert image description here

Then thiscd ~ is to enter the home directory ofuser


5.5 whoami

  • Here I will teach you how to query which user is currently logged in
whoami
  • ExistingNormalUsing:

Insert image description here

  • ExistingrootUsing below:

Insert image description here

  • When I once entered a deep folder, I wanted to go back to my home directory. What should I do? I can perform this operation~~

Insert image description here

  • We have entered the price inquiry folder four times, and we need to execute it four times.../
cd ../../../../

Insert image description here

  • So is there a way to directly go back to the home directory at once?
  • That is to execute the following command
cd ~

Insert image description here

  • You can see that you can go directly to your home directory. Isn’t it very convenient~~

There is another one next

cd -
  • What is this?

  • If I want to switch back and forth between two directories, I need to type in the directory manually. There is a good way to solve this trouble. This is actually to directly go back to the directory just now, One-click rewind

Insert image description here

  • Isn’t it very convenient~~

The commands related tocd are here~~


6. touch

Syntax: touch [options]... File...
Function: The touch command parameters can change the document or The date and time of the directory, including access time and change time, or create a new file that does not exist.
Common options:

-a or –time=atime or –time=access or –time=use only changes the access time.
-c or –no-create do not create any documents.
-d uses the specified date and time instead of the current time.
-f This parameter will be ignored and will not be processed. It is only responsible for solving the compatibility problem of the BSD version of the touch command.
-m or –time=mtime or –time=modify only changes the change time.
-r Set the date and time of the specified document or directory to be the same as the date and time of the reference document or directory.
-t uses the specified date and time instead of the current time.

  • To put it simply, touch is to create files. It cannot create directories. To create a directory, there is a command to create a directory. Don’t get confused~~
touch 文件名

Insert image description here

  • There is nothing to say here, just simply create a file, but I will add a command here. I want to view the detailed attributes of this file. How to view it?

6.1 stat

stat 文件名
  • View specific file properties

  • We will discuss the attributes here in detail in later chapters~~

Insert image description here

  • Okay, let's start learning the next command

7. mkdir command [Important]

Syntax: mkdir [options] dirname…
Function: Create a directory named Directory of "dirname"
Common options:

-p, --parents can be a path name. At this time, if some directories in the path do not yet exist, after adding this option, the system will automatically create those directories that do not yet exist, that is, multiple directories can be created at one time;

mkdir 目录
  • The d in front of here means directory. I will talk about the details later~~

Insert image description here

  • What we basically need to use here is the -p command, which is to recursively create multiple directories
  • Let me mention it again, the command must have spaces.
mkdir -p aaa/bbb/ccc/ddd/eee

Insert image description here

  • It’s hard to see if the creation is successful. We can use the tree command.

7.1 tree

tree 目录
  • Some people may not be able to use this tree. It needs to be installed. Execute the following command:
yum install -y tree
  • and then execute
tree ./

Insert image description here

  • Isn’t it very convenient to observe like this~~

  • Then if I execute ittree /, it will execute such a long sequence, "I can't stop at all"

Insert image description here

  • At this time we have to pressctrl + c to forcefully stop execution

Insert image description here


8. rmdir command && rm command (important)

rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is a delete command.
Syntax: rmdir [-p][dirName]
Applicable objects: With All users with operating permissions for the current directory
Function: Delete empty directories
Common options:

  • -p When the subdirectory is deleted, if the parent directory also becomes an empty directory, the parent directory will be deleted together.

The rm command can delete files or directories at the same time
Syntax: rm [-f-i-r-v][dirName/dir]
Applicable objects: All users
Function: Delete files or directories

Common options:

-f Even if the file attribute is read-only (i.e. write-protected), delete it directly
-i Ask for confirmation one by one before deleting
-r Delete Directory and all files under it

  • Once you have added files and folders, you can delete them~~
  • Here we also need to distinguish between deleting files [rm] and folders [rmdir]

8.1 rm

rm 文件名
  • When deleting a file here, you will be prompted whether you want to delete it? Enter n here not to delete. Enter y to delete. The deletion here means real deletion. There is no Windows Recycle Bin or anything like that~~

Insert image description here


8.2 rm -f

  • If you delete a file like this, you will be asked whether you want to delete it every time. Isn’t it very troublesome? We can add an option-f, so that there will be no prompt and you can delete it directly

Insert image description here

  • But this command cannot delete the folder.

Insert image description here

  • Then we have to learn the next command

8.3 is rm

rmdir 文件夹
  • Delete a single folder here. If the folder is empty and contains nothing, it can be deleted.

Insert image description here

  • So this folder is not empty, can it be deleted directly?

Insert image description here

  • So what should we do? You can't just go into the folders and delete them one by one. At this time, you need to use the -r option
  • This -r is recursive deletion
rm -r 名字
  • But when deleting here, there is a prompt whether to delete it. I just learned the -f option. You can directly add the -f option and delete it directly~~< /span>

Insert image description here


8.4 rm -rf

rm -r -f 名字
  • You can delete it directly when you see it~~
  • Hererm -r -f can also be written asrm -rfThey are equivalent

Insert image description here

  • But one thing to note here is that you must not executerm -rf /. This will delete all files in the root directory and the system will crash, so delete the command with caution! ! ! ! ! !

9. man command [Important]

Linux commands have many parameters. It is impossible for us to remember them all. We can get help by checking the online manual. The command to access the Linux man page is man
Syntax: man [options] command

  • -k Search online help based on keywords
    num is only found in chapter num
  • -a displays all chapters, such as man printf. By default, it starts searching from the first chapter and stops when it is known. Use the a option. When pressing q to exit, it will continue to search until all chapters are searched. .

Explain, the manual is divided into 8 chapters
1 is a common command
2 is a system call, such as open, write, etc. ( Through this, you can at least easily find out what header files need to be added to call this function)
3 is a library function, such as printf, fread4 is a special file, that is, various files under /dev Device file
5 refers to the format of the file, such as passwd, which will explain the meaning of each field in the file
6 is reserved for games, and is determined by each game Define it yourself
7 is an attachment and there are some variables, such as global variables such as environ, which are explained here
8 is a command for system management. These commands Can only be used by root, such as ifconfig

  • We can execute the following command
man man
  • After entering this interface, press the up and down keys on the keyboard to scroll up and down~~

Insert image description here

  • To exit, press on the keyboard in English modeq

  • This command is to check which command you want to check, and the help document will be displayed.

  • If I want to view the help documentation of ls, execute the following command:

man ls

Insert image description here

  • What about the functions in C language that I want to view? How to check it?
man printf
  • Some students may report an error when executing
  • We execute the following command
yum install -y man-pages
  • Execute againman printf
  • You can check the usage of this function

Insert image description here

  • So how can I check this library function?
  • We execute the following command
man 3 printf

Insert image description here

  • So what if I want to check out c++?
  • I don’t recommend it here. It is recommended to check it directly oncplusplus website~~

Other serial numbers are not often used, so I won’t talk about them for now~~


10. cp command [Important]

Syntax: cp [options] Source file or directory Target file or directory
Function: Copy file Or directory
**Note: ** The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, then It will copy any previously specified files or directories into this directory. If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear
Common options:

  • -f or --force forcibly copies a file or directory, regardless of whether the destination file or directory already exists
  • -i or --interactive Ask the user before overwriting the file
  • -r recursively processes files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file.
  • -R or --recursive recursive processing, processing files and subdirectories in the specified directory together
  • We can copy a file or directory to another place, butThere cannot be files with the same name in the same directory
cp src dst

Insert image description here

  • Then I want to copy multiple files in a directory, how do I copy them?
  • Can I copy it directly?
cp aaa a

Insert image description here

  • You can see but you can’t~~

10.1 cp -r

  • At this time, you need to add the-r option [recursive processing, processing files in the specified directory and subdirectories together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file]
cp -r aaa a

Insert image description here


11. mv command [Important]

The mv command is the abbreviation of move, which can be used to move files or rename files (move (rename) files). It is a commonly used command in Linux systems and is often used to back up files or directories.
Syntax: mv [options] Source file or directory Target file or directory
Function:

  1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory.
  2. When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (it can also be the source directory name). It will rename the given source file or directory to the given one. Target file name.
  3. When the second parameter is an existing directory name, there can be multiple source files or directory parameters, and the mv command will move all the source files specified by each parameter to the target
    in the directory.

Common options:

  • -f: force means force. If the target file already exists, it will be overwritten directly without asking.
  • -i: If the destination file (destination) already exists, you will be asked whether to overwrite it!
  • This is similar to the previous copy
mv src dst

Insert image description here


11.1 mv -f

  • Copy the file you just copied back. Here we can use the -f option to force the copy~~
mv -f a/test.c ./

Insert image description here


11.2 cat

Syntax: cat [option][file]
Function: View the contents of the target file a>
Common options:

  • -b output line numbers for non-empty lines
  • -n numbers all lines of output
  • -s does not output multiple blank lines
  • The cat command is very simple, let’s take a look
cat 文件名

Insert image description here

11.2.1 cat -n
  • The output has line numbers, and blank lines also have line numbers, and they are still output~~
cat -n test.c

Insert image description here

11.2.2 cat -b
  • Empty lines are not numbered, non-empty lines are numbered~~

Insert image description here

  • So what should I do if I want to output the contents of the file backwards?

11.3 tac

tac 文件名
  • The cat is upside down, so the output is backwards~~

Insert image description here

This article ends here. It is quite long. We will continue writing in the next article. This article probably only covers some creation, deletion, viewing and basic naming of Linux. If it is helpful, remember to like, collect and comment~~ ,thank you all!

Guess you like

Origin blog.csdn.net/2201_76004325/article/details/134451982