两大原则
- 一切皆文件
- Linux中任何命令、操作都有对应的文件
- 沉默是金
- 在Linux命令执行的过程中,没有消息就是好消息
首先明确几个重要概念
$ command [-options] parameter1 parameter2
指令 选项 参数1 参数2
- command:指令(command)或 可执行文件(如批次脚本 script)
- command:是指令名称,例如变换工作目录的指令是 cd 等
- 中括号是可选配置参数
-
一个短横线,如 -h,这是选项的简写--
两个短横线,是选项的完整名称,如 --help
- 指令、选项、参数等中间以空格来区分
- 按下 enter 按键后,就代表一行指令的开始启动
- 严格区分英文大小写
基础指令的使用
ls [-l]
查看文件夹列表(查看更多信息)
ll 是ls -l的别名
-r :将目录的内容清单以英文字母顺序的逆序显示
-t :按文件的修改时间进行排序
[root@localhost lianxi]# ls -l
总用量 24
-rw-r--r--. 1 root root 181 2月 23 16:27 buckup.sh
-rw-r--r--. 1 root root 147 2月 19 15:12 create_file.sh
-rw-r--r--. 1 root root 207 2月 20 20:45 create_user.sh
drwxr-xr-x. 2 root root 21 2月 21 16:30 function
-rw-r--r--. 1 root root 180 2月 23 20:55 monitor_crond.sh
drwxr-xr-x. 3 root root 18 2月 18 19:15 sanchuang
-rw-r--r--. 1 root root 4 2月 20 19:52 test.txt
-rw-r--r--. 1 root root 590 2月 20 20:45 user_pwd.txt
-rw-r--r--. 1 root root 181 2月 23 16:27 buckup.sh
对于输出的文件信息,具体解释如下
-
r代表读权限 w代表写权限 x代表执行权限
-
-rw-r--r--
开头的-
代表文件类型为普通文件,往后每三个为一组,第一组代表文件拥有者对该文件的权限,第二组代表文件所属用户组对该文件的权限,第三组代表其他非拥有者与用户组者对该文件的权限。
mkdir
新建文件夹,可以同时建立多个文件
[root@localhost demo]# mkdir test
[root@localhost demo]# ls
test
[root@localhost demo]# mkdir test1 test2
[root@localhost demo]# ls
test test1 test2
cd
切换目录 cd … 返回上一级
[root@localhost demo]# cd ..
[root@localhost lianxi]# cd demo
[root@localhost demo]#
pwd
查看当前目录
[root@localhost demo]# pwd
/lianxi/demo
touch
新建一个文件或多个文件
[root@localhost demo]# touch test.txt
[root@localhost demo]# ls
test.txt
[root@localhost demo]# touch test{1..10}.txt
[root@localhost demo]# ls
test10.txt test2.txt test4.txt test6.txt test8.txt test.txt
test1.txt test3.txt test5.txt test7.txt test9.txt
[root@localhost demo]#
rm
删除命令
-
rm -r 递归删除 文件夹中的子文件夹也会删除
-
rm -f 强制删除 不给予提醒
-
rm -rf * 删除文件夹中所有文件 但不包括隐藏文件
-
rm -rf .*删除文件夹中的隐藏文件
[root@localhost demo]# rm -rf *
[root@localhost demo]# ls
[root@localhost demo]#
env
查看环境变量
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/lianxi/demo
LANG=zh_CN.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.220.1 6180 192.168.220.129 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
ps aux
查看进程信息
[root@localhost demo]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 128020 6648 ? Ss 21:40 0:00 /usr/lib/
root 2 0.0 0.0 0 0 ? S 21:40 0:00 [kthreadd
root 4 0.0 0.0 0 0 ? S< 21:40 0:00 [kworker/
常用快捷键
table:自动补全文件名,补齐命令
ctrl+c:终止当前进程
ctrl+l:清屏
top:显示进程
获取帮助
这么多的指令,死记硬背肯定是不行的,由于 Linux 上大多数都是自由软件,因此开发者一般都会提供 文档等方式让你了解这些指令的用法
指令的 --help 求助说明
命令 --help
[root@localhost /]# mkdir --help
用法:mkdir [选项]... 目录...
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
--help 显示此帮助信息并退出
--version 显示版本信息并退出
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告mkdir 的翻译错误
要获取完整文档,请运行:info coreutils 'mkdir invocation'
man命令
如果是从来没有用过的指令, 或则要查询的根本就不是指令,而是文件的「格式」时,就需要通过 man page 了。
比如
DATE(1) User Commands DATE(1)
NAME
date - print or set the system date and time
SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
DESCRIPTION
Display the current time in the given FORMAT, or set the system
date.
Mandatory arguments to long options are mandatory for short
options too.
-d, --date=STRING
display time described by STRING, not 'now'
-f, --file=DATEFILE
like --date once for each line of DATEFILE
-I[TIMESPEC], --iso-8601[=TIMESPEC]
output date/time in ISO 8601 format. TIMESPEC='date' for
date only (the default), 'hours', 'minutes', 'seconds', or
'ns' for date and time to the indicated precision.
-r, --reference=FILE
display the last modification time of FILE
-R, --rfc-2822
output date and time in RFC 2822 format. Example: Mon, 07
Aug 2006 12:34:56 -0600
--rfc-3339=TIMESPEC
output date and time in RFC 3339 format. TIMESPEC='date',
'seconds', or 'ns' for date and time to the indicated pre‐
cision. Date and time components are separated by a single
space: 2006-08-07 12:34:56-06:00
-s, --set=STRING
set time described by STRING
-u, --utc, --universal
print or set Coordinated Universal Time (UTC)
--help display this help and exit
下表总结一些常用按键
按键 | 进行工作 |
---|---|
空格键 | 向下翻页 |
Page Down | 向下翻页 |
Page Up | 向上翻页 |
home | 去第一页 |
end | 去到最后一页 |
/string | 向下搜寻 string 这个字符串,如果要搜索 vbird 就输入 /vbird |
?string | 向上搜寻 string |
n,N | 利用 / 或 ?来搜寻字符串时,可以用 n 来继续下一个搜寻,N 上一个 |
q | 结束这次的 man page |
man page 搜索的不是在线的(前面书上说是在线求助),提供 man page 的数据通常放在 /usr/share/man
目录的。