[2] Basic commands commonly used in Linux

Catalog management

Absolute path and relative path

We know that the Linux directory structure is a tree structure, and the top directory is the root directory /.

Other directories can be added to the tree by mounting, and they can be removed by unmounting.

Before starting this tutorial, we need to know what an absolute path and a relative path are.

Absolute path:

The path is written from the root directory /, for example: /usr/share/doc this directory.

relative path:

The path is not written from /. For example, when going from /usr/share/doc to /usr/share/man, you can write: cd …/man This is the way of writing relative paths!

Common commands for handling directories

Next, let's look at a few common commands for handling directories:

  • ls: list directories
  • cd: switch directory
  • pwd: display the current directory
  • mkdir: create a new directory
  • rmdir: delete an empty directory
  • cp: copy files or directories
  • rm: remove files or directories
  • mv: Move files and directories, or modify the names of files and directories

You can use man [command] to view the documentation of each command, such as man cp.

ls (list directories)

Among Linux systems, the ls command is probably the most frequently run.

grammar:

[root@www ~]# ls [-aAdfFhilnrRSt] 目录名称

Options and parameters:

  • -a: All files, together with hidden files (files starting with .) are listed together (commonly used)
  • -l: List long data strings, including file attributes and permissions, etc.; (commonly used)

List all files in the directory (including attributes and hidden files)

[root@www ~]# ls -al ~

cd (change directory)

cd is the abbreviation of Change Directory , which is a command used to change the working directory.

grammar:

cd [相对路径或绝对路径]

test:

# 切换到用户目录下
[root@kuangshen /]# cd home  

# 使用 mkdir 命令创建 kuangstudy 目录
[root@kuangshen home]# mkdir kuangstudy

# 进入 kuangstudy 目录
[root@kuangshen home]# cd kuangstudy

# 回到上一级
[root@kuangshen kuangstudy]# cd ..

# 回到根目录
[root@kuangshen kuangstudy]# cd /

# 表示回到自己的家目录,亦即是 /root 这个目录
[root@kuangshen kuangstudy]# cd ~

Next, you should be able to understand the cd command well after a few more operations.

pwd (display the current directory)

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

[root@kuangshen kuangstudy]#pwd [-P]

Options and parameters: -P : Display the actual path instead of using the link path.

test:

# 单纯显示出目前的工作目录
[root@kuangshen ~]# pwd
/root

# 如果是链接,要显示真实地址,可以使用 -P参数
[root@kuangshen /]# cd bin
[root@kuangshen bin]# pwd -P
/usr/bin

mkdir (create a new directory)

If you want to create a new directory, then use mkdir (make directory).

mkdir [-mp] 目录名称

Options and parameters:

  • -m: Permission of configuration file! Direct configuration, no need to look at the face of the default permission (umask)~
  • -p: Help you directly create the required directory (including the upper directory) recursively!

test:

# 进入我们用户目录下
[root@kuangshen /]# cd /home

# 创建一个 test 文件夹
[root@kuangshen home]# mkdir test

# 创建多层级目录
[root@kuangshen home]# mkdir test1/test2/test3/test4
mkdir: cannot create directory ‘test1/test2/test3/test4’:
No such file or directory  # <== 没办法直接创建此目录啊!

# 加了这个 -p 的选项,可以自行帮你创建多层目录!
[root@kuangshen home]# mkdir -p test1/test2/test3/test4

# 创建权限为 rwx--x--x 的目录。
[root@kuangshen home]# mkdir -m 711 test2
[root@kuangshen home]# ls -l
drwxr-xr-x 2 root root  4096 Mar 12 21:55 test
drwxr-xr-x 3 root root  4096 Mar 12 21:56 test1
drwx--x--x 2 root root  4096 Mar 12 21:58 test2

rmdir (can only delete empty directories)

grammar:

rmdir [-p] 目录名称

Options and parameters: **-p: ** Delete along with the upper level "empty" directory

test:

# 看看有多少目录存在?
[root@kuangshen home]# ls -l
drwxr-xr-x 2 root root  4096 Mar 12 21:55 test
drwxr-xr-x 3 root root  4096 Mar 12 21:56 test1
drwx--x--x 2 root root  4096 Mar 12 21:58 test2

# 可直接删除掉,没问题
[root@kuangshen home]# rmdir test

# 因为尚有内容,所以无法删除!
[root@kuangshen home]# rmdir test1
rmdir: failed to remove ‘test1’: Directory not empty

# 利用 -p 这个选项,立刻就可以将 test1/test2/test3/test4 依次删除。
[root@kuangshen home]# rmdir -p test1/test2/test3/test4

Note: This rmdir can only delete empty directories, you can use the rm command to delete non-empty directories, we will later!

cp (copy file or directory)

grammar:

[root@www ~]# cp [-adfilprsu] 来源档(source) 目标档(destination)
[root@www ~]# cp [options] source1 source2 source3 .... directory

Options and parameters:

  • **-a: ** Equivalent to the meaning of -pdr, as for pdr, please refer to the following instructions; (commonly used)
  • **-p: ** Copy together with the attributes of the file instead of using the default attributes (used for backup);
  • **-d: ** If the source file is a link file, copy the link file attributes instead of the file itself;
  • **-r: ** Recursive continuous copy, used for directory copy behavior; (commonly used)
  • **-f: ** means force. If the target file already exists and cannot be opened, remove it and try again;
  • **-i: ** If the destination file (destination) already exists, the action will be asked first when overwriting (commonly used)
  • **-l: ** Create a hard link link file instead of copying the file itself.
  • **-s: ** Copy into a symbolic link (symbolic link), which is a "shortcut" file;
  • **-u: ** Only upgrade the destination if the destination is older than the source!

test:

# 找一个有文件的目录,我这里找到 root目录
[root@kuangshen home]# cd /root
[root@kuangshen ~]# ls
install.sh
[root@kuangshen ~]# cd /home

# 复制 root目录下的install.sh 到 home目录下
[root@kuangshen home]# cp /root/install.sh /home
[root@kuangshen home]# ls
install.sh

# 再次复制,加上-i参数,增加覆盖询问?
[root@kuangshen home]# cp -i /root/install.sh /home
cp: overwrite ‘/home/install.sh’? y #如果文件重复,n不覆盖,y为覆盖

rm (remove files or directories)

grammar:

rm [-fir] 文件或目录

Options and parameters:

  • -f: means force, ignore files that do not exist, and no warning message will appear;
  • -i: Interactive mode, the user will be asked whether to act before deleting
  • -r: delete recursively! The most commonly used in the deletion of the directory! This is a very dangerous option! ! !

test:

# 将刚刚在 cp 的实例中创建的 install.sh删除掉!
[root@kuangshen home]# rm -i install.sh
rm: remove regular file ‘install.sh’? y
# 如果加上 -i 的选项就会主动询问喔,避免你删除到错误的档名!

#rm -rf /  #尽量不要在服务器上使用,系统中所有的文件都会被删除,删库跑路就是这么操作的!

mv (Move files and directories, or modify names)

grammar:

[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory

Options and parameters:

  • -f: Force means that if the target file already exists, it will be overwritten without asking;
  • -i: If the destination file already exists, it will ask whether to overwrite it!
  • -u: If the target file already exists and the source is relatively new, it will be updated (update)

test:

# 复制一个文件到当前目录
[root@kuangshen home]# cp /root/install.sh /home

# 创建一个文件夹 test
[root@kuangshen home]# mkdir test

# 将复制过来的文件移动到我们创建的目录,并查看
[root@kuangshen home]# mv install.sh test    #移动文件
[root@kuangshen home]# ls
test
[root@kuangshen home]# cd test
[root@kuangshen test]# ls
install.sh

# 将文件夹重命名,然后再次查看!
[root@kuangshen test]# cd ..
[root@kuangshen home]# mv test mvtest   #重命名
[root@kuangshen home]# ls
mvtest

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/113100732