Linux系统的命令通常都是如下所示的格式:
命令名称 [命名参数] [命令对象]
[root@izwz97tbgo9lkabnat2lo8z ~]# w
23:31:16 up 12:16, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 182.139.66.250 23:03 4.00s 0.02s 0.00s w
jackfrue pts/1 182.139.66.250 23:26 3:56 0.00s 0.00s -bash
[root@izwz97tbgo9lkabnat2lo8z ~]# who
root pts/0 2018-04-12 23:03 (182.139.66.250)
jackfrued pts/1 2018-04-12 23:26 (182.139.66.250)
[root@izwz97tbgo9lkabnat2lo8z ~]# who am i
root pts/0 2018-04-12 23:03 (182.139.66.250)
>Shell也被称为“壳”,它是用户与内核交流的翻译官,简单的说就是人与计算机交互的接口。目前很多Linux系统默认的Shell都是bash(Bourne Again SHell),因为它可以使用Tab键进行命令补全、可以保存历史命令、可以方便的配置环境变量以及执行批处理操作等。
[root@izwz97tbgo9lkabnat2lo8z ~]# ps
PID TTY TIME CMD
3531 pts/0 00:00:00 bash
3553 pts/0 00:00:00 ps
[root@izwz97tbgo9lkabnat2lo8z ~]# whatis ps
ps (1) - report a snapshot of the current processes.
[root@izwz97tbgo9lkabnat2lo8z ~]# whatis python
python (1) - an interpreted, interactive, object-oriented programming language
[root@izwz97tbgo9lkabnat2lo8z ~]# whereis ps
ps: /usr/bin/ps /usr/share/man/man1/ps.1.gz
[root@izwz97tbgo9lkabnat2lo8z ~]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
[root@izwz97tbgo9lkabnat2lo8z ~]# which ps
/usr/bin/ps
[root@izwz97tbgo9lkabnat2lo8z ~]# which python
/usr/bin/python
[root@izwz97tbgo9lkabnat2lo8z ~]# ps --help
Usage:
ps [options]
Try 'ps --help '
or 'ps --help '
for additional help text.
For more details see ps(1).
[root@izwz97tbgo9lkabnat2lo8z ~]# man ps
PS(1) User Commands PS(1)
NAME
ps - report a snapshot of the current processes.
SYNOPSIS
ps [options]
DESCRIPTION
[root@izwz97tbgo9lkabnat2lo8z ~]# info ps
[root@izwz97tbgo9lkabnat2lo8z ~]# su hellokitty
[hellokitty@izwz97tbgo9lkabnat2lo8z root]$
[jackfrued@izwz97tbgo9lkabnat2lo8z ~]$ ls /root
ls: cannot open directory /root: Permission denied
[jackfrued@izwz97tbgo9lkabnat2lo8z ~]$ sudo ls /root
[sudo] password for jackfrued:
calendar.py code error.txt hehe hello.c index.html myconf result.txt
说明:如果希望用户能够以管理员身份执行命令,用户必须被添加到sudoers名单中,该文件在 /etc
目录下。
[root@izwz97tbgo9lkabnat2lo8z ~]# adduser hellokitty
[root@izwz97tbgo9lkabnat2lo8z ~]# passwd hellokitty
Changing password for user jackfrued.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@izwz97tbgo9lkabnat2lo8z ~]# ssh [email protected]
[email protected]'s password:
Last login: Thu Apr 12 23:05:32 2018 from 10.12.14.16
[hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ logout
Connection to 1.2.3.4 closed.
[root@izwz97tbgo9lkabnat2lo8z ~]#
[root@izwz97tbgo9lkabnat2lo8z ~]# uname
Linux
[root@izwz97tbgo9lkabnat2lo8z ~]# hostname
izwz97tbgo9lkabnat2lo8z
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
[root@iZwz97tbgo9lkabnat2lo8Z ~]# history
...
452 ls
453 cd Python-3.6.5/
454 clear
455 history
[root@iZwz97tbgo9lkabnat2lo8Z ~]# !454
说明:查看到历史命令之后,可以用!历史命令编号
来重新执行该命令;通过history -c
可以清除历史命令。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir abc
[root@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir -p xyz/abc
[root@iZwz97tbgo9lkabnat2lo8Z ~]# rmdir abc
[root@iZwz97tbgo9lkabnat2lo8Z ~]# touch readme.txt
[root@iZwz97tbgo9lkabnat2lo8Z ~]# touch error.txt
[root@iZwz97tbgo9lkabnat2lo8Z ~]# rm error.txt
rm: remove regular empty file ‘error.txt’? y
[root@iZwz97tbgo9lkabnat2lo8Z ~]# rm -rf xyz
说明:cd
命令后面可以跟相对路径(以当前路径作为参照)或绝对路径(以/
开头)来切换到指定的目录,也可以用cd ..
来返回上一级目录。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://www.sohu.com/ -O sohu.html
--2018-06-20 18:42:34-- http://www.sohu.com/
Resolving www.sohu.com (www.sohu.com)... 14.18.240.6
Connecting to www.sohu.com (www.sohu.com)|14.18.240.6|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 212527 (208K) [text/html]
Saving to: ‘sohu.html’
100%[==================================================>] 212,527 --.-K/s in 0.03s
2018-06-20 18:42:34 (7.48 MB/s) - ‘sohu.html’ saved [212527/212527]
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat sohu.html
...
[root@iZwz97tbgo9lkabnat2lo8Z ~]# head -10 sohu.html
搜狐
[root@iZwz97tbgo9lkabnat2lo8Z ~]# tail -2 sohu.html
[root@iZwz97tbgo9lkabnat2lo8Z ~]# less sohu.html
...
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat -n sohu.html | more
...
[root@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir backup
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cp sohu.html backup/
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cd backup
[root@iZwz97tbgo9lkabnat2lo8Z backup]# ls
sohu.html
[root@iZwz97tbgo9lkabnat2lo8Z backup]# mv sohu.html sohu_index.html
[root@iZwz97tbgo9lkabnat2lo8Z backup]# ls
sohu_index.html
[root@iZwz97tbgo9lkabnat2lo8Z ~]# find / -name "*.html"
/root/sohu.html
/root/backup/sohu_index.html
[root@izwz97tbgo9lkabnat2lo8z ~]# find . -atime 7 -type f -print
[root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -size +2k
[root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -name "*.swp" -delete
[root@iZwz97tbgo9lkabnat2lo8Z ~]# grep "
24:
25:
26:
29:
...
说明:grep
在搜索字符串时可以使用正则表达式,如果需要使用正则表达式可以用grep -E
或者直接使用egrep
。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html
-rw-r--r-- 1 root root 212131 Jun 20 19:15 sohu.html
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html
-rw-r--r-- 2 root root 212131 Jun 20 19:15 sohu.html
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup2
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html
-rw-r--r-- 3 root root 212131 Jun 20 19:15 sohu.html
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ln -s /etc/centos-release sysinfo
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sysinfo
lrwxrwxrwx 1 root root 19 Jun 20 19:21 sysinfo -> /etc/centos-release
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat sysinfo
CentOS Linux release 7.4.1708 (Core)
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
说明:链接可以分为硬链接和软链接(符号链接)。硬链接可以认为是一个指向文件数据的指针,就像Python中对象的引用计数
,每添加一个硬链接,文件的对应链接数就增加1,只有当文件的链接数为0时,文件所对应的存储空间才有可能被其他文件覆盖。我们平常删除文件时其实并没有删除硬盘上的数据,我们删除的只是一个指针
,或者说是数据的一条使用记录,所以类似于“文件粉碎机”之类的软件在“粉碎”文件时除了删除文件指针,还会在文件对应的存储区域填入数据来保证文件无法再恢复
。软链接类似于Windows系统下的快捷方式,当软链接链接的文件被删除时,软链接也就失效了。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz
--2018-06-20 19:29:59-- http://download.redis.io/releases/redis-4.0.10.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1738465 (1.7M) [application/x-gzip]
Saving to: ‘redis-4.0.10.tar.gz’
100%[==================================================>] 1,738,465 70.1KB/s in 74s
2018-06-20 19:31:14 (22.9 KB/s) - ‘redis-4.0.10.tar.gz’ saved [1738465/1738465]
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*
redis-4.0.10.tar.gz
[root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip redis-4.0.10.tar.gz
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*
redis-4.0.10.tar
[root@iZwz97tbgo9lkabnat2lo8Z ~]# tar -xvf redis-4.0.10.tar
redis-4.0.10/
redis-4.0.10/.gitignore
redis-4.0.10/00-RELEASENOTES
redis-4.0.10/BUGS
redis-4.0.10/CONTRIBUTING
redis-4.0.10/COPYING
redis-4.0.10/INSTALL
redis-4.0.10/MANIFESTO
redis-4.0.10/Makefile
redis-4.0.10/README.md
redis-4.0.10/deps/
redis-4.0.10/deps/Makefile
redis-4.0.10/deps/README.md
...
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis*
redis-4.0.10.tar
redis-4.0.10:
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING INSTALL README.md runtest-cluster src
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat foo.txt
grape
apple
pitaya
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat bar.txt
100
200
300
400
[root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt
grape 100
apple 200
pitaya 300
400
[root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt > hello.txt
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cut -b 4-8 hello.txt
pe 10
le 20
aya 3
0
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat hello.txt | tr '\t' ','
grape,100
apple,200
pitaya,300
,400
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget https://www.baidu.com/img/bd_logo1.png
--2018-06-20 18:46:53-- https://www.baidu.com/img/bd_logo1.png
Resolving www.baidu.com (www.baidu.com)... 220.181.111.188, 220.181.112.244
Connecting to www.baidu.com (www.baidu.com)|220.181.111.188|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7877 (7.7K) [image/png]
Saving to: ‘bd_logo1.png’
100%[==================================================>] 7,877 --.-K/s in 0s
2018-06-20 18:46:53 (118 MB/s) - ‘bd_logo1.png’ saved [7877/7877][root@iZwz97tbgo9lkabnat2lo8Z ~]# file bd_logo1.png
bd_logo1.png: PNG image data, 540 x 258, 8-bit colormap, non-interlaced
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wc sohu.html
2979 6355 212527 sohu.html
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wc -l sohu.html
2979 sohu.html