linux学习(1): 基础指令和进阶指令

基础指令

ls

ls 输出显示 的颜色:蓝色代表文件夹;黑色代表文件;绿色表示该文件/文件夹拥有所有权限

cp

cp linux1.txt /home/linux123/linux1.txt

cp linux1.txt /home/linux123/linux10.txt  # 可以改名字

linux 复制的时候可以为文件重新命名,但是一般不介意复制的时候修改文件名字

# 复制一个文件夹
cp -r ./yunweihenniux /home/linux123/yunweihenniux

mv

类似于 windows下面的剪切

输出重定向

>: 覆盖输出,覆盖原文件的内容
>>: 追加输出,在原始文件的末尾来追加内容

cat

可以打开一个文件

也可以结合 输出重定向,合并文件

进阶指令

df

df -h  # 表示按照可读性较高的形式展示大小

F i l e s y s t e m Filesystem Filesystem: 可以将其理解为 磁盘分区,类似于 windows 下面的 C盘;D盘;等 /dev/mapper/rhel_linuxprobe-root 是我们的硬盘

S i z e Size Size: 大小

Used

A v a i l Avail Avail

$Use% $ 使用的百分比

Mounted on: 挂载点

# Filesystem: 是系统的分区,类似于windows 界面下的磁盘分区,devtmpfs、tmpfs组和/dev/sda1和windows为系统预留的内存,是我们所不能使用的
# 因此我们看硬盘的分区只需要看 /dev/mapper/rhel_linuxprobe-root 就可以了

[linuxprobe@linuxprobe Desktop]$ df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root   18G  3.5G   15G  20% /
devtmpfs                          905M     0  905M   0% /dev
tmpfs                             914M   92K  914M   1% /dev/shm
tmpfs                             914M  8.8M  905M   1% /run
tmpfs                             914M     0  914M   0% /sys/fs/cgroup
/dev/sda1                         497M  119M  379M  24% /boot

free 指令

# 查看内存使用情况
free [-m]  # 输出显示 内存单位:MB 一般使用 free -m
free -k
free -g
free 

[linuxprobe@linuxprobe Desktop]$ free
             total       used       free     shared    buffers     cached
Mem:       1870784    1486376     384408      10208       1564     439236
-/+ buffers/cache:    1045576     825208
Swap:      2097148          0    2097148
[linuxprobe@linuxprobe Desktop]$ free -m
             total       used       free     shared    buffers     cached
Mem:          1826       1451        375          9          1        428
-/+ buffers/cache:       1021        805
Swap:         2047          0       2047
[linuxprobe@linuxprobe Desktop]$ free -g
             total       used       free     shared    buffers     cached
Mem:             1          1          0          0          0          0
-/+ buffers/cache:          1          0
Swap:            1          0          1
[linuxprobe@linuxprobe Desktop]$ free -k
             total       used       free     shared    buffers     cached
Mem:       1870784    1492504     378280      10264       1564     439272
-/+ buffers/cache:    1051668     819116
Swap:      2097148          0    2097148


# 如何查看系统有多少是可以使用的呢?
# 我们真实看内存使用情况 : -/+ buffers/cache 这一行的数据比较准确 ;包含了系统为其分配但没有被使用的内存空间
# Mem行的 buffers 和 cached 项是被系统分配的,但是没有被使用的内存 前者是程序输出可以占用的内存,后者是程序输入可以占用的内存 


# 这里的 375 + 10 + 1 + 428 约等于有 805,可能会有1的差距,因为此时的显示单位为MB,可能会存在四舍五入。

# 查看硬盘的剩余内存,查看 -/+ buffers/cache: 行的 free 项 为 805 MB
[linuxprobe@linuxprobe Desktop]$ free -m
      total       used       free     shared    buffers     cached
Mem:  1826       1451        375         10          1        428
-/+ buffers/cache: 
	1021        805
Swap:   
	2047          0       2047
	
# Swap : 交换 安装虚拟机的时候,内存分配空间为2GB,也就是上面 Mem 一行; 当该内存空间不够用的时候。允许我们使用硬盘空间来充当临时内存,但效率会慢好多,一般用于应急。同时windows下 ReadyBoost 也支持该机制
# Swap : 是我们在安装系统的时候,linux默认为我们分配2GB的交换空间 ,该值理想值应该是实际内存的 2~4 倍

在windows磁盘分区下面,“属性-> ReadyBoost" 可以设置当内存空间不够用的时候,允许使用该硬盘内存来代替内存空间来运行

head 指令

# 查看一个文件的前 n 行,如果不指定 n,则默认显示前10行
head -n 文件路径
[linuxprobe@linuxprobe Desktop]$ head testtanh.c 
#include <stdio.h>
#include <math.h>

int main(void)
{
	printf("%f\n", tanh(1));
	return 0;
}
[linuxprobe@linuxprobe Desktop]$ head -n 3 testtanh.c 
#include <stdio.h>
#include <math.h>

[linuxprobe@linuxprobe Desktop]$ head -3 testtanh.c 
#include <stdio.h>
#include <math.h>

[linuxprobe@linuxprobe Desktop]$ 


tail 指令

# 查看文件后末尾 n 行的数据, 默认显示后10行
tail -n 文件路径

# 可以通过 tail 指令来查看一个文件的动态变化(火车站的站台屏幕可以循环播放监测)变化的内容不能是用户手动增加的
tail -f 文件路径
# 该命令一般用来查看系统日志
[linuxprobe@linuxprobe Desktop]$ tail testtanh.c 
#include <stdio.h>
#include <math.h>

int main(void)
{
	printf("%f\n", tanh(1));
	return 0;
}
[linuxprobe@linuxprobe Desktop]$ tail -3 testtanh.c 
	printf("%f\n", tanh(1));
	return 0;
}

less 指令

# 查看文件,以较少的内容来进行输出,按下辅助功能键来查看 更多内容
# 辅助功能键: 
# 1. 数字 + 回车:跳转到指定行
# 2. 空格键 : 翻页
# 3. 上下方向键 : 控制行
less 需要查看的文件路径

# 在退出的时候,按下 q 键即可

wc 指令

# 用于统计文件内容信息: 多少字、多少行;(包括行数、单词数、字节数)
# 统计单词的个数是依照空格来判断单词的个数(对于统计中文不太适用,我们可以使用其来统计行数)

# l - lines
wc -l 待统计的文件路径 

# w - words
wc -w 待统计的文件路径

# c - bytes characters
wc -c 待统计的文件路径
# 当什么参数也不加的时候,显示三个数据:行数、单词数、字节数
[linuxprobe@linuxprobe ~]$ wc ls.txt 
 132 1194 6420 ls.txt
[linuxprobe@linuxprobe ~]$ wc -l ls.txt 
132 ls.txt
[linuxprobe@linuxprobe ~]$ wc -w ls.txt 
1194 ls.txt
[linuxprobe@linuxprobe ~]$ wc -c ls.txt 
6420 ls.txt

date 指令(重点)

# 表示操作时间日期的命令: 读取 & 设置;读取是主要使用的功能
# 在之后的shell脚本中,date指令比较重要

# 1. 输出系统默认格式的日期和时间 
date 

# 2. 输出标准的年月日 2021-04-04
# 字母的大小写应该被严格注意
date +%F
date "+%F"
date "+%Y-%m-%d"

# 3. 输入 年月日 时分秒 单引号或者双引号都可以
# -%m-%d %H:%M:%S 都是带“前导0”
date "+%F %T"
date "+%Y-%m-%d %T"
date "+%Y-%m-%d %H:%M:%S"

# 4. 获取之前或者以后某个时间点的时间 (在备份的时候,会使用到。一般数据库备份保留 三十天内或者七天内的数据)
# 往前 推1天(昨天),按照后面的个数输出数据
# 年月日(year  month  day) + -
date -d "-1 day" "+%Y-%m-%d %H:%M:%S"
date -d "-1 year" "+%Y-%m-%d %H:%M:%S"
date -d "+1 year" "+%Y-%m-%d %H:%M:%S"
...
# CST: 指当地时间 
# UCT、CST、GMT
# 一般输出形式使用:年-月-日 时:分:秒
[linuxprobe@linuxprobe Desktop]$ date 
Sun Apr  4 18:56:59 CST 2021

# 输出形式
[linuxprobe@linuxprobe Desktop]$ date "+%F"
2021-04-04
[linuxprobe@linuxprobe Desktop]$ date +%F
2021-04-04
[linuxprobe@linuxprobe Desktop]$ date "+%Y-%m-%d"
2021-04-04

[linuxprobe@linuxprobe Desktop]$ date "+%F %T"
2021-04-04 19:05:20
[linuxprobe@linuxprobe Desktop]$ date "+%Y-%m-%d %T"
2021-04-04 19:05:14
[linuxprobe@linuxprobe Desktop]$ date "+%Y-%m-%d %H:%M:%S"
2021-04-04 19:08:40

[linuxprobe@linuxprobe Desktop]$ date -d "-1 day" "+%Y-%m-%d %H:%M:%S"
2021-04-03 19:14:53
[linuxprobe@linuxprobe Desktop]$ date "+%Y-%m-%d %H:%M:%S"
2021-04-04 19:15:03
[linuxprobe@linuxprobe Desktop]$ 
[linuxprobe@linuxprobe Desktop]$ date -d "-1 year" "+%Y-%m-%d %H:%M:%S"
2020-04-04 19:16:18
[linuxprobe@linuxprobe Desktop]$ date -d "-1 month" "+%Y-%m-%d %H:%M:%S"
2021-03-04 19:17:53




[linuxprobe@linuxprobe Desktop]$ date -d "+1 year" "+%Y-%m-%d %H:%M:%S"
2022-04-04 19:16:44
[linuxprobe@linuxprobe Desktop]$ date -d "+1 day" "+%Y-%m-%d %H:%M:%S"
2021-04-05 19:16:54
[linuxprobe@linuxprobe Desktop]$ date -d "+1 month" "+%Y-%m-%d %H:%M:%S"
2021-05-04 19:17:06

cal 指令

# 直接输出当前月份的日历
cal 
cal -1 # -1 表示一个月份(当前月份)的日历
cal -3 # 输出 上一个月 + 本月 + 下一个月 的日历

# 输出一年的日历
cal -y 2021	# 输出2021年全年的日历

cal -s  # 等价于 cal 默认是 "cal -s" 一个礼拜的第一天是从周六开始
cal -m  # 一个礼拜的第一天从周一开始
[linuxprobe@linuxprobe Desktop]$ cal
     April 2021     
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

[linuxprobe@linuxprobe Desktop]$ cal -3
     March 2021            April 2021             May 2021      
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6               1  2  3                     1
 7  8  9 10 11 12 13   4  5  6  7  8  9 10   2  3  4  5  6  7  8
14 15 16 17 18 19 20  11 12 13 14 15 16 17   9 10 11 12 13 14 15
21 22 23 24 25 26 27  18 19 20 21 22 23 24  16 17 18 19 20 21 22
28 29 30 31           25 26 27 28 29 30     23 24 25 26 27 28 29
                                            30 31               
[linuxprobe@linuxprobe Desktop]$ 

                                            30 31               
[linuxprobe@linuxprobe Desktop]$ cal -y 2021 > cal.txt
[linuxprobe@linuxprobe Desktop]$ ls
cal.txt  linux10.txt  linux1.txt  testtanh.c  Untitled Folder
[linuxprobe@linuxprobe Desktop]$ more cal.txt 
                               2021                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6       1  2  3  4  5  6
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    7  8  9 10 11 12 13
10 11 12 13 14 15 16   14 15 16 17 18 19 20   14 15 16 17 18 19 20
17 18 19 20 21 22 23   21 22 23 24 25 26 27   21 22 23 24 25 26 27
24 25 26 27 28 29 30   28                     28 29 30 31
31
        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
             1  2  3                      1          1  2  3  4  5
 4  5  6  7  8  9 10    2  3  4  5  6  7  8    6  7  8  9 10 11 12
11 12 13 14 15 16 17    9 10 11 12 13 14 15   13 14 15 16 17 18 19
18 19 20 21 22 23 24   16 17 18 19 20 21 22   20 21 22 23 24 25 26
25 26 27 28 29 30      23 24 25 26 27 28 29   27 28 29 30
                       30 31
        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
             1  2  3    1  2  3  4  5  6  7             1  2  3  4
 4  5  6  7  8  9 10    8  9 10 11 12 13 14    5  6  7  8  9 10 11
11 12 13 14 15 16 17   15 16 17 18 19 20 21   12 13 14 15 16 17 18
18 19 20 21 22 23 24   22 23 24 25 26 27 28   19 20 21 22 23 24 25
25 26 27 28 29 30 31   29 30 31               26 27 28 29 30

       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6             1  2  3  4
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    5  6  7  8  9 10 11
10 11 12 13 14 15 16   14 15 16 17 18 19 20   12 13 14 15 16 17 18
17 18 19 20 21 22 23   21 22 23 24 25 26 27   19 20 21 22 23 24 25
24 25 26 27 28 29 30   28 29 30               26 27 28 29 30 31
31

clear / ctrl-l 指令

将终端显示的数据置顶,留出来当前终端位置

管道符号 |

  1. 过滤作用
  2. 结合其他指令,完成特定的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值