系统相关
首页 > 系统相关> > Linux基本常用命令

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

选项和参数

[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 目录)

[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 (复制文件或目录)

选项与参数:

[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(移除文件或目录)

选线或参数

[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中的剪切。还有重命名功能)

选项与参数:

剪切
[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

标签:基本,Luoking,常用命令,text1,LuoKing,Linux,home,txt,root
来源: https://www.cnblogs.com/luoking/p/16146964.html