Linux file path, cd command, mkdir, rmdir, rm command

Chapter 2 File and Directory Management

2.6 Relative and absolute paths

  • Relative path
    The relative path is relative to the current directory. For example, if I am currently under /root, then to indicate /root/213/, you can directly ls 213/
  • Absolute paths
    Absolute paths start at / (also known as the root directory), such as /usr, /etc/X11. If a path starts at /, it must be absolute

pwd command

pwd = Print Working Directory
Role:  Display the path name of the working directory
Syntax:  pwd [options]
Common parameters:
generally without any parameters.
If the directory is a link:
Format: pwd -P Display the actual path instead of using the link path.

eg1:  View the full path to the default (current) working directory

[root@3 ~]# pwd
/root

eg2:  View the specified folder

[root@3 ~]# cd /home/ 切换到home文件夹
[root@3 home]# pwd
/home

eg3:  When a directory has a link, pwd -P displays its actual path

# cd /etc/init.d
# pwd
/etc/init.d
# pwd -P
/etc/rc.d/init.d

 

2.7 cd command

The cd (change directory) command is used to change the directory where the user is located. If nothing is followed, it will go directly to the root directory of the current user. We use the root account for the experiment, so after running cd, it will enter the The root directory /root of the root account. Followed by the directory name, it will directly switch to the specified directory:

[root@3 ~]# cd /tmp
[root@3 tmp]# pwd
/tmp
[root@3 tmp]# cd
[root@3 ~]# pwd
/root

cd=change directory
cd - Enter the last directory, similar to the "alternate" command of the remote control
cd ~ Enter the home directory of the current directory
cd  .  The current directory
cd  ..  Enter the previous directory
cd /var/log/ Enter the specified directory (log)
concept:
home directory: it is the user directory where all users other than root are stored, and all their own files are placed in the home directory.
Root directory: The uppermost directory of the logical drive, the home directory is included in the root directory.

2.8 Creating and deleting directories

  • The mkdir command
    mkdir=make directory
    syntax:  mkdir [-mp] [directory name], where -m and -p are options.
    -m  =mod specifies the permissions of the directory
    -p  forces the creation of the directory or the creation of cascading directories

         -v: visualize

  • Create cascading directories
# mkdir /tmp/test/123  在tmp目录下的test(不存在)目录创建123目录(即创建级联目录)
mkdir:无法创建目录‘/tmp/test/123’:没有那个文件或目录
# mkdir -p /tmp/test/123  在tmp目录下的test(不存在)目录创建123目录(即创建级联目录)
# ls /tmp/  查看tmp目录下内容
test 123    创建成功

Force directory creation

# ls -ld /tmp/test/123  查看目录
drwxr-xr-x. 2 root root 4096 5月 9 19:10 /tmp/test/123
# mkdir /tmp/test/123
mkdir:无法创建目录‘/tmp/test/123’:文件已存在
# mkdir -p /tmp/test/123
# ls -ld /tmp/test/123 
drwxr-xr-x. 2 root root 4096 5月 9 19:10 /tmp/test/123

Another benefit of this option is that when you create a directory that already exists, you won't get an error.

-m: specify permission

 

Command: rmdir

rmdir=remove directory (it removes empty directories)

Syntax: rmdir [directory name] (here can be followed by multiple directories, separated by spaces)
Defect:  rmdir has the same option '-p' as mkdir, which can also cascade delete a large list of directories, but in the cascaded directory It is not easy to use when there are directories or files in one of the directories.

# ls -ld /tmp/test/123
drwxr-xr-x. 2 root root 4096 5月 9 19:10 /tmp/test/123
# rmdir /tmp/test/ 
rmdir: 删除 '/tmp/test/' 失败: 目录非空
# rmdir /tmp/test/123
# ls -ld /tmp/test/ 
drwxr-xr-x. 2 root root 4096 5月 9 19:10 /tmp/test/

The conclusion is that 'rmdir' can only delete empty directories. Even with the '-p' option, it can only delete a series of empty directories. It can be seen that this command has great limitations, and it can be used occasionally.

2.9 rm command

rm=remove
The rm command can delete one or more files or directories in a directory, and can also delete all files and subdirectories of a directory and its subordinates. For linked files, just delete the entire linked file, leaving the original file unchanged.
Syntax:  rm (option) (parameter)
option:
-r option  for deleting a directory
-f  forcibly delete a file or directory
-d  directly delete the hard link data of the directory to be deleted to 0, delete the directory
-i  delete the existing file Or ask the user before the directory
-v  =visual shows the detailed execution process of the command
Note:  -rf can be used in combination. Be extra careful with the rm command, because once a file is deleted, it cannot be recovered. Therefore, before deleting the file, it is best to look at the contents of the file again to determine whether it is really necessary to delete it. The rm command can be used with the -i option, which is especially useful when deleting multiple files using file extension characters. Using this option, you will be asked to determine whether you want to delete one by one. At this point, you must type y and press Enter to delete the file. If you just press Enter or other characters, the file will not be deleted.

-r

[root@3 ~]# mkdir -p /tmp/text/123
[root@3 ~]# rm -r /tmp/text/123
rm:是否删除目录 "/tmp/text/123"?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324437907&siteId=291194637