linux常用指令

一、开关机

1.1 立即关机

[root@localhost ~]# shutdown -h now

1.2 立即重启

[root@localhost ~]# shutdown -r now

[root@localhost ~]# reboot

1.3 延迟1分钟关机

[root@localhost ~]# shutdown -h 1
Shutdown scheduled for Fri 2019-01-18 13:56:15 CST, use ‘shutdown -c’ to cancel.
[root@localhost ~]#
Broadcast message from root@localhost.localdomain2 (Fri 2019-01-18 13:55:15 CST):
The system is going down for power-off at Fri 2019-01-18 13:56:15 CST!

1.4 指定时间机器重启

[root@localhost ~]# shutdown -r 14:04
Shutdown scheduled for Fri 2019-01-18 14:04:00 CST, use ‘shutdown -c’ to cancel.
[root@localhost ~]#
Broadcast message from root@localhost.localdomain2 (Fri 2019-01-18 14:03:32 CST):
The system is going down for reboot at Fri 2019-01-18 14:04:00 CST!

1.5 取消定时关机

[root@localhost ~]# shutdown -c

Broadcast message from root@localhost.localdomain2 (Fri 2019-01-18 14:06:00 CST):
The system shutdown has been cancelled at Fri 2019-01-18 14:07:00 CST!

1.6退出当前用户登录:

[root@localhost ~]# exit
[root@localhost ~]# logout

二、其他

">":用于将结果重写入文件中(覆盖,没有就新创建)
">>":用于将结果追加到文件中
[root@localhost /]# ls -al >demo
[root@localhost /]# ls -al demo
-rw-r–r--. 1 root root 1119 Jan 18 11:37 demo
[root@localhost /]# ls -al >demo
[root@localhost /]# ls -al demo
-rw-r–r--. 1 root root 1119 Jan 18 11:37 demo
[root@localhost /]# ls -al >>demo
[root@localhost /]# ls -al demo
-rw-r–r--. 1 root root 2238 Jan 18 11:38 demo
"kill -9 7399":杀死进程号为7399的进程
管道命令:
ps aux |grep root > res.txt
解释: 使用ps aux命令产生进程的相关信息然后输出给grep命令去检索然后将结果写入到res.txt中
回忆命令:
[root@localhost ~]# history >history
解释: 将这个终端输入的历史命令全部写入到history文件中
more&less
都是分页查看内容
more的[空格:下一页][b:上一下][enter:下一行]
less的[up:上一行][down:下一行][left:向左滚动][right:向右滚动][pagedown:下一页][pageup:上一页]
查看当前系统是64位还是32位
[root@localhost ~]# getconf LONG_BIT
64
查看系统名称
[root@localhost hadoop-3.2.0]# uname
Linux
查看系统内核版本
[root@localhost hadoop-3.2.0]# uname -r
3.10.0-957.el7.x86_64
查看系统信息
[root@localhost hadoop-3.2.0]# uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
查看当前系统的内存情况
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 641M 416M 9.6M 761M 928M
Swap: 2.0G 520K 2.0G

解释: total表示安装内存,used表示已经使用的,available表示可以使用的内存,-h表示单位换算
[root@localhost ~]# free -h -s 1
解释: -s 1表示每个1秒输出内存的使用情况
查看系统主机名
[root@localhost data]# hostname
localhost.localdomain

使用systemd查看系统主机名
[root@localhost data]# hostnamectl

[root@localhost ~]# hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: d9d25a0f1996408b8e4c5d6f22909262
           Boot ID: 49d8aa45d8ca4df788fe7ba6873ecba8
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-957.el7.x86_64
      Architecture: x86-64

三、日期时间

3.1 查看当前系统时间

[root@localhost /]# date
Thu Jan 17 14:32:32 CST 2019

3.2 查看当前系统使用的时区

[root@localhost /]# date -R
Thu, 17 Jan 2019 14:33:28 +0800

3.3 查看当前月的日历

[root@localhost /]# cal
January 2019
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 31

3.4 将系统日期设置为 2019-02-19

[root@localhost /]# date -s 2/17/2019
Sun Feb 17 00:00:00 CST 2019
注意: 这里说的是系统时间,是linux由操作系统维护的。
在系统启动时,Linux操作系统将时间从CMOS中读到系统时间变量中,以后修改时间通过修改系统时间实现。
为了保持系统时间与CMOS时间的一致性,Linux每隔一段时间会将系统时间写入CMOS。
由于该同步是每隔一段时间(大约是11分钟)进行的,在我们执行date -s后,如果马上重起机器,修改时间就有可能没有被写入CMOS,这就是问题的原因。
如果要确保修改生效可以执行如下命令。
[root@localhost ~]# clock -w

3.5 格式化输出当前系统的时间

[root@localhost ~]# date "+%Y-%m-%d %H:%M:%S"
2019-01-17 00:03:35

[root@localhost ~]# date +"%Y-%m-%d"
2019-01-17

[root@localhost ~]# date "+%H:%M:%S"
00:37:02
注意:
date中没有毫秒,直接到纳秒了

3.6 设置当前系统的日期

[root@localhost ~]# date -s "2019-1-17 16:43:20"
[root@localhost ~]# date -s 2019-1-17
[root@localhost ~]# date -s 16:44:40

四、进程相关命令

4.1 查看当前系统信息(所有用户登录的情况)

[root@localhost ~]# w
10:33:17 up 17:27, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
u1 tty1 10:33 13.00s 0.04s 0.04s -bash
root pts/0 192.168.114.1 10:29 5.00s 0.05s 0.03s w
root pts/1 192.168.114.1 Fri17 5:33 1.62s 0.08s bash

解释:
第一行显示 现在时间(10:33:17) 开机了多长时间(17小时27分钟) 登录了几个用户(3 users) 系统在过去1、5、15分钟内的平均负载程度(0.00, 0.01, 0.05)
最后三行显示登录的用户u1使用直接终端登录 今天10:33分登录的 用户闲置的时间(13秒没有做任何事情) JCPU(该终端所有进程执行消耗的cpu时间为0.04s) PCPU表示CPU执行程序消耗的时间(0.04) WHAT(用户现在正在执行的操作)

4.2 查看指定用户登录的情况

[root@localhost ~]# w root
10:39:40 up 17:34, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.114.1 10:29 4.00s 0.04s 0.00s w root
root pts/1 192.168.114.1 Fri17 11:56 1.62s 0.08s bash

4.3 查看属于自己的进程在运行的进程

[root@localhost ~]# ps
PID TTY TIME CMD
10300 pts/0 00:00:01 bash
10702 pts/0 00:00:00 ps

4.4 列举所有以用户和启动时间分类和排序的进程(包含无控制终端的进程)

[root@localhost ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 193552 6536 ? Ss Jan18 0:03 /usr/lib/systemd/systemd --switched-root --system --des
root 2 0.0 0.0 0 0 ? S Jan18 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Jan18 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Jan18 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S Jan18 0:03 [kworker/u256:0]

root 10725 0.0 0.0 0 0 ? S 11:53 0:00 [kworker/1:2]
root 10740 0.0 0.0 0 0 ? S 11:59 0:00 [kworker/0:0]
root 10778 0.0 0.0 0 0 ? R 12:01 0:00 [kworker/1:1]
root 10780 0.0 0.1 155360 1880 pts/0 R+ 12:01 0:00 ps aux

4.5 实时监控进程

[root@localhost ~]# top
top - 13:57:17 up 20:51, 3 users, load average: 0.00, 0.01, 0.05
Tasks: 150 total, 1 running, 145 sleeping, 4 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1863252 total, 1494752 free, 175380 used, 193120 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1494820 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 193552 6536 4136 S 0.0 0.4 0:04.00 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.09 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.76 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 0:04.00 kworker/u256:0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.06 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:14.70 rcu_sched

五、解压缩命令

5.1 将所有的*.txt文件打包成alltxt.tar

[u1@localhost ~]$ tar -cf alltxt.tar *.txt

5.2 列出alltxt.tar中的所有文件

[u1@localhost ~]$ tar -tf alltxt.tar
d2.txt
d3.txt
d4.txt
demo.txt

5.3 将文件readme增加到alltxt.tar的打包文件中

[u1@localhost ~]$ tar -rf alltxt.tar readme
[u1@localhost ~]$ tar -tf alltxt.tar
d2.txt
d3.txt
d4.txt
demo.txt
readme

5.4 将alltxt.tar包中的readme进行更新(发现更新后由两个readme,解压后实际就一个)

[u1@localhost ~]$ tar -uf alltxt.tar readme

5.5 从alltxt.tar包中删除文件d2.txt

[u1@localhost ~]$ tar --delete -f alltxt.tar d2.txt

5.6 将a目录和b目录打包到c目录下的test.tar文件包中

[u1@localhost ~]$ tar -cvf c/test.tar ./a ./b

5.7 查看test.tar包中的内容

[u1@localhost c]$ tar -tvf test.tar
drwxrwxr-x u2/u3 0 2019-01-19 09:00 ./a/
drwxrwxr-x u2/u3 0 2019-01-19 08:59 ./a/a1/
drwxrwxr-x u2/u3 0 2019-01-19 09:00 ./a/a2/
-rw-rw-r-- u2/u3 0 2019-01-19 09:00 ./a/a.txt
drwxrwxr-x u1/u1 0 2019-01-19 08:59 ./b/

5.8 将文件./d/demo.txt增加到test.tar包中

[u1@localhost c]$ tar -rvf test.tar ./d/demo.txt
./d/demo.txt
[u1@localhost c]$ tar -tvf test.tar
drwxrwxr-x u2/u3 0 2019-01-19 09:00 ./a/
drwxrwxr-x u2/u3 0 2019-01-19 08:59 ./a/a1/
drwxrwxr-x u2/u3 0 2019-01-19 09:00 ./a/a2/
-rw-rw-r-- u2/u3 0 2019-01-19 09:00 ./a/a.txt
drwxrwxr-x u1/u1 0 2019-01-19 08:59 ./b/
-rw-rw-r-- u1/u1 5 2019-01-19 14:33 ./d/demo.txt

5.9 将文件test.tar解包到指定文件夹

[u1@localhost c]$ tar -xvf test.tar -C test2
./a/
./a/a1/
./a/a2/
./a/a.txt
./b/
./d/demo.txt
[u1@localhost c]$ ls ./test2
a b d

注意: 如果不使用-C指定目录的话,会直接解包到当前目录
tar命令调用gzip在打包解包的同时使用gzip进行压缩和解压缩

5.10 一些文件或文件夹打包并压缩成文件test.tar.gz

[u1@localhost c]$ tar -zcvf test.tar.gz a b/ji c

5.11 查看test.tar.gz中的内容

[u1@localhost c]$ tar -ztvf test.tar.gz
-rw-rw-r-- u1/u1 4 2019-01-19 14:52 a
-rw-rw-r-- u1/u1 3 2019-01-19 14:53 b/ji
drwxrwxr-x u1/u1 0 2019-01-19 14:53 c/

5.12 对经压缩的打包文件(test.tar.gz)进行包内文件的更新、增加、删除都是不允许的

[u1@localhost c]$ tar -zuvf test.tar.gz b/ji
tar: Cannot update compressed archives
Try `tar --help’ or `tar --usage’ for more information.

[u1@localhost c]$ tar --delete -zvf b/ji
tar: Cannot update compressed archives
Try `tar --help’ or `tar --usage’ for more information.

[u1@localhost c]$ echo "ghjk">h
[u1@localhost c]$ tar -rzvf test.tar h
tar: Cannot update compressed archives
Try tar --help' ortar --usage’ for more information.

5.13 解压缩并解包文件test.tar.gz

[u1@localhost c]$ ls
test.tar.gz
[u1@localhost c]$ mkdir test2
[u1@localhost c]$ tar -zxvf test.tar.gz -C test2
a
b/ji
c/

tar命令调用bzip2在打包解包的同时使用gzip进行压缩和解压缩
centos7.6的最小化安装中应该是没有安装bzip2和compress程序(不再测试)

六、wget命令

6.1 http下载文件到当前目录

[root@localhost ~]# wget https://www.cnblogs.com/lxz88/p/6278268.html
–2019-01-21 11:44:52-- https://www.cnblogs.com/lxz88/p/6278268.html
Resolving www.cnblogs.com (www.cnblogs.com)… 47.111.45.248, 47.96.240.190
Connecting to www.cnblogs.com (www.cnblogs.com)|47.111.45.248|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 10280 (10K) [text/html]
Saving to: ‘6278268.html’
100%[=============================================================================>] 10,280 --.-K/s in 0.005s
2019-01-21 11:44:53 (1.83 MB/s) - ‘6278268.html’ saved [10280/10280]

6.2 ftp文件下载到当前目录

[root@localhost ~]# wget ftp://192.168.1.130/jdk8/jdk-8u102-windows-x64.exe
–2019-01-21 11:45:53-- ftp://192.168.1.130/jdk8/jdk-8u102-windows-x64.exe
=> ‘jdk-8u102-windows-x64.exe’
Connecting to 192.168.1.130:21… connected.
Logging in as anonymous … Logged in!
==> SYST … done. ==> PWD … done.
==> TYPE I … done. ==> CWD (1) /jdk8 … done.
==> SIZE jdk-8u102-windows-x64.exe … 204139576
==> PASV … done. ==> RETR jdk-8u102-windows-x64.exe … done.
Length: 204139576 (195M) (unauthoritative)
100%[==============================]
204,139,576 7.15MB/s in 20s
2019-01-21 11:46:14 (9.68 MB/s) - ‘jdk-8u102-windows-x64.exe’ saved [204139576]

6.3 下载文件指定文件名称

[root@localhost ~]# wget ftp://192.168.1.130/jdk8/jdk-8u102-windows-x64.exe -O /home/jdk

七、linux下安装配置

7.1 安装gcc

[root@localhost ~]# yum install -y gcc

7.2 测试gcc安装

[root@localhost ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright © 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

7.3.编译运行demo.c

#include <stdio.h>

int main()
{
		printf("hello linux gcc\n");
}

[root@localhost ~]# gcc demo.c
[root@localhost ~]# ls
6278268.html anaconda-ks.cfg a.out demo.c jdk jdk-8u102-windows-x64.exe
[root@localhost ~]# ./a.out
hello linux gcc

八、linux下使用zip和unzip

linux系统下默认没有安装zip和unzip名称:
[root@localhost ~]# yum install zip -y
[root@localhost ~]# yum install unzip -y

8.1 将指定文件夹和文本打成压缩包

[root@localhost ~]# zip -r demo.zip a demo.c
adding: a/ (stored 0%)
adding: a/b/ (stored 0%)
adding: a/b/ji (stored 0%)
adding: a/ko.txt (stored 0%)
adding: a/lp (stored 0%)
adding: demo.c (stored 0%)

8.2 将指定文件打成压缩包

[root@localhost ~]# zip a.zip a.out demo.c
adding: a.out (deflated 73%)
adding: demo.c (stored 0%)

8.3 解压缩zip文件到指定文件夹

[root@localhost ~]# unzip a.zip -d azip
Archive: a.zip
inflating: azip/a.out
extracting: azip/demo.c

解释: 如果不加-d azip就表示解压到当前文件夹

8.4 查看压缩包中的内容

[root@localhost ~]# unzip -Z demo.zip
Archive: demo.zip
Zip file size: 915 bytes, number of entries: 6
drwxr-xr-x 3.0 unx 0 bx stor 19-Jan-22 10:35 a/
drwxr-xr-x 3.0 unx 0 bx stor 19-Jan-22 10:36 a/b/
-rw-r–r-- 3.0 unx 0 bx stor 19-Jan-22 10:36 a/b/ji
-rw-r–r-- 3.0 unx 0 bx stor 19-Jan-22 10:35 a/ko.txt
-rw-r–r-- 3.0 unx 0 bx stor 19-Jan-22 10:35 a/lp
-rw-r–r-- 3.0 unx 65 tx stor 19-Jan-21 11:56 demo.c
6 files, 65 bytes uncompressed, 65 bytes compressed: 0.0%

九、使用xz解压缩

9.1 将多个文件和目录打压缩包

[root@localhost ~]# tar -Jcf a.xz a.out demo.c a

9.2 查看xz文件中的内容

[root@localhost ~]# tar -Jtvf a.xz
-rwxr-xr-x root/root 8440 2019-01-21 11:59 a.out
-rw-r–r-- root/root 108 2019-01-22 16:41 demo.c
drwxr-xr-x root/root 0 2019-01-22 10:35 a/
drwxr-xr-x root/root 0 2019-01-22 10:36 a/b/
-rw-r–r-- root/root 0 2019-01-22 10:36 a/b/ji
-rw-r–r-- root/root 0 2019-01-22 10:35 a/ko.txt
-rw-r–r-- root/root 0 2019-01-22 10:35 a/lp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

jackletter

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

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

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

打赏作者

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

抵扣说明:

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

余额充值