Linux file and directory management - Note 5

1. Directory and path

1.1 Operations related to the directory

The command to change the directory is cd, and the following rooms are relatively special directory operations:

.  代表此层目录
.. 代表上一层目录
-  代表前一个工作目录
~  代表 目前使用者身份所在的主文件夹
~  account 代表 account 这个使用者的主文件夹 (account是个帐号名称)

Let's talk about some common commands for processing directories:

 c :        变换目录
 pwd :      显示目前的目录
 mkdir:     创建一个新的目录
 rmdir:     删除一个空的目录
 cd (change directory, 变换目录)

c d \mathrm{cd} cd is the abbreviation of Change Directory, which is an instruction to change the working directory. Note that there is a space between the directory name and the cd command.

pwd is the abbreviation of Print Working Directory, which is the instruction to display the current directory.

If you want to create a new directory, use mkdir (make directory)! However, by default, the directories you need have to be created layer by layer! For example: If you want to create a directory as /home/bird/testing/test1, then you must first have /home and then /home/bird, and then /home/bird/testing must exist before you can create /home/bird /testing/test1 this directory! If there is no /home/bird/testing, there is no way to create the test1 directory!

However, now there is an easier and more effective way! That is to add the -p option! You can directly issue: "mkdir -p /home/bird/testing/test1", then the system will automatically help you create /home, /home/bird, /home/bird/testing in order! And, if the directory already exists, the system will not display an error message!

In addition, there is a place where you must have a concept first, and that is the place of "default permissions". We can use -m to enforce permissions related to a new directory. For example, in the above table, we give -m 711 to give the new directory drwx–x–x permissions. However, if the -m option is not given, what are the default new directory permissions? This is related to umask, which we will introduce later in this chapter.

insert image description here
rmdir (delete "empty" directory), only "delete empty" directory

insert image description here
What if you want to kill everything in the directory? ! At this time, you must use "rm -r test"! Still, using rmdir is less dangerous! You can also try adding the -p option to delete the upper directory!

1.2 Variables related to the executable file path: $PATH

Now, please issue "echo $PATH" to see which directories are defined? echo means "display, print out", and the $ added in front of PATH means that what follows is a variable, so the current PATH will be displayed!

The content of the variable PATH (must be uppercase) is composed of a bunch of directories, each directory is separated by a colon (:), and each directory has a "order".

And from the above few examples, we can also know a few things:

  • Users with different identities have different default PATHs, and the default commands that can be executed at will are also different (such as root and dmtsai);
  • PATH can be modified;
  • Using an absolute path or a relative path to directly specify the file name of a command to execute is more correct than searching PATH;
  • The instruction should be placed in the correct directory, so that it will be more convenient to execute;
  • This directory (.) is best not to be placed in PATH.

2. File and directory management

2.1 View files and directories: ls

insert image description here

The following are some commonly used examples, try them out in practice:
insert image description here

insert image description here

insert image description here

2.2 Copy, delete and move: cp, rm, mv

To copy a file, please use the cp (copy) command~ However, the cp command can be used for many purposes~ In addition to simple copying, you can also create a link file (that is, a shortcut), and compare the old and new of the two files And update it, and copy the entire directory and so on! As for moving directories and files, use mv (move). This command can also be used directly for renaming (rename) actions! As for removal? That is the rm (remove) command~ Let's take a look at it first~

cp (copy file or directory)

insert image description here

The copy (cp) command is very important. Different identities will have different results when executing this command, especially the -a, -p options. For different identities, the difference is very large! In the following exercises, some identities are root and some identities are general accounts (I use the dmtsai account here), please pay special attention to the differences in identities during the exercises!
insert image description here

rm (remove a file or directory)

insert image description here

mv (move files and directories, or rename them)
insert image description here

3. Document content review

What should we do if we want to check the content of a file? Here are quite a few interesting commands to share: The most commonly used commands to display file content can be said to be cat, more and less! Also, what if we want to view a very large file (hundreds of megabytes), but we only need a few lines in the backend? hehe! Use tail, in addition, the command tac can also achieve this purpose! Well, let’s talk about the purpose of each command!

cat  由第一行开始显示文件内容
tac  从最后一行开始显示,可以看出 tac 是 cat 的倒着写!
nl   显示的时候,顺道输出行号!
more 一页一页的显示文件内容
less 与 more 类似,但是比 more 更好的是,他可以往前翻页!
head 只看头几行
tail 只看尾巴几行
od   以二进制的方式读取文件内容!

You can use the commands cat/tac/nl to directly check the content of a file!
cat (concatenate)
insert image description here

hey-hey! Is there a "cat" command in Linux? oh! No, cat is the abbreviation of Concatenate (continuous), the main
function is to print out the contents of a file on the screen continuously! For example, in the above example, we print /etc/issue
! If you add -n or -b, the line number will be added in front of each line!

4. Command and file search

4.1 Searching for command file names

We know that in the terminal mode, you can know how many commands the user can issue by entering the [tab] key twice in a row. Do you know where the full filenames of these instructions are placed? For example, where is the commonly used command ls? Just use which or type to find it!
which (looks for "executable")

insert image description here

4.2 File name search

Let's talk about how to search for files! There are also excellent search commands under Linux! Usually find is not very commonly used! Because of the slow speed, it is also very difficult to manipulate the hard disk! Generally, we use whereis or locate to check first. If we really can't find it, we use find to search! why? Because whereis only finds the files under some specific directories in the system, and locate uses the database to search for file names. Of course, the two are quite fast, and there is no actual search for the file system status in the hard disk, which saves time!
whereis (find filenames by some specific directory)
insert image description here

Guess you like

Origin blog.csdn.net/wokaowokaowokao12345/article/details/127518101