pos2@instance-20220901-0057:~$ man mkdir > mk.txt
pos2@instance-20220901-0057:~$ cat mk.txt
MKDIR(1) User Commands MKDIR(1)
NAME
mkdir - make directories
SYNOPSIS
mkdir [OPTION]... DIRECTORY...
DESCRIPTION
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
>> 追加输入
pos2@instance-20220901-0057:~$ echo "wdnmd" >> mk.txt
pos2@instance-20220901-0057:~$ tail -n 5 mk.txt
Full documentation at: <https://www.gnu.org/software/coreutils/mkdir>
or available locally via: info '(coreutils) mkdir invocation'
GNU coreutils 8.30 September 2019 MKDIR(1)
wdnmd
pos2@instance-20220901-0057:~/trydir$ ls -l xxx 2> err.txt
pos2@instance-20220901-0057:~/trydir$ cat err.txt
ls: cannot access 'xxx': No such file or directory
pos2@instance-20220901-0057:~/trydir$ ls -l /etc | more
total 856
drwxr-xr-x 3 root root 4096 Aug 16 09:45 ModemManager
drwxr-xr-x 3 root root 4096 Aug 16 09:45 NetworkManager
drwxr-xr-x 2 root root 4096 Aug 16 09:46 PackageKit
drwxr-xr-x 4 root root 4096 Aug 16 09:45 X11
-rw-r--r-- 1 root root 3028 Aug 16 09:44 adduser.conf
drwxr-xr-x 2 root root 4096 Sep 1 10:45 alternatives
drwxr-xr-x 3 root root 4096 Aug 16 09:45 apparmor
drwxr-xr-x 7 root root 4096 Aug 16 09:46 apparmor.d
--More--
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ubuntu不能用--stdin这个参数。。
root@instance-20220901-0057:/home/pos2/trydir# useradd tp
root@instance-20220901-0057:/home/pos2/trydir# echo "tppassword" | passwd --stdin tp
passwd: unrecognized option '--stdin'
Usage: passwd [options] [LOGIN]
# 可以用chpasswd
root@instance-20220901-0057:/home/pos2/trydir# echo tp:tppassword | chpasswd
root@instance-20220901-0057:/home/pos2/trydir#
# 顺便删了临时账户
root@instance-20220901-0057:/home/pos2/trydir# userdel -r tp
userdel: tp mail spool (/var/mail/tp) not found
userdel: tp home directory (/home/tp) not found
root@instance-20220901-0057:/home/pos2/trydir# cat /etc/passwd | grep "tp"
tss:x:106:111:TPM software stack,,,:/var/lib/tpm:/bin/false
命令行的通配符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 匹配所有在/dev目录中且以sda开头的文件
pos2@instance-20220901-0057:~$ ls -l /dev/sda*
brw-rw---- 1 root disk 8, 0 Sep 13 11:24 /dev/sda
brw-rw---- 1 root disk 8, 1 Sep 13 11:24 /dev/sda1
brw-rw---- 1 root disk 8, 14 Sep 13 11:24 /dev/sda14
brw-rw---- 1 root disk 8, 15 Sep 13 11:24 /dev/sda15
# 匹配sda后面带一个字符的文件
pos2@instance-20220901-0057:~$ ls -l /dev/sda?
brw-rw---- 1 root disk 8, 1 Sep 13 11:24 /dev/sda1
pos2@instance-20220901-0057:~$ ls -l /dev/sda[0-9][0-9]
brw-rw---- 1 root disk 8, 14 Sep 13 11:24 /dev/sda14
brw-rw---- 1 root disk 8, 15 Sep 13 11:24 /dev/sda15
常用的转义字符
反斜杠 \ 使\后面的一个变量变成单纯的字符串
单引号 ’’ 转移其中所有的变量为单纯的字符串
双引号 "" 保留其中的变量属性,不转义
反引号 `` 把其中的命令执行后返回结果
1
2
3
4
5
6
7
pos2@instance-20220901-0057:~$ PRICE=5
pos2@instance-20220901-0057:~$ echo "price is $PRICE"
price is 5
pos2@instance-20220901-0057:~$ echo "price is \$$PRICE"
price is $5
pos2@instance-20220901-0057:~$ echo `uname -a`
Linux instance-20220901-0057 5.15.0-1016-oracle #20~20.04.1-Ubuntu SMP Mon Aug 8 07:20:11 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
重要的环境变量
命令在linux中的执行分为四步
判断是否以绝对路径或相对路径输入
检查是否是别名命令
判断是内部命令还是外部命令
在$PATH中查找命令
1
2
3
4
5
6
7
# 给命令起别名,可以添加默认选项
pos2@instance-20220901-0057:~$ alias rm='rm -i'
pos2@instance-20220901-0057:~$ rm mk.txt
rm: remove regular file 'mk.txt'? n
pos2@instance-20220901-0057:~$ alias rm
alias rm='rm -i'
pos2@instance-20220901-0057:~$ unalias rm
1
2
3
4
5
6
7
# type+命令 检查是内部命令还是外部命令
pos2@instance-20220901-0057:~$ type clear
clear is hashed (/usr/bin/clear)
pos2@instance-20220901-0057:~$ type cd
cd is a shell builtin
pos2@instance-20220901-0057:~$ type ls
ls is aliased to `ls --color=auto'