Linux常用命令

一. 文件操作命令

1. df:显示磁盘使用情况

[root@ecs-x-medium-2-linux-20200307131357 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       41152832 1629712  37426020   5% /
tmpfs             960800       0    960800   0% /dev/shm

2. du:显示目录或文件的大小

[root@ecs-x-medium-2-linux-20200307131357 ~]# du
4       ./.ssh
32      .

3. ls:显示目录

基本使用 ls -l等价于ll

[root@ecs-x-medium-2-linux-20200307131357 /]# ls -l
total 116
dr-xr-xr-x.  2 root root  4096 Oct 15 21:21 bin
dr-xr-xr-x.  4 root root  4096 Oct 15 20:46 boot
drwxr-xr-x   7 root root  4096 Apr 27  2020 CloudResetPwdUpdateAgent
drwxr-xr-x   7 root root  4096 Apr 27  2020 CloudrResetPwdAgent
drwxr-xr-x  17 root root  3480 Oct 20 21:41 dev
drwxr-xr-x. 79 root root  4096 Oct 23 03:44 etc
drwxr-xr-x.  2 root root  4096 Sep 23  2011 home
-rw-r--r--   1 root root   102 May 14 13:01 HostGuardAgent_Linux64_V1.12.50.rpm.sha256
-rwxr-xr-x   1 root root  2052 May 14 13:01 hostguard_setup_config.dat
dr-xr-xr-x. 11 root root  4096 Apr 27  2020 lib
dr-xr-xr-x.  9 root root 12288 Oct 15 21:21 lib64
drwx------.  2 root root 16384 Apr 27  2020 lost+found
drwxr-xr-x.  2 root root  4096 Sep 23  2011 media
drwxr-xr-x.  2 root root  4096 Sep 23  2011 mnt
drwxr-xr-x.  3 root root  4096 Apr 27  2020 opt
dr-xr-xr-x  96 root root     0 Oct 20 21:40 proc
dr-xr-x---.  3 root root  4096 Oct 15 20:46 root
drwxr-xr-x   3 root root  4096 Apr 27  2020 run
dr-xr-xr-x.  2 root root 12288 Oct 15 21:21 sbin
drwxr-xr-x.  2 root root  4096 Apr 27  2020 selinux
drwxr-xr-x.  2 root root  4096 Sep 23  2011 srv
drwxr-xr-x  13 root root     0 Oct 20 21:40 sys
drwxrwxrwt.  4 root root  4096 Oct 29 22:30 tmp
drwxr-xr-x. 13 root root  4096 Apr 27  2020 usr
drwxr-xr-x. 20 root root  4096 Apr 27  2020 var
文件类型:
	-:普通文件(File)
	d:目录文件
	b:块设备文件(Block)
	c:字符设备文件(Character)
	l:符号链接文件(Symboic link file)
	p:命令管道文件(Pipe)
	s:套接字文件(Socket)

查看多个目录:ls path1 path2

[root@ecs-x-medium-2-linux-20200307131357 /]# ls / /var
/:
bin                                         lib         sbin
boot                                        lib64       selinux
CloudResetPwdUpdateAgent                    lost+found  srv
CloudrResetPwdAgent                         media       sys
dev                                         mnt         tmp
etc                                         opt         usr
home                                        proc        var
HostGuardAgent_Linux64_V1.12.50.rpm.sha256  root
hostguard_setup_config.dat                  run

/var:
account  crash  db     games  local  log   nis  preserve  spool  yp
cache    cvs    empty  lib    lock   mail  opt  run       tmp

查看隐藏文件:ls -a

[root@ecs-x-medium-2-linux-20200307131357 /]# ls -a
.                         home                                        root
..                        HostGuardAgent_Linux64_V1.12.50.rpm.sha256  run
.autofsck                 hostguard_setup_config.dat                  sbin
.autorelabel              lib                                         selinux
bin                       lib64                                       srv
boot                      lost+found                                  sys
CloudResetPwdUpdateAgent  media                                       tmp
CloudrResetPwdAgent       mnt                                         usr
dev                       opt                                         var
etc                       proc

4. cd:切换工作目录

切换到根目录:cd /

[root@ecs-x-medium-2-linux-20200307131357 ~]# cd /
[root@ecs-x-medium-2-linux-20200307131357 /]#

切换到上一级目录:cd …

[root@ecs-x-medium-2-linux-20200307131357 yum]# cd ..
[root@ecs-x-medium-2-linux-20200307131357 etc]#

切换到当前用户家目录: cd

[root@ecs-x-medium-2-linux-20200307131357 etc]# cd
[root@ecs-x-medium-2-linux-20200307131357 ~]#

切换到普通用户家目录:cd ~user

[root@myComputer /]# cd ~a
[root@myComputer a]# 

5. pwd:显示当前工作目录

[root@ecs-x-medium-2-linux-20200307131357 vars]# pwd
/etc/yum/vars

6. mkdir:创建目录

创建单个目录:mkdir MyFiles

[root@ecs-x-medium-2-linux-20200307131357 ~]# mkdir MyFiles
[root@ecs-x-medium-2-linux-20200307131357 ~]# ls
MyFiles

创建多个目录:mkdir -p a/b/c

[root@ecs-x-medium-2-linux-20200307131357 ~]# mkdir -p a/b/c
[root@ecs-x-medium-2-linux-20200307131357 c]# pwd
/root/MyFiles/a/b/c

同时创建多个目录:mkdir dir1 dir2 dir3

[root@ecs-x-medium-2-linux-20200307131357 MyFiles]# mkdir dir1 dir2 dir3
[root@ecs-x-medium-2-linux-20200307131357 MyFiles]# ls
a  dir1  dir2  dir3

含有相同名称可简写(花括号扩展)

[root@ecs-x-medium-2-linux-20200307131357 b]# mkdir dir{1,2,3}
[root@ecs-x-medium-2-linux-20200307131357 b]# ls
dir1  dir2  dir3

7. rm:删除

普通删除:rm a.txt

[root@ecs-x-medium-2-linux-20200307131357 b]# ls
a.txt  dir1  dir2  dir3
[root@ecs-x-medium-2-linux-20200307131357 b]# rm a.txt
rm: remove regular empty file `a.txt'? yes
[root@ecs-x-medium-2-linux-20200307131357 b]# ls
dir1  dir2  dir3

强制删除:rm -f b.txt(慎用强制删除,没有提示)

[root@ecs-x-medium-2-linux-20200307131357 b]# ls
b.txt  dir1  dir2  dir3
[root@ecs-x-medium-2-linux-20200307131357 b]# rm -f b.txt
[root@ecs-x-medium-2-linux-20200307131357 b]# ls
dir1  dir2  dir3

删除文件夹 rm -r dir1

[root@ecs-x-medium-2-linux-20200307131357 b]# ls
dir1  dir2  dir3
[root@ecs-x-medium-2-linux-20200307131357 b]# rm -r dir1
rm: remove directory `dir1'? yes
[root@ecs-x-medium-2-linux-20200307131357 b]# ls
dir2  dir3

结合使用 rm -rf dir2

[root@ecs-x-medium-2-linux-20200307131357 b]# ls
dir2  dir3
[root@ecs-x-medium-2-linux-20200307131357 b]# rm -rf dir2
[root@ecs-x-medium-2-linux-20200307131357 b]# ls
dir3

8. cp:拷贝

拷贝文件:cp a.txt b.txt

[root@ecs-x-medium-2-linux-20200307131357 c]# ls
a.txt
[root@ecs-x-medium-2-linux-20200307131357 c]# cp a.txt b.txt
[root@ecs-x-medium-2-linux-20200307131357 c]# ls
a.txt  b.txt

拷贝文件夹

[root@ecs-x-medium-2-linux-20200307131357 c]# ls
a  a.txt  b.txt
[root@ecs-x-medium-2-linux-20200307131357 c]# cp -r a b
[root@ecs-x-medium-2-linux-20200307131357 c]# ls
a  a.txt  b  b.txt

9. mv :移动

移动文件 mv file path

[root@ecs-x-medium-2-linux-20200307131357 dira]# ls
a.txt
[root@ecs-x-medium-2-linux-20200307131357 dira]# mv a.txt ../dirb
[root@ecs-x-medium-2-linux-20200307131357 dira]# ls
[root@ecs-x-medium-2-linux-20200307131357 dira]# ls ../dirb
a.txt

重命名 mv ofile nfile

[root@ecs-x-medium-2-linux-20200307131357 dirb]# ls
a.txt
[root@ecs-x-medium-2-linux-20200307131357 dirb]# mv a.txt a.md
[root@ecs-x-medium-2-linux-20200307131357 dirb]# ls
a.md

10. ln:链接

建立硬链接,相当于Java中指向的同一块地址

[root@ecs-x-medium-2-linux-20200307131357 a]# ls
a.txt
[root@ecs-x-medium-2-linux-20200307131357 a]# ln a.txt a.link
[root@ecs-x-medium-2-linux-20200307131357 a]# ll
total 0
-rw-r--r-- 2 root root 0 Oct 30 00:08 a.link
-rw-r--r-- 2 root root 0 Oct 30 00:08 a.txt

建立软链接

[root@ecs-x-medium-2-linux-20200307131357 a]# ln -s a.txt a.link
[root@ecs-x-medium-2-linux-20200307131357 a]# ll
total 4
lrwxrwxrwx 1 root root 5 Oct 30 00:13 a.link -> a.txt
-rw-r--r-- 1 root root 5 Oct 30 00:10 a.txt

11. stat:显示文件的元数据

[root@ecs-x-medium-2-linux-20200307131357 a]# stat a.txt
  File: `a.txt'
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fc01h/64513d    Inode: 2362687     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-10-30 00:15:52.259975262 +0800
Modify: 2020-10-30 00:10:11.601793899 +0800
Change: 2020-10-30 00:16:12.336281151 +0800

12. touch:新建文件

如果文件已存在则刷新时间

[root@ecs-x-medium-2-linux-20200307131357 a]# stat a.txt
  File: `a.txt'
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fc01h/64513d    Inode: 2362687     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-10-30 00:15:52.259975262 +0800
Modify: 2020-10-30 00:10:11.601793899 +0800
Change: 2020-10-30 00:16:12.336281151 +0800
[root@ecs-x-medium-2-linux-20200307131357 a]# touch a.txt
[root@ecs-x-medium-2-linux-20200307131357 a]# stat a.txt
  File: `a.txt'
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fc01h/64513d    Inode: 2362687     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-10-30 00:20:27.798166785 +0800
Modify: 2020-10-30 00:20:27.798166785 +0800
Change: 2020-10-30 00:20:27.798166785 +0800

文件不存在则新建

[root@ecs-x-medium-2-linux-20200307131357 a]# ls
a.link  a.txt  b.link
[root@ecs-x-medium-2-linux-20200307131357 a]# touch c.txt
[root@ecs-x-medium-2-linux-20200307131357 a]# ls
a.link  a.txt  b.link  c.txt

二. 文本操作命令

1. cat 展示文本(more)

[root@ecs-x-medium-2-linux-20200307131357 a]# cat a.txt
qwer
qswd
dsfsfds
fsdfsf
fssdfs
fsfsdg
dfdf
sdfwsf
fdgfdsfg
fdsf
ddfdgd
gdgf
d
g
dg
d
f
gd
g
dg
drgdgegd
g
dhdg
dg
d
rgdd
g
rd
greg
efgd

2. less:分屏展示

[root@ecs-x-medium-2-linux-20200307131357 a]# less a.txt
qwer
qswd
dsfsfds
fsdfsf
fssdfs
fsfsdg
dfdf
sdfwsf
fdgfdsfg
fdsf
ddfdgd
gdgf
d
g
dg
d
f
gd
g
dg
drgdgegd
g
dhdg
a.txt

3.head:展示前n行

展示前十行head a.txt

[root@ecs-x-medium-2-linux-20200307131357 a]# head a.txt
qwer
qswd
dsfsfds
fsdfsf
fssdfs
fsfsdg
dfdf
sdfwsf
fdgfdsfg
fdsf

展示前i行:head -i a.txt

[root@ecs-x-medium-2-linux-20200307131357 a]# head -5 a.txt
qwer
qswd
dsfsfds
fsdfsf
fssdfs

4. tail 展示后n行

展示后十行tail a.txt

[root@ecs-x-medium-2-linux-20200307131357 a]# tail a.txt
g
dhdg
dg
d
rgdd
g
rd
greg
efgd

展示后i行:tail -i a.txt

[root@ecs-x-medium-2-linux-20200307131357 a]# tail -5 a.txt
g
rd
greg
efgd

监控文件变化:tail -f a.txt

5. 管道

[root@ecs-x-medium-2-linux-20200307131357 a]# cat a.txt|head -3
qwer
qswd
dsfsfds
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值