linux哲学思想:一切皆文件
常用系统工作命令
echo
echo命令用于在终端显示字符串或变量,
格式为:echo [字符串 | 变量]
将echo命令的字符串输出到终端:
[root@localhost ~]# echo chengwufei
chengwufei
用echo命令查看SHELL变量的值(前面有$符号):
[root@localhost ~]# echo $SHELL
/bin/bash
查看本机主机名:
[root@localhost ~]# echo $HOSTNAME
localhost.localdomain
date
date命令用于显示/设置系统的时间或日期,
格式为:date [选项] [+指定的格式]
强大的date命令能够按照指定格式显示系统的时间或日期,只需键入”+”号开头的字符串指定其格式.
详细格式如下:
eg:
查看当前的系统时间:
[root@localhost ~]# date
Mon Sep 17 06:00:05 EDT 2018
按照”年-月-日 小时:分钟:秒”的格式:
[root@localhost ~]# date "+%Y-%m-%d %H:%M:%S"
2018-09-17 06:01:12
设置系统时间为2018年9月17日8点半:
[root@localhost ~]# date -s "20180917 8:30:00"
Mon Sep 17 08:30:00 EDT 2018
查看本地系统时区:
[root@localhost ~]# date "+%Z"
EDT
查看星期几:
[root@localhost ~]# date "+%A"
Monday
输入当前是上午还是下午:
[root@localhost ~]# date "+%p"
AM
判断今天是一年中的第几天:
[root@localhost ~]# date "+%j"
260
reboot
reboot命令用于重启系统(仅root用户可以使用),格式为:”reboot”。
重启计算机:
[root@localhost ~]# reboot
wget
wget命令用于使用命令行下载网络文件,
格式为:wget [参数] 下载地址
系统状态检测命令
ifconfig
ifconfig用于获取网卡配置与网络状态等信息:格式为ifconfig [网络设备] [参数]
查看本机当前的网卡配置与网络状态等信息:
[root@xgj ~]# ifconfig
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.134.135 netmask 255.255.255.0 broadcast 192.168.134.255
inet6 fe80::563a:c614:1ea8:686a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1b:5b:dd txqueuelen 1000 (Ethernet)
RX packets 1081 bytes 88707 (86.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 516 bytes 67896 (66.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 4 bytes 352 (352.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 352 (352.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
uname
uname命令用于查看系统内核版本等信息,格式为:“uname [-a]”。
查看系统的内核名称、内核发行版、内核版本、节点名、硬件名称、硬件平台、处理器类型、操作系统等信息:
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
uptime 查看系统的负载情况
uptime命令用于查看系统的负载情况,格式为:“uptime”。
我也经常用”watch -n 1 uptime“来每秒刷新一次获得当前的系统负载情况,输出内容分别为系统当前时间、系统已运行时间、当前在线用户以及平均负载值。
而平均负载分为最近1分钟、5分钟、15分钟的系统负载情况,负载值越低越好(小于1是正常)。
[root@localhost ~]# uptime
08:41:23 up 1:07, 3 users, load average: 0.00, 0.01, 0.04
free
free命令用于显示当前系统中内存的使用量情况,格式为:free [-m/-g]
以m为单位显示当前系统中内存的使用量情况:
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 976M 131M 705M 6.9M 139M 684M
Swap: 2.0G 0B 2.0G
who
[root@localhost ~]# who
root tty1 2018-09-17 08:33
root pts/0 2018-09-17 05:09 (192.168.134.1)
root pts/1 2018-09-17 08:34 (192.168.134.1)
last
last命令用于查看所有系统的登入记录,格式为:”last [参数]”。
[root@localhost ~]# last
root pts/0 192.168.134.1 Tue Sep 11 08:23 - down (00:00)
reboot system boot 3.10.0-693.el7.x Tue Sep 11 05:17 - 08:24 (03:06)
root pts/0 192.168.134.1 Mon Sep 10 06:50 - crash (22:27)
reboot system boot 3.10.0-693.el7.x Mon Sep 10 06:00 - 08:24 (1+02:23)
root pts/0 192.168.134.1 Mon Sep 10 04:04 - down (01:55)
reboot system boot 3.10.0-693.el7.x Mon Sep 10 04:02 - 05:59 (01:57)
root pts/1 192.168.134.1 Sat Sep 8 21:36 - crash (1+06:25)
root pts/0 192.168.134.1 Sat Sep 8 21:10 - 23:22 (02:11)
reboot system boot 3.10.0-693.el7.x Sat Sep 8 21:10 - 05:59 (1+08:49)
root pts/0 192.168.134.1 Sat Sep 8 00:53 - crash (20:16)
reboot system boot 3.10.0-693.el7.x Fri Sep 7 09:38 - 05:59 (2+20:21)
root pts/0 192.168.134.1 Thu Sep 6 07:12 - crash (1+02:26)
root pts/0 192.168.134.1 Thu Sep 6 06:21 - 07:11 (00:50)
reboot system boot 3.10.0-693.el7.x Thu Sep 6 06:20 - 05:59 (3+23:39)
root pts/0 192.168.134.1 Thu Sep 6 05:47 - down (00:32)
root tty1 Thu Sep 6 05:46 - 06:20 (00:33)
reboot system boot 3.10.0-693.el7.x Thu Sep 6 13:46 - 06:20 (-7:-26)
root tty1 Thu Sep 6 13:44 - 13:46 (00:02)
reboot system boot 3.10.0-693.el7.x Thu Sep 6 13:43 - 06:20 (-7:-23)
wtmp begins Thu Sep 6 13:43:54 2018
history
history命令用于显示历史执行过的命令,格式为:“history [-c]”。
历史命令会被保存到用户家目录中的”.bash_history“文件中。Linux系统中以点(.)开头的文件均代表隐藏文件,一般会是系统文件。
清空该用户在本机中执行过命令的历史记录:
[root@localhost ~]# history -c
history默认会保存1000条执行过的命令,若要修改可直接编辑**/etc/profile**文件的HISTSIZE值。
sosreport
sosreport命令用于收集系统系统配置并诊断信息后输出结论文档,格式为:“sosreport”。
当我们的红帽系统出现故障需要联系红帽厂商或其他技术支持时,大多数情况都需要提供使用到这个命令。
收集系统本地配置信息并诊断
工作目录切换命令
pwd
pwd命令用于显示当前的工作目录,格式为:“pwd [选项]”。
[root@localhost ~]# pwd
/root
cd
cd命令用于切换工作路径,格式为:“cd [目录名称]”
ls
ls命令用于查看目录中有那些文件,格式为:ls [选项] [文件]
查看当前目录下有那些文件(长格式):
查看/etc目录中有那些文件:
[root@localhost ~]# ls /etc
adjtime hosts.allow rc1.d
aliases hosts.deny rc2.d
aliases.db init.d rc3.d
alternatives inittab rc4.d
anacrontab inputrc rc5.d
asound.conf iproute2 rc6.d
。。。。。。
查看/etc目录的权限与属性:
[root@localhost ~]# ls -ld /etc
drwxr-xr-x. 75 root root 8192 Sep 17 08:33 /etc
追加-h参数,以K/M/G为单位显示容量:
[root@localhost ~]# ls -ldh /etc
drwxr-xr-x. 75 root root 8.0K Sep 17 08:33 /etc
文本文件编辑命令
cat
cat命令用于查看纯文本文件(较短的),格式为:“cat [选项] [文件]”。
查看文本文件:
[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
more
more命令用于查看纯文本文件(较长的),格式为:“more [选项] 文件”。
查看文本文件:
[root@localhost test]# more /etc/passwd
head
head命令用于查看纯文本文档的前N行,格式为:“head [选项] [文件]”。
查看文本文件前2行:
[root@localhost test]# head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
tail
tail命令用于查看纯文本文档的后N行,格式为:“tail [选项] [文件]”
查看文本文件后2行:
[root@localhost test]# tail -2 /etc/passwd
asdsad:x:1001:1001::/home/asdsad:/bin/bash
aaaa:x:1002:100::/home/aaaa:/bin/bash
tr
tr命令用于转换文本文件中的字符,格式为:“tr [原始字符] [目标字符]”。
wc
wc命令用于统计指定文本的行数、字数、字节数,格式为“wc [参数] 文本”。
cut
cut命令用于通过列来提取文本字符,格式为:“cut [参数] 文本”。
[root@localhost test]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
diff
diff命令用于比较多个文本文件的差异,格式为:”diff [参数] 文件”。
[root@localhost test]# diff 1 2
1c1
< 1111
---
> 222
[root@localhost test]# cat 1
1111
[root@localhost test]# cat 2
222
[root@localhost test]# diff --brief 1 2
Files 1 and 2 differ
文件目录管理命令
touch命令
touch命令用于创建空白文件或设置文件的时间,格式为“touch [选项] [文件]”。
参数 | 作用 |
---|---|
-a | 仅修改“读取时间”(atime) |
-m | 仅修改“修改时间”(mtime) |
-d | 同时修改atime与mtime |
[root@localhost ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1421 Sep 6 13:40 anaconda-ks.cfg
[root@localhost ~]# echo "test" >> anaconda-ks.cfg
[root@localhost ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1426 Sep 18 05:24 anaconda-ks.cfg
[root@localhost ~]# touch -d "2017-09-06 13:40" anaconda-ks.cfg
[root@localhost ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1426 Sep 6 13:40 anaconda-ks.cfg
mkdir命令
mkdir命令用于创建空白的目录,格式为“mkdir [选项] 目录”。
root@localhost ~]# mkdir cwf
[root@localhost ~]# ls
1 anaconda-ks.cfg awk cwf datafile
cp命令
cp命令用于复制文件或目录,格式为“cp [选项] 源文件 目标文 件”。
参数 | 作用 |
---|---|
-p | 保留原始文件的属性 |
-d | 若对象为“链接文件”,则保留该“链接 文件”的属性 |
-r | 递归持续复制(用于目录) |
-i | 若目标文件存在则询问是否覆盖 |
-a | 相当于-pdr(p、d、r为上述参数) |
mv命令
mv命令用于剪切文件或将文件重命名,格式为“mv [选项] 源文件 [目标路径|目标文件名]”。
rm命令
rm命令用于删除文件或目录,格式为“rm [选项] 文件”。
dd命令
dd命令用于按照指定大小和个数的数据块来复制文件或转换文件, 格式为“dd [选项]”。
参数 | 作用 |
---|---|
if | 输入的文件名称 |
of | 输出的文件名称 |
bs | 设置每个“块”的大小 |
count | 设置要复制“块”的个数 |
[root@localhost ~]# dd if=/dev/zero of=560_file count=1 bs=5M
1+0 records in
1+0 records out
5242880 bytes (5.2 MB) copied, 0.030183 s, 174 MB/s
file命令
file命令用于查看文件的类型,格式为“file 文件名”。
[root@localhost ~]# file 1
1: ASCII text
[root@localhost ~]# cd /
[root@localhost /]# file /etc/
/etc/: directory
就上就这些了 ,可能不会太全。。。。