Linux之基础命令篇

综述

现在市面上主流linux发行版包括红帽系(有Redhat,Centos,Fedora,Rocky)、Debian系(有Debian,ubuntu,Deepin)、还有就是SUSE系统…,这几款系统都各有特点,主要还是看个人喜好,个人感觉如果是新手可以从红帽系列系系统切入,这样更容易上手,接下里要讲的命令也是基本围绕红帽系统展开,当然大部分命令也是用于其他系统系统,那就愉快的开始吧(更新中。。。)

1. cd

cd命令的主要作用是切换目录

cd  /  切换到根目录
cd ../ 切换到上一级目录
cd ~   切换到家目录 
cd -   切换到上一次所在的目录
===============================
[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# cd ../
[root@localhost local]# pwd
/usr/local
[root@localhost local]# cd /
[root@localhost /]# pwd
/
[root@localhost /]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd -
/
[root@localhost /]# 

2. pwd

pwd命令主要是显示当前所在目录(绝对路径)

[root@localhost mulu]# pwd
/usr/local/src/mulu
[root@localhost mulu]# cd ../
[root@localhost src]# pwd
/usr/local/src

3. ls

ls命令是用来查询当前目录下的内容

ls 查询当前目录下的基本内容
ls -l 查询当前目录详情内容
ls -la 查询当前目录下的目录详情内容(包含隐藏文件和文件夹)
===================================================
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@localhost local]# ls -l
total 0
drwxr-xr-x. 2 root root  6 May 11  2019 bin
drwxr-xr-x. 2 root root  6 May 11  2019 etc
drwxr-xr-x. 2 root root  6 May 11  2019 games
drwxr-xr-x. 2 root root  6 May 11  2019 include
drwxr-xr-x. 2 root root  6 May 11  2019 lib
drwxr-xr-x. 2 root root  6 May 11  2019 lib64
drwxr-xr-x. 2 root root  6 May 11  2019 libexec
drwxr-xr-x. 2 root root  6 May 11  2019 sbin
drwxr-xr-x. 5 root root 49 Jun  9  2020 share
drwxr-xr-x. 3 root root 48 Sep 17 22:45 src
[root@localhost local]# ls -la
total 0
drwxr-xr-x. 12 root root 144 Sep 17 23:13 .
drwxr-xr-x. 12 root root 144 Jun  9  2020 ..
drwxr-xr-x.  2 root root   6 May 11  2019 bin
drwxr-xr-x.  2 root root   6 May 11  2019 etc
drwxr-xr-x.  2 root root   6 May 11  2019 games
drwxr-xr-x.  2 root root   6 May 11  2019 include
drwxr-xr-x.  2 root root   6 May 11  2019 lib
drwxr-xr-x.  2 root root   6 May 11  2019 lib64
drwxr-xr-x.  2 root root   6 May 11  2019 libexec
drwxr-xr-x.  2 root root   6 May 11  2019 sbin
drwxr-xr-x.  5 root root  49 Jun  9  2020 share
drwxr-xr-x.  3 root root  48 Sep 17 22:45 src
-rw-r--r--.  1 root root   0 Sep 17 23:13 .test
[root@localhost local]# 

4. 查看文件内容

4.1 less

less查看文件内容是可以上下翻页的,一般会搭配管道符(|)、grep命令使用
例如:less 123.txt | grep ‘123’ 只查看包含123的内容,起到过滤一些不想查看的内容,这在日常使用中非常频繁。

4.2 more

和less很像,但是它只能单向从前往后翻页,有点像象棋中过了河的小卒子,只能前进,不能后退

4.3 cat

cat是一次查看所有内容,比较适合查看短小的内容,或是文件末尾内容

4.4 tac

是cat的倒序版,从它的命令符与cat相反就能看出来

4.5 head

它是查看文件内容的头几行,可以指定行数
例如:head -n 10 是指查看文件的前10行内容

4.6 tail

它是查看文件的尾部内容,可以指定行数,当文件内容有更新后总是可以查看最后的几行内容,所以经常用来查看动态日志
例如:tail -10f 123.txt 是指查看文件的末尾10行内容

5. 压缩命令

5.1 tar (.tar)

压缩文件和解压缩文件(.tar格式的) 它是保留源文件的,严格意义上他只能算是打包文件而非压缩文件,因为经过它打包的文件大小并没有减小

   压缩文件:   tar -cf  压缩包名(带.tar)  原文件
   解压文件:   tar -xf  压缩包
   ===========================================
[root@localhost src]# ls
12  123  m  mulu
[root@localhost src]# tar -cf 123.tar 123      //压缩文件
[root@localhost src]# ls
12  123  123.tar  m  mulu
[root@localhost src]# rm -rf 123
[root@localhost src]# ls
12  123.tar  m  mulu
[root@localhost src]# tar -xf 123.tar      //解压所包
[root@localhost src]# ls
12  123  123.tar  m  mulu
[root@localhost src]# 
5.2 tar (.tar.gz)

压缩文件和解压缩文件(.tar.gz格式的) 它是保留源文件的,这个实际上是个gzip一起用的

   压缩文件:  tar -zcvf 压缩包名(带.tar.gz)  原文件
   解压文件:  tar -zxvf 压缩包
   ============================================
   [root@localhost src]# ls
12  123  mulu
[root@localhost src]# tar -zcvf mulu.tar.gz mulu
mulu/
mulu/123.txt
mulu/124.txt
mulu/234.txt
mulu/list2/
mulu/list3/
mulu/list4/
[root@localhost src]# ls
12  123  mulu  mulu.tar.gz
[root@localhost src]# rm -rf mulu
[root@localhost src]# ls
12  123  mulu.tar.gz
[root@localhost src]# tar -zxvf mulu.tar.gz 
mulu/
mulu/123.txt
mulu/124.txt
mulu/234.txt
mulu/list2/
mulu/list3/
mulu/list4/
[root@localhost src]# ls
12  123  mulu  mulu.tar.gz
[root@localhost src]# 
5.3 tar (.tar.xz)

压缩文件和解压缩文件(.tar.xz格式的) 它是保留源文件的

压缩文件:  tar -cvf 压缩包名(带.tar.xz)  原文件
解压文件:  tar -xvf 压缩包
=============================================
[root@96573332e887 src]# ls
hello  update2CallData.sh  update2CallData2.sh
[root@96573332e887 src]# tar -cvf hello.tar.xz hello
hello
[root@96573332e887 src]# ls
hello  hello.tar.xz  update2CallData.sh  update2CallData2.sh
[root@96573332e887 src]# rm -rf hello
[root@96573332e887 src]# tar -xvf hello.tar.xz 
hello
[root@96573332e887 src]# ls
hello  hello.tar.xz  update2CallData.sh  update2CallData2.sh
[root@96573332e887 src]# 
5.4 zip

压缩文件和解压缩文件(.zip格式的) 它是保留源文件的

   压缩文件:  zip 压缩包名(带.zip)  原文件
   解压文件:  unzip 压缩包
   ====================================
[root@localhost src]# ls
12  123  mulu
[root@localhost src]# zip mulu.zip mulu
  adding: mulu/ (stored 0%)
[root@localhost src]# ls
12  123  mulu  mulu.zip
[root@localhost src]# rm -rf mulu
[root@localhost src]# ls
12  123  mulu.zip
[root@localhost src]# unzip mulu.zip
Archive:  mulu.zip
   creating: mulu/
[root@localhost src]# ls
12  123  mulu  mulu.zip
[root@localhost src]# 
5.5 bzip2

压缩文件和解压缩文件(.bz格式的) 它是保留源文件的,从下面的案例中可以看出,bzip2是不能压缩目录的,只能压缩文件

   压缩文件: bzip2 -k 原文件
   解压文件: bunzip -k 压缩包
   ==========================
[root@localhost src]# ls
12  123  mulu
[root@localhost src]# bzip2 -k mulu
bzip2: Input file mulu is a directory.
[root@localhost src]# ls
12  123  mulu
[root@localhost src]# bzip2 -k 123
[root@localhost src]# ls
12  123  123.bz2  mulu
[root@localhost src]# rm -rf 123
[root@localhost src]# ls
12   123.bz2  mulu
[root@localhost src]# bunzip2 -k 123.bz2 
[root@localhost src]# ls
12  123  123.bz2  mulu
[root@localhost src]# 
5.6 gzip

压缩文件和解压缩文件(.gz格式) ,也锁文件后它是不保留源文件的,而且解压后,也是不保留压缩文件的

   压缩文件: gzip  文件
   解压文件: gunzip 压缩包
   =====================
[root@localhost src]# ls
12  123
[root@localhost src]# gzip 123
[root@localhost src]# ls
12  123.gz
[root@localhost src]# gunzip 123.gz 
[root@localhost src]# ls
12  123
[root@localhost src]# 

6.文本编辑器

以下是两种文本编辑器,相比较而言,vim功能更强大,而nano更简单易用,个人更喜欢nano,现在的部分linux发行版默认编辑器就是nano例如像debian,ubuntu,如果要使用vim必须安装vim软件包才能使用

6.1 vim编辑器

在这里插入图片描述

6.2 nano编辑器

nano比vim命令要简单,执行nano 文件名就进入了编辑模式,编辑完成后执行Ctrl+O保存,然后回车,最后执行Ctrl+X退出编辑模式
在这里插入图片描述

7. 创建和删除文件

7.1 touch

创建文件
例如:touch 123.txt 创建一个文件
touch 123.txt 234.txt 同时创建多个文件夹

[root@localhost mulu]# ls
[root@localhost mulu]# touch 123.txt
[root@localhost mulu]# ls
123.txt
[root@localhost mulu]# touch 124.txt 234.txt
[root@localhost mulu]# ls
123.txt  124.txt  234.txt
[root@localhost mulu]# 

7.2 rm

删除文件
例如:

rm 123.txt  删除一个文件,但是会有提示确认是否删除
=============================================
[root@localhost mulu]# ls
123.txt  124.txt  234.txt  test
[root@localhost mulu]# rm 123.txt
rm: remove regular empty file '123.txt'? Y
[root@localhost mulu]# ls
124.txt  234.txt  test

rm 124.txt 234.txt 同时删除多个文件,但要多次确认
=============================================
[root@localhost mulu]# rm 124.txt 234.txt
rm: remove regular empty file '124.txt'? Y
rm: remove regular empty file '234.txt'? Y
[root@localhost mulu]# ls
test

rm  -rf 123.txt 
=============================================
[root@localhost mulu]# ls
123.txt  124.txt  test
[root@localhost mulu]# rm -rf 123.txt
[root@localhost mulu]# ls
124.txt  test
[root@localhost mulu]# rm -rf 124.txt test
[root@localhost mulu]# ls
[root@localhost mulu]# 

注意:大家删除文件时要特别小心,以防出现误删除

8. 创建和删除文件夹

8.1 mkdir

创建空的文件夹

[root@localhost mulu]# ls
123.txt  124.txt  234.txt
[root@localhost mulu]# mkdir list
[root@localhost mulu]# ls
123.txt  124.txt  234.txt  list
[root@localhost mulu]# mkdir list2 list3 list4
[root@localhost mulu]# ls
123.txt  124.txt  234.txt  list  list2  list3  list4
8.2 rmdir

删除空的文件夹,如果文件下有内容,要用rm -rf才能删除

[root@localhost list]# ls
[root@localhost list]# cd ../
[root@localhost mulu]# ls
123.txt  124.txt  234.txt  list  list2  list3  list4
[root@localhost mulu]# cd list
[root@localhost list]# ls
[root@localhost list]# mkdir list5
[root@localhost list]# ls
list5
[root@localhost list]# cd list5
[root@localhost list5]# ls
[root@localhost list5]# mkdir list6
[root@localhost list5]# ls
list6
[root@localhost list5]# cd list6
[root@localhost list6]# ls
[root@localhost list6]# touch 12.txt
[root@localhost list6]# ls
12.txt
[root@localhost list6]# cd ../../../
[root@localhost mulu]# ls
123.txt  124.txt  234.txt  list  list2  list3  list4
[root@localhost mulu]# rm -rf list
[root@localhost mulu]# ls
123.txt  124.txt  234.txt  list2  list3  list4
[root@localhost mulu]# 

9. cp

cp命令是用来复制文件或文件夹的,复制文件夹需要加上参数-r

[root@localhost src]# ls
12  123
[root@localhost src]# cp 123 124
[root@localhost src]# ls
12  123  124
[root@localhost src]# mkdir mulu
[root@localhost src]# ls
12  123  124  mulu
[root@localhost src]# cp -r mulu  ma
[root@localhost src]# ls
12  123  124  ma  mulu
[root@localhost src]# cp -r mulu  ../ma
[root@localhost src]# cd ../
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  ma  sbin  share  src
[root@localhost local]#

10. mv

mv命令是用来移动或时重命名文件或文件夹的,如果只有一个文件名存在(两个都是文件名)或一个文件夹存在(两个都是文件夹名),则为重命名;如果文件名在前,文件夹名在后或者时两个文件夹名(两个都存在),则为移动

[root@localhost src]# ls
12  123  124  ma  mulu
[root@localhost src]# mv 123 321    //将文件123重名为321
[root@localhost src]# ls
12  124  321  ma  mulu
[root@localhost src]# mv 321 ma     //将文件321移动到ma目录
[root@localhost src]# ls
12  124  ma  mulu
[root@localhost src]# cd ma
[root@localhost ma]# ls
321
[root@localhost ma]# cd ../
[root@localhost src]# ls
12  124  ma  mulu
[root@localhost src]# mv ma mulu      //将文件夹ma移动到文件夹mulu
[root@localhost src]# ls
12  124  mulu
[root@localhost src]# cd mulu
[root@localhost mulu]# ls
ma
[root@localhost mulu]# mv ma mb    //将文件夹ma重命名为mb
[root@localhost mulu]# ls
mb
[root@localhost mulu]# 

11. clear

这个命令简单,clear回车,直接清屏。

12. echo

这个命令也简单,后面跟什么就输出什么,也可以跟表达式

[root@localhost mulu]# echo hellworld
hellworld
[root@localhost mulu]# echo "hell world"
hell world
[root@localhost mulu]# echo $SHELL
/bin/bash
[root@localhost mulu]# echo $(date)
Sat Sep 18 23:17:03 CST 2021
[root@localhost mulu]# 

13. kill

主要时用来杀死进程的,当某个服务已经关闭,但是后台进程意外还是挂在,这个时候就适合用它来杀死进程或结束进程。

kill 进程号
kill -9 进程号    //加上参数-9表示强制杀死某个进程

14. ps

主要用来查看进程,通常和-ef 还有grep一起使用

在这里插入图片描述

15. grep

主要用来过滤查询结果,比如和less一起用:less 123.txt | grep “123”
和ps一起用ps -ef | grep 进程号

16. date

这个命令主要用来查询日期时间的

[root@localhost mulu]# date
Sat Sep 18 23:37:23 CST 2021
[root@localhost mulu]# echo $(date +%Y-%m-%d)
2021-09-18
[root@localhost mulu]# echo $(date +%Y/%m/%d)
2021/09/18
[root@localhost mulu]# echo $(date +%z)
+0800
[root@localhost mulu]# echo $(date +%:z)
+08:00
[root@localhost mulu]# echo $(date +%::z)
+08:00:00
[root@localhost mulu]# echo $(date -d "3 days ago" +%Y-%m-%d)
2021-09-15

17. 服务停止和启动

(1)查看服务运行状态

systemctl  status  服务名
例如:systemctl  status  sshd

在这里插入图片描述

(2)启动服务

systemctl  start  服务名     
例如:systemctl start sshd

在这里插入图片描述

(3)停止服务

systemctl stop 服务名
例如:systemctl  stop  sshd

在这里插入图片描述
(4)重启服务

systemctl  restart   服务名
例如:systemctl  restart  sshd
这里的重启服务和启动服务时差不多的,start启动的时非活动状态的服务,restart重启的是已经处于活动状态的服务

(5)开机启动服务

systemctl  enable  服务名
例如:
[root@localhost ~]# systemctl enable firewalld
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
[root@localhost ~]# 

(6)开机禁用服务

systemctl  disable  firewalld
例如:
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# 

18. wc

统计文件内容
wc -l 统计文件的行数
wc -w 统计文件的字数
wc -m 统计文件的字符数
wc -c 统计文件的字节数

[root@96573332e887 src]# ls
1  1.tar.xz  hello  update2CallData.sh	update2CallData2.sh
[root@96573332e887 src]# cat hello
welcome to the city of beijing
123456
[root@96573332e887 src]# wc -l hello
2 hello
[root@96573332e887 src]# wc -w hello
7 hello
[root@96573332e887 src]# wc -m hello
38 hello
[root@96573332e887 src]# wc -c hello
38 hello
[root@96573332e887 src]# 

19. curl

curl -o 把输出写到该文件中
curl -O 把输出写到该文件中,保留远程文件的文件名

[root@96573332e887 src]# curl -o 12.html https://jamysong.gitee.io/documents.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1514  100  1514    0     0   4601      0 --:--:-- --:--:-- --:--:--  4587
[root@96573332e887 src]# ls
1  1.tar.xz  12.html  hello  update2CallData.sh  update2CallData2.sh
[root@96573332e887 src]# curl -O https://jamysong.gitee.io/documents.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1514  100  1514    0     0   3692      0 --:--:-- --:--:-- --:--:--  3683
[root@96573332e887 src]# ls
1  1.tar.xz  12.html  documents.html  hello  update2CallData.sh  update2CallData2.sh
[root@96573332e887 src]# 

20.其他

(1) reboot
重启(只有root可以执行),等同于init 6

(2) poweroff
关机(只有root可以执行),等同于init 0

(3) su
切换用户,其他用户切换到root需要输入root账户密码,但root账户切换到其他账户不需要,因为root账户是linux下的超级管理员账户,拥有最高权限
例如: su root

(4) which
查看命令文件的位置
例如:which ls

(5) whoami
查看当前终端的用户

(6) df
查看磁盘的使用情况(磁盘容量的划分情况)

[andy@96573332e887 src]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          44G  9.5G   33G  23% /
tmpfs            64M     0   64M   0% /dev
tmpfs          1000M     0 1000M   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/sda1        44G  9.5G   33G  23% /etc/hosts
tmpfs          1000M     0 1000M   0% /proc/acpi
tmpfs          1000M     0 1000M   0% /sys/firmware

(7) free
查看内存使用情况

[andy@96573332e887 src]$ free -h
              total        used        free      shared  buff/cache   available
Mem:          2.0Gi       1.0Gi       192Mi       0.0Ki       764Mi       813Mi
Swap:         1.0Gi          0B       1.0Gi
[andy@96573332e887 src]$ 

(8) tr
用于查看文件时替换指定的文件内容

[root@96573332e887 src]# ls
12  hello  update2CallData.sh  update2CallData2.sh
[root@96573332e887 src]# cat 12
wqeqewqeweqeeqeqe123qeqweeqe123
[root@96573332e887 src]# cat 12|tr 123 456
wqeqewqeweqeeqeqe456qeqweeqe456
[root@96573332e887 src]# 

(9) stat
用于查看文件的具体存储细节和时间等信息

[root@96573332e887 src]# ls
12  hello  update2CallData.sh  update2CallData2.sh
[root@96573332e887 src]# stat 12
  File: 12
  Size: 32        	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 1323450     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-09-22 08:52:48.954277000 +0000
Modify: 2021-09-22 08:52:11.242109000 +0000
Change: 2021-09-22 08:52:11.252109000 +0000
 Birth: -
[root@96573332e887 src]# 

(10) locate
用来按照文件名称搜索文件位置,第一次使用前需要执行updatedb命令来生成索引数据库,然后再进行查找

(11)find
它也是用来搜索文件的位置,会全盘扫描,虽然更准确,但效率比locate要低得多

(12)mount
用来挂载磁盘的
例如:挂载光驱
mount /dev/sr0 /mnt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下雨天的太阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值