shell命令
1 内部命令和外部命令
使用 type 命令查询:
ding@ding-ubuntu:~$ type cd
cd 是 shell 内建
ding@ding-ubuntu:~$ type man
man 是 /usr/bin/man
ding@ding-ubuntu:~$ type ls
ls 是 `ls --color=auto' 的别名
ding@ding-ubuntu:~$
2 命令帮助查询
1 help 命令(内部命令用法:help 命令 外部命令用法:命令 --help)
help 命令是用于显示 shell 内建命令的简要帮助信息,如下所示:
ding@ding-ubuntu:~$ help pwd
pwd: pwd [-LP]
打印当前工作目录的名字。
选项:
-L 打印 $PWD 变量的值,如果它包含了当前的工作目录
-P 打印当前的物理路径,不带有任何的符号链接
默认情况下,`pwd' 的行为和带 `-L' 选项一致
退出状态:
除非使用了无效选项或者当前目录不可读,否则返回状态为0。
外部命令的 help:外部命令基本上都有一个参数 --help,通过这个参数可以得到相应的帮助
ding@ding-ubuntu:~$ man --help
用法: man [选项...] [章节] 手册页...
-C, --config-file=文件 使用该用户设置文件
-d, --debug 输出调试信息
-D, --default 将所有选项都重置为默认值
--warnings[=警告] 开启 groff 的警告
主要运行模式:
-f, --whatis 等同于 whatis
-k, --apropos 等同于 apropos
-K, --global-apropos 在所有页面中搜索文字
-l, --local-file
把“手册页”参数当成本地文件名来解读
-w, --where, --path, --location
输出手册页的物理位置
-W, --where-cat, --location-cat
输出 cat 文件的物理位置
-c, --catman 由 catman 使用,用来对过时的 cat
页重新排版
-R, --recode=编码 以指定编码输出手册页源码
2 man 命令(外部命令 + 部分内部命令),查询 Linux 系统中在线软件文档
ding@ding-ubuntu:~$ man ls
ding@ding-ubuntu:~$ man man
3 info 命令 (来自自由软件基金会的 GNU 项目,是 GNU 的超文本帮助系统)
ding@ding-ubuntu:~$ info man
ding@ding-ubuntu:~$ info ls
3 常用命令
sudo:su为超级用户(super),sudo = su + do,就是以超级用户运行;
ls:查看目录信息;
ding@ding-ubuntu:~$ ls -a
ding@ding-ubuntu:~$ ls -al
// 命令帮助
ding@ding-ubuntu:~$ man ls
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
-a, --all
do not ignore entries starting with . // 不忽略以 .开头的条目
-A, --almost-all
do not list implied . and .. // 不列出隐藏项 .和..
--author
with -l, print the author of each file // 打印每个文件的作者
-l use a long listing format // 使用长列表格式
cd:目录切换(进入路径)
ding@ding-ubuntu:~$ cd /
ding@ding-ubuntu:/$ cd /home/ding/
// 命令帮助
ding@ding-ubuntu:~$ help cd
cd: cd [-L|[-P [-e]] [-@]] [目录]
改变 shell 工作目录。
改变当前目录至 DIR 目录。默认的 DIR 目录是 shell 变量 HOME 的值。
pwd:显示当前路径
ding@ding-ubuntu:~$ pwd
/home/ding
uname:打印系统信息
ding@ding-ubuntu:~$ uname -a
Linux ding-ubuntu 5.3.0-26-generic #28~18.04.1-Ubuntu SMP Wed Dec 18 16:40:14 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
// 命令帮助
NAME
uname - print system information
SYNOPSIS
uname [OPTION]...
DESCRIPTION
Print certain system information. With no OPTION, same as -s.
-a, --all
print all information, in the following order, except omit -p and -i if unknown:
-s, --kernel-name
print the kernel name
-n, --nodename
print the network node hostname
-r, --kernel-release
print the kernel release
-v, --kernel-version
print the kernel version
-m, --machine
print the machine hardware name
-p, --processor
print the processor type (non-portable)
-i, --hardware-platform
print the hardware platform (non-portable)
clear:清屏
cat:显示文件内容
ding@ding-ubuntu:~/桌面$ cat test.c
/* test.c -- 测试程序 */
#include <stdio.h>
int main(int argc, char argv[])
{
printf("hello, world!\r\n");
return 0;
}
su:切换超级用户
ding@ding-ubuntu:~$ sudo su // 切换至超级用户
root@ding-ubuntu:/home/ding# sudo su ding // 从root切换回当前用户
rm:删除文件(谨慎操作)
经常使用 rm -r 文件夹 来删除文件夹(rm -rf 文件夹 这样更好用)
ding@ding-ubuntu:~$ rm -rf /* // 删库跑路
-rf 强制递归删除文件,/为根目录,*为通配符,表示删除/路径下所有文件
ding@ding-ubuntu:~$ rm --help
用法:rm [选项]... [文件]...
Remove (unlink) the FILE(s).
-f, --force ignore nonexistent files and arguments, never prompt
// 忽略不存在的文件和参数,不提示
-r, -R, --recursive remove directories and their contents recursively
// 递归删除目录及其内容
-d, --dir remove empty directories
-v, --verbose explain what is being done
默认时,rm 不会删除目录。使用--recursive(-r 或-R)选项可删除每个给定
的目录,以及其下所有的内容。
touch:创建文件(更新 / 修改文件时间属性)
ding@ding-ubuntu:~$ touch a.c
ding@ding-ubuntu:~$ touch --help
用法:touch [选项]... 文件...
Update the access and modification times of each FILE to the current time.
若文件不存在,系统会建立一个新的文件
必选参数对长短选项同时适用。
-a 只更改访问时间
-c, --no-create 不创建任何文件
-d, --date=字符串 使用指定字符串表示时间而非当前时间
-f (忽略)
-m 只更改修改时间
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help 显示此帮助信息并退出
--version 显示版本信息并退出
请注意,-d 和-t 选项可接受不同的时间/日期格式。
cp:复制文件(cp -r 复制文件夹)
ding@ding-ubuntu:~$ cp a.c b.c // 将a.c复制一份,重命名为b.c
ding@ding-ubuntu:~$ cp -r 软件/ 桌面/ // 将“软件”文件夹复制到“桌面”文件夹下
ding@ding-ubuntu:~$ cp --help
用法:cp [选项]... [-T] 源文件 目标文件
或:cp [选项]... 源文件... 目录
或:cp [选项]... -t 目录 源文件...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
必选参数对长短选项同时适用。
-n, --no-clobber 不要覆盖已存在的文件(使前面的 -i 选项失效)
-R, -r, --recursive 递归复制目录及其子目录内的所有内容
复制文件夹的结果:
mkdir:创建文件夹
ding@ding-ubuntu:~$ mkdir test
cd ../:回到上级目录,cd ../../:回到上级上级目录
rmdir:删除空目录(不好用,非空目录删不掉)
mv:移动文件 or 重命名文件
ding@ding-ubuntu:~$ mv a.c b.c // a.c重命名为b.c
ding@ding-ubuntu:~$ mv test/ 桌面/ // “test”文件夹移动到“桌面”下
ding@ding-ubuntu:~/桌面$ mv test/ test_重命名 // 重命名文件夹
ding@ding-ubuntu:~$ mv --help
用法:mv [选项]... [-T] 源文件 目标文件
或:mv [选项]... 源文件... 目录
或:mv [选项]... -t 目录 源文件...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
必选参数对长短选项同时适用。
--backup[=CONTROL] 为每个已存在的目标文件创建备份
-b 类似--backup 但不接受参数
-f, --force 覆盖前不询问
-i, --interactive 覆盖前询问
-n, --no-clobber 不覆盖已存在文件
路径说明
/ 表示根目录
./ 表示当前目录
../ 表示父级目录
ifconfig:显示网络配置信息
ding@ding-ubuntu:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
// IP地址 // 子网掩码 // 广播
inet 192.168.43.84 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::11e5:d8bc:a8b:9c56 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:94:7a:11 txqueuelen 1000 (以太网)
RX packets 8478 bytes 2004488 (2.0 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4142 bytes 642217 (642.2 KB)
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 1000 (本地环回)
RX packets 2675 bytes 177837 (177.8 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2675 bytes 177837 (177.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig还有非常多的配置选项,下面是一小部分:
ifconfig eth0 up # 启动eth0网卡
ifconfig eth0 down # 关闭eth0网卡
ifconfig eth0 reload # 重启
下面是关闭 / 打开网卡的测试:
ding@ding-ubuntu:~$ sudo ifconfig ens33 down // 关闭网卡
ding@ding-ubuntu:~$ ping www.baidu.com
ping: www.baidu.com: 未知的名称或服务 // 此时无法连接网络
ding@ding-ubuntu:~$ sudo ifconfig ens33 up // 打开网卡
ding@ding-ubuntu:~$ ping www.baidu.com
PING www.a.shifen.com (112.80.248.75) 56(84) bytes of data. // 网络恢复正常
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=1 ttl=55 time=57.9 ms
ifconfig命令还可以临时修改IP地址(重启清除),如下所示:
ding@ding-ubuntu:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
// 当前IP地址为192.168.43.84
inet 192.168.43.84 netmask 255.255.255.0 broadcast 192.168.43.255
ding@ding-ubuntu:~$ sudo ifconfig ens33 192.168.43.100 // 修改IP地址
[sudo] ding 的密码:
ding@ding-ubuntu:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
// 查看新IP地址
inet 192.168.43.100 netmask 255.255.255.0 broadcast 192.168.43.255
ding@ding-ubuntu:~$ ping 192.168.43.84 // 原IP地址无效
connect: 网络不可达
ding@ding-ubuntu:~$ ping 192.168.43.100 // 新IP地址生效
PING 192.168.43.100 (192.168.43.100) 56(84) bytes of data.
64 bytes from 192.168.43.100: icmp_seq=1 ttl=64 time=0.033 ms
reboot:重启系统
poweroff:关机
man:系统帮助命令(不仅能查询shell命令,还可以查询c语言函数)
ding@ding-ubuntu:~$ man printf
PRINTF(1) User Commands PRINTF(1)
NAME
printf - format and print data
SYNOPSIS
printf FORMAT [ARGUMENT]...
printf OPTION
DESCRIPTION
Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:
--help display this help and exit
--version
output version information and exit
FORMAT controls the output as in C printf. Interpreted sequences are:
\" double quote
\\ backslash
\a alert (BEL)
\b backspace
\c produce no further output
\e escape
\f form feed
\n new line
sync:同步数据写入磁盘(常用,因为在大型系统中,写入修改文件会先保存至内存缓冲区,再集中一次写入到硬盘中,这样能提高读写效率,延长硬盘使用寿命,这个命令能直接保存内存缓冲到磁盘中)
通配符说明:
通配符是一类键盘字符。
当查找文件夹时;当不知道真正字符或者不想键入完整名字时,常常使用通配符代替一个或多个真正字符。
星号(*)
可以使用星号代替零个、单个或多个字符。如果正在查找以AEW开头的一个文件,但不记得文件名其余部分,可以输入AEW*,查找以AEW开头的所有文件类型的文件,如AEWT.txt、AEWU.EXE、AEWI.dll等;要缩小范围可以输入AEW*.txt,查找以AEW开头的所有文件类型并.txt为扩展名的文件如AEWIP.txt、AEWDF.txt;要查询以 .c 结尾的文件,可以输入 *.c。
问号(?)
可以使用问号代替一个字符。如果输入love?,查找以love开头的一个字符结尾文件类型的文件,如lovey、lovei等。要缩小范围可以输入love?.doc,查找以love开头的一个字符结尾文件类型并.doc为扩展名的文件如lovey.doc、loveh.doc。
通配符包括星号“*”和问号“?”
星号表示匹配的数量不受限制,而后者的匹配字符数则受到限制。这个技巧主要用于英文搜索中,如输入““computer*”,就可以找到“computer、computers、computerised、computerized”等单词,而输入“comp?ter”,则只能找到“computer、compater、competer”等单词。
find:查找文件命令
一般格式:
find + 目录名称 + 参数[-name] + 文件名
如不设置目录,则在当前目录下查找
可使用通配符,如 *.c 查找目录下以 .c结尾的所有文件
ding@ding-ubuntu:~$ ls test/ // test目录下有4个 .c文件
file1.c file2.c file3.c file4.c
ding@ding-ubuntu:~$ find -name file1.c // 查找 file1.c
./test/file1.c
ding@ding-ubuntu:~$ find -name *.c // 查找以 .c结尾的所有文件
./test/file1.c
./test/file3.c
./test/file2.c
./test/file4.c
grep:查找文件内容
一般用法:
grep + 参数[-nr] + 要查找的字符串 + 路径
参数:
-i 或 --ignore-case : 忽略字符大小写
-s 或 --no-messages : 忽略错误提示信息
-n 或 --line-number : 显示查找到的行编号
-r 或 --recursive : 递归查找目录下所有文件
如下为查询结果:
ding@ding-ubuntu:~$ grep -nrs "你好世界" ./
./test/file1.c:1:你好世界
ding@ding-ubuntu:~$ grep -nrs "串查" ./
./test/file1.c:2:字符串查找
ding@ding-ubuntu:~$ grep -nrs "试文本" ./
./test/file1.c:3:测试文本
du:显示目录或文件的大小
ding@ding-ubuntu:~$ du -sh test/
8.0K test/
df:检查磁盘空间
ding@ding-ubuntu:~$ df
文件系统 1K-块 已用 可用 已用% 挂载点
udev 4038152 0 4038152 0% /dev
tmpfs 812492 2096 810396 1% /run
/dev/sda1 153767376 10257644 135629132 8% /
tmpfs 4062456 0 4062456 0% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
tmpfs 4062456 0 4062456 0% /sys/fs/cgroup
gedit:使用 gedit 打开文件
ding@ding-ubuntu:~$ gedit test/file1.c
ps:查看当前的系统进程
top:查看当前进程实时运行状态
file:查看文件类型
ding@ding-ubuntu:~$ file test/file1.c
test/file1.c: UTF-8 Unicode text