Linux基本常用命令

cd :切换目录

绝对路径和相对路径

绝对路径

Linux中一切皆文件,一切的文件都在 /(根)目录下
路径写法:由根目录写起 / 例如:/usr/local

[root@LuoKing /]# ls #列出目录 bin   dev  home  lib64       media  opt   root  sbin  sys  usr boot  etc  lib   lost+found  mnt    proc  run   srv   tmp  var [root@LuoKing /]# cd usr #相对路径写法 [root@LuoKing usr]# ls bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp [root@LuoKing usr]# cd local  #相对路径写法 [root@LuoKing local]# pwd /usr/local 

相对路径

../ : 上一级目录

[root@LuoKing /]# cd usr [root@LuoKing usr]# ls bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp [root@LuoKing games]# cd ../ [root@LuoKing usr]# pwd /usr 

./ : 当前目录

[root@LuoKing games]# cd ./ [root@LuoKing games]# pwd /usr/games 

例如,想从/usr/local转换到/usr/games目录下,我们可以使用 cd ../games

[root@LuoKing usr]# ls bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp [root@LuoKing usr]# cd local [root@LuoKing local]# pwd /usr/local [root@LuoKing local]# cd ../games #使用相对路径切换路径 [root@LuoKing games]# pwd /usr/games 

ls列出目录

[root@LuoKing /]# ls bin   dev  home  lib64       media  opt   root  sbin  sys  usr boot  etc  lib   lost+found  mnt    proc  run   srv   tmp  var 

选项和参数

  • -a :全部文件,包括隐藏文件
  • -l :长数据列出,包含文件的属性与权限等等
[root@LuoKing /]# ls -al total 68 dr-xr-xr-x. 18 root root  4096 Apr 14 16:33 . dr-xr-xr-x. 18 root root  4096 Apr 14 16:33 .. -rw-r--r--   1 root root     0 Sep 14  2020 .autorelabel lrwxrwxrwx.  1 root root     7 Sep 14  2020 bin -> usr/bin dr-xr-xr-x.  5 root root  4096 Nov 30 15:19 boot drwxr-xr-x  19 root root  2980 Apr 13 21:48 dev drwxr-xr-x. 77 root root  4096 Apr 13 21:46 etc drwxr-xr-x.  3 root root  4096 Apr 14 16:33 home lrwxrwxrwx.  1 root root     7 Sep 14  2020 lib -> usr/lib lrwxrwxrwx.  1 root root     9 Sep 14  2020 lib64 -> usr/lib64 drwx------.  2 root root 16384 Sep 14  2020 lost+found drwxr-xr-x.  2 root root  4096 Apr 11  2018 media drwxr-xr-x.  2 root root  4096 Apr 11  2018 mnt drwxr-xr-x.  2 root root  4096 Apr 11  2018 opt dr-xr-xr-x  84 root root     0 Apr 13 21:46 proc dr-xr-x---.  5 root root  4096 Apr 14 16:42 root drwxr-xr-x  24 root root   640 Apr 13 21:46 run lrwxrwxrwx.  1 root root     8 Sep 14  2020 sbin -> usr/sbin drwxr-xr-x.  2 root root  4096 Apr 11  2018 srv dr-xr-xr-x  13 root root     0 Apr 14 21:41 sys drwxrwxrwt.  8 root root  4096 Apr 14 03:31 tmp drwxr-xr-x. 13 root root  4096 Sep 14  2020 usr drwxr-xr-x. 20 root root  4096 Apr 13 21:03 var 

pwd 显示目前所在的目录

pwd 是 Print Working Directory 的缩写,也就是显示目前所在目录的命令。

不同颜色代表不同的文件类型
image

[root@LuoKing /]# ls bin   dev  home  lib64       media  opt   root  sbin  sys  usr boot  etc  lib   lost+found  mnt    proc  run   srv   tmp  var [root@LuoKing /]# cd bin # bin 是链接, [root@LuoKing bin]# pwd  #这里显示的是链接路径,并非真实路径 /bin [root@LuoKing bin]# pwd -P # 使用参数 -P(注意是大写)可以显示真实路径 /usr/bin 

mkdir 创建新目录(make dirctory 目录)

  • -p:允许一次性创建多级目录
  • -m:配置文件的权限直接配置,不需要看默认权限 (umask)
[root@LuoKing home]# mkdir text1 #创建一个普通的目录 [root@LuoKing home]# ls Luoking  text1  text1.txt 

一次性创建多级目录

[root@LuoKing home]# mkdir text2/text3/text4 #不加参数,创建不了 mkdir: cannot create directory ‘text2/text3/text4’: No such file or directory [root@LuoKing home]# mkdir -p text2/text3/text4 # 加了参数创建成功 [root@LuoKing home]# ls Luoking  text1  text1.txt  text2 

rmdir (删除空的目录)

[root@LuoKing home]# ls Luoking  text1  text1.txt  text2 [root@LuoKing home]# rmdir text1 #简单的移除空目录 [root@LuoKing home]# ls Luoking  text1.txt  text2 

选项与参数:-p :连同上一级『空的』目录也一起删除

[root@LuoKing home]# rmdir text2/text3/text4 #没有带参数,只删除了text4 [root@LuoKing home]# ls Luoking  text1.txt  text2 [root@LuoKing home]# cd text2 [root@LuoKing text2]# ls text3 [root@LuoKing text2]# cd text4 -bash: cd: text4: No such file or directory 

我们带上-p参数

[root@LuoKing home]# ls Luoking  text1.txt  text2 [root@LuoKing home]# rmdir -p text2/text3 #成功将text2的空目录也删除 [root@LuoKing home]# ls Luoking  text1.txt 

cp (复制文件或目录)

选项与参数:

  • -a:相当於 -pdr 的意思,至於 pdr 请参考下列说明;(常用)

  • -p:连同文件的属性一起复制过去,而非使用默认属性(备份常用);

  • -d:若来源档为连结档的属性(link file),则复制连结档属性而非文件本身;

  • -r:递归持续复制,用於目录的复制行为;(常用)

  • -f:为强制(force)的意思,若目标文件已经存在且无法开启,则移除后再尝试一次;

  • -i:若目标档(destination)已经存在时,在覆盖时会先询问动作的进行(常用)

  • -l:进行硬式连结(hard link)的连结档创建,而非复制文件本身。

  • -s:复制成为符号连结档 (symbolic link),亦即『捷径』文件;

  • -u:若 destination 比 source 旧才升级 destination

[root@LuoKing home]# ls Luoking  text1.txt  text2.txt [root@LuoKing home]# cd Luoking # [root@LuoKing Luoking]# ls #展示Luoking目录中没有文件 [root@LuoKing Luoking]# cd ../ [root@LuoKing home]# ls Luoking  text1.txt  text2.txt [root@LuoKing home]# cp text1.txt Luoking #将text1.txt复制到Luoking目录中 [root@LuoKing home]# cd Luoking/ [root@LuoKing Luoking]# ls text1.txt 

当然也可以一次复制多个文件到目录中

[root@LuoKing home]# cp text1.txt text2.txt Luoking #一次复制多个文件到目录中 cp: overwrite ‘Luoking/text1.txt’? y [root@LuoKing home]# cd Luoking/ [root@LuoKing Luoking]# ls text1.txt  text2.txt 

rm(移除文件或目录)

选线或参数

  • -f:就是force的意思,忽略不存在的文件,不会出现警告消息
  • -i:互动模式,会有消息提示
  • -r:递归删除啊!最常用在目录的删除了!这是非常危险的选项!!!
[root@LuoKing Luoking]# ls text1.txt  text2.txt [root@LuoKing Luoking]# rm text1.txt # 默认 -i rm: remove regular empty file ‘text1.txt’? y [root@LuoKing Luoking]# ls text2.txt 

添加 -i 参数

[root@LuoKing Luoking]# rm -i text2.txt  rm: remove regular empty file ‘text2.txt’? y [root@LuoKing Luoking]# ls 

mv(移动文件或目录,想当windows中的剪切。还有重命名功能)

选项与参数:

  • -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;

  • -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!

  • -u :若目标文件已经存在,且 source 比较新,才会升级 (update)

剪切
[root@LuoKing home]# ls Luoking  text1.txt  text2.txt [root@LuoKing home]# mv text1.txt Luoking  [root@LuoKing home]# ls Luoking  text2.txt 
重命名(文件和目录都可以)
[root@LuoKing home]# ls Luoking  text1.txt  text2.txt [root@LuoKing home]# mv text1.txt Luoking [root@LuoKing home]# ls Luoking  text2.txt [root@LuoKing home]# mv Luoking Zhiking [root@LuoKing home]# ls text1.txt  Zhiking [root@LuoKing home]# ls text1.txt  Zhiking [root@LuoKing home]# mv Zhiking text1.txt # 默认有提示模式 mv: overwrite ‘text1.txt’? y