Linux - 基础命令介绍
day01
1. 常用命令快捷键
命令 | 介绍 |
---|---|
Tab | 自动补全命令;按两下tab键,显示所有补全的命令 |
Ctrl+L | 清理屏幕(最后一行排到最顶层) |
clear | 清理屏幕(最后一行排到最顶层) |
Ctrl+C | 强制终止当前执行的命令,即杀死当前进程 |
2.系统相关的命令
命令 | 介绍 |
---|---|
hostname | 查看主机名 |
who | 查询当前系统登陆用户 |
whoami | 查询当前系统登陆的用户名 |
which 命令名 | 查询命令是否存在 |
history | 查看历史操作过的命令 |
! 历史编号 | 通过历史编号,执行历史操作过的命令 |
ifconfig | 查看网卡信息 |
ping | 测试IP地址或者域名是否访问的通 |
eg:
[root@VM-0-7-centos /]# hostname
VM-0-7-centos
[root@VM-0-7-centos /]# who
root pts/1 2021-02-07 15:06 (101.88.192.222)
[root@VM-0-7-centos /]# whoami
root
[root@VM-0-7-centos /]# which pwd
/bin/pwd
[root@VM-0-7-centos /]# history
....
2009 2021-02-07 17:24:03 hostname
2010 2021-02-07 17:24:07 who
2011 2021-02-07 17:24:19 whoami
2012 2021-02-07 17:24:38 which pwd
2013 2021-02-07 17:25:46 history
[root@VM-0-7-centos /]# !2009
hostname
VM-0-7-centos
[root@VM-0-7-centos /]# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:2F:58:33
inet addr:172.17.0.7 Bcast:172.17.15.255 Mask:255.255.240.0
inet6 addr: fe80::5054:ff:fe2f:5833/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:20389338 errors:0 dropped:0 overruns:0 frame:0
TX packets:20558183 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2169780671 (2.0 GiB) TX bytes:3655046018 (3.4 GiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:10 errors:0 dropped:0 overruns:0 frame:0
TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:700 (700.0 b) TX bytes:700 (700.0 b)
[root@VM-0-7-centos /]# ping www.baidu.com
PING www.a.shifen.com (180.101.49.12) 56(84) bytes of data.
64 bytes from 180.101.49.12: icmp_seq=1 ttl=49 time=7.44 ms
64 bytes from 180.101.49.12: icmp_seq=2 ttl=49 time=7.37 ms
64 bytes from 180.101.49.12: icmp_seq=3 ttl=49 time=7.36 ms
64 bytes from 180.101.49.12: icmp_seq=4 ttl=49 time=7.34 ms
64 bytes from 180.101.49.12: icmp_seq=5 ttl=49 time=7.35 ms
64 bytes from 180.101.49.12: icmp_seq=6 ttl=49 time=7.34 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5408ms
rtt min/avg/max/mdev = 7.343/7.370/7.449/0.105 ms
[root@VM-0-7-centos /]#
3. pwd 查看当前路径
命令 | 介绍 |
---|---|
pwd | 显示当前所在路径(显示的是绝对路径) |
eg:
[root@VM-0-7-centos ~]# pwd
/root
[root@VM-0-7-centos ~]#
4. cd 路径的切换
语法:cd 目标路径(相对路径/绝对路径)
常用的路径切换:
命令 | 介绍 |
---|---|
cd ~ | 进入当前用户的家路径 |
cd ./ | 进入当前路径 |
cd . ./ | 进入上一级路径 |
cd - | 进入上一次跳转的路径 |
cd / | 进入根路径 |
eg:
[root@VM-0-7-centos ~]# cd ~
[root@VM-0-7-centos ~]# pwd
/root
[root@VM-0-7-centos ~]# cd ./
[root@VM-0-7-centos ~]# pwd
/root
[root@VM-0-7-centos ~]# cd ../
[root@VM-0-7-centos /]# pwd
/
[root@VM-0-7-centos /]# cd -
/root
[root@VM-0-7-centos ~]# cd /
[root@VM-0-7-centos /]# pwd
/
[root@VM-0-7-centos /]#
5. ls、ll 显示指定工作目录下的内容
语法:
ls 参数 目录名(不写,默认当前目录)
ll = ls -l
参数 | 介绍 |
---|---|
-a | 显示所有文件及目录(. 开头的隐藏文件也会列出) |
-l | 除了文件名称外,也将文件型态、权限、拥有者、文件大小等详细内容列出 |
eg:
[root@VM-0-7-centos ~]# ls
1 a b
[root@VM-0-7-centos ~]# ls b #显示当前目录下的b目录下的内容
c
[root@VM-0-7-centos ~]# ls -a
. 1 b .bash_logout .bashrc .pip .ssh .viminfo
.. a .bash_history .bash_profile .cshrc .pydistutils.cfg .tcshrc
[root@VM-0-7-centos ~]# ls -l
total 84
-rw-r--r-- 1 root root 70356 Feb 7 15:36 1
drwxr-xr-x 2 root root 4096 Feb 7 15:25 a
drwxr-xr-x 3 root root 4096 Feb 7 15:26 b
[root@VM-0-7-centos ~]# ll
total 84
-rw-r--r-- 1 root root 70356 Feb 7 15:36 1
drwxr-xr-x 2 root root 4096 Feb 7 15:25 a
drwxr-xr-x 3 root root 4096 Feb 7 15:26 b
[root@VM-0-7-centos ~]# ls -al
total 180
dr-xr-x---. 6 root root 4096 Feb 7 15:35 .
dr-xr-xr-x. 24 root root 4096 Feb 7 17:51 ..
-rw-r--r-- 1 root root 70356 Feb 7 15:36 1
drwxr-xr-x 2 root root 4096 Feb 7 15:25 a
drwxr-xr-x 3 root root 4096 Feb 7 15:26 b
-rw-r--r-- 1 root root 41485 Feb 7 17:51 .bash_history
-rw-r--r--. 1 root root 18 May 20 2009 .bash_logout
-rw-r--r--. 1 root root 176 May 20 2009 .bash_profile
-rw-r--r--. 1 root root 176 Sep 23 2004 .bashrc
-rw-r--r--. 1 root root 100 Sep 23 2004 .cshrc
drwxr-xr-x 2 root root 4096 Dec 27 10:11 .pip
-rw-r--r-- 1 root root 73 Dec 27 10:11 .pydistutils.cfg
drwx------ 2 root root 4096 Mar 13 2018 .ssh
-rw-r--r--. 1 root root 129 Dec 4 2004 .tcshrc
-rw------- 1 root root 4689 Jan 13 14:28 .viminfo
[root@VM-0-7-centos ~]# ll -a
total 180
dr-xr-x---. 6 root root 4096 Feb 7 15:35 .
dr-xr-xr-x. 24 root root 4096 Feb 7 17:51 ..
-rw-r--r-- 1 root root 70356 Feb 7 15:36 1
drwxr-xr-x 2 root root 4096 Feb 7 15:25 a
drwxr-xr-x 3 root root 4096 Feb 7 15:26 b
-rw-r--r-- 1 root root 41504 Feb 7 17:51 .bash_history
-rw-r--r--. 1 root root 18 May 20 2009 .bash_logout
-rw-r--r--. 1 root root 176 May 20 2009 .bash_profile
-rw-r--r--. 1 root root 176 Sep 23 2004 .bashrc
-rw-r--r--. 1 root root 100 Sep 23 2004 .cshrc
drwxr-xr-x 2 root root 4096 Dec 27 10:11 .pip
-rw-r--r-- 1 root root 73 Dec 27 10:11 .pydistutils.cfg
drwx------ 2 root root 4096 Mar 13 2018 .ssh
-rw-r--r--. 1 root root 129 Dec 4 2004 .tcshrc
-rw------- 1 root root 4689 Jan 13 14:28 .viminfo
[root@VM-0-7-centos ~]#
6. mkdir 创建目录
语法:
语法 | 介绍 |
---|---|
mkdir 目录名1 目录名2 | 创建一个或者多个无层级目录 |
mkdir 目录名1/目录名2 目录名3 -p | 创建一个或者多个有层级目录 |
mkdir {目录1,目录2,目录3}/目录4 -p | 创建3个目录,并在3个目录下分别再创建一个同名的目录 |
参数 | 介绍 |
---|---|
-p | 创建层级目录 |
eg:
[root@VM-0-7-centos ~]# rm -fr *
[root@VM-0-7-centos ~]# ll
total 0
[root@VM-0-7-centos ~]# mkdir {a,b,c}/e -p
[root@VM-0-7-centos ~]# ll
total 12
drwxr-xr-x 3 root root 4096 Feb 7 18:17 a
drwxr-xr-x 3 root root 4096 Feb 7 18:17 b
drwxr-xr-x 3 root root 4096 Feb 7 18:17 c
[root@VM-0-7-centos ~]# ll a;ll b;ll c
total 4
drwxr-xr-x 2 root root 4096 Feb 7 18:17 e
total 4
drwxr-xr-x 2 root root 4096 Feb 7 18:17 e
total 4
drwxr-xr-x 2 root root 4096 Feb 7 18:17 e
[root@VM-0-7-centos ~]# mkdir f g
[root@VM-0-7-centos ~]# ll
total 20
drwxr-xr-x 3 root root 4096 Feb 7 18:17 a
drwxr-xr-x 3 root root 4096 Feb 7 18:17 b
drwxr-xr-x 3 root root 4096 Feb 7 18:17 c
drwxr-xr-x 2 root root 4096 Feb 7 18:22 f
drwxr-xr-x 2 root root 4096 Feb 7 18:22 g
[root@VM-0-7-centos ~]# mkdir o/p/q -p
[root@VM-0-7-centos ~]# ll
total 24
drwxr-xr-x 3 root root 4096 Feb 7 18:17 a
drwxr-xr-x 3 root root 4096 Feb 7 18:17 b
drwxr-xr-x 3 root root 4096 Feb 7 18:17 c
drwxr-xr-x 2 root root 4096 Feb 7 18:22 f
drwxr-xr-x 2 root root 4096 Feb 7 18:22 g
drwxr-xr-x 3 root root 4096 Feb 7 18:23 o
[root@VM-0-7-centos ~]# cd o/p/q
[root@VM-0-7-centos q]# pwd
/root/o/p/q
[root@VM-0-7-centos q]#
7.touch 创建普通文件
语法:
touch 文件名 (如果文件存在,则修改该文件的时间属性,如果文件不存在,则新建空白文件)
eg:
[root@VM-0-7-centos ~]# rm -fr *
[root@VM-0-7-centos ~]# ll
total 0
[root@VM-0-7-centos ~]# touch a b c
[root@VM-0-7-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 7 18:41 a
-rw-r--r-- 1 root root 0 Feb 7 18:41 b
-rw-r--r-- 1 root root 0 Feb 7 18:41 c
[root@VM-0-7-centos ~]# touch a b c d
[root@VM-0-7-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 7 18:42 a
-rw-r--r-- 1 root root 0 Feb 7 18:42 b
-rw-r--r-- 1 root root 0 Feb 7 18:42 c
-rw-r--r-- 1 root root 0 Feb 7 18:42 d
[root@VM-0-7-centos ~]#
8.ln 创建连接文件
语法:
ln -s 源文件名 链接文件名
eg:
[root@VM-0-7-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 7 18:42 a
-rw-r--r-- 1 root root 0 Feb 7 18:42 b
-rw-r--r-- 1 root root 0 Feb 7 18:42 c
-rw-r--r-- 1 root root 0 Feb 7 18:42 d
[root@VM-0-7-centos ~]# ln -s a g
[root@VM-0-7-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 7 18:42 a
-rw-r--r-- 1 root root 0 Feb 7 18:42 b
-rw-r--r-- 1 root root 0 Feb 7 18:42 c
-rw-r--r-- 1 root root 0 Feb 7 18:42 d
lrwxrwxrwx 1 root root 1 Feb 7 18:53 g -> a
[root@VM-0-7-centos ~]#
9.rmdir、rm -fr 删除文件
语法:
语法 | 介绍 |
---|---|
rmdir 文件夹名 | 只能删除空文件夹 |
rm -fr * | 强制删除当前目录下的所有文件,且没有任何提示 !!!不要在根目录下操作 |
rm -fr 文件名 | 删除当前目录下的该文件夹,且没有任何提示,可以删除任意文件,包括含有层级的文件夹 |
eg:
[root@VM-0-7-centos ~]# mkdir 1 2 3/4 -p
[root@VM-0-7-centos ~]# ll
total 12
drwxr-xr-x 2 root root 4096 Feb 7 19:01 1
drwxr-xr-x 2 root root 4096 Feb 7 19:01 2
drwxr-xr-x 3 root root 4096 Feb 7 19:01 3
-rw-r--r-- 1 root root 0 Feb 7 18:42 a
-rw-r--r-- 1 root root 0 Feb 7 18:42 b
-rw-r--r-- 1 root root 0 Feb 7 18:42 c
-rw-r--r-- 1 root root 0 Feb 7 18:42 d
lrwxrwxrwx 1 root root 1 Feb 7 18:53 g -> a
[root@VM-0-7-centos ~]# rmdir 1
[root@VM-0-7-centos ~]# ll
total 8
drwxr-xr-x 2 root root 4096 Feb 7 19:01 2
drwxr-xr-x 3 root root 4096 Feb 7 19:01 3
-rw-r--r-- 1 root root 0 Feb 7 18:42 a
-rw-r--r-- 1 root root 0 Feb 7 18:42 b
-rw-r--r-- 1 root root 0 Feb 7 18:42 c
-rw-r--r-- 1 root root 0 Feb 7 18:42 d
lrwxrwxrwx 1 root root 1 Feb 7 18:53 g -> a
[root@VM-0-7-centos ~]# rm -fr 3
[root@VM-0-7-centos ~]# ll
total 4
drwxr-xr-x 2 root root 4096 Feb 7 19:01 2
-rw-r--r-- 1 root root 0 Feb 7 18:42 a
-rw-r--r-- 1 root root 0 Feb 7 18:42 b
-rw-r--r-- 1 root root 0 Feb 7 18:42 c
-rw-r--r-- 1 root root 0 Feb 7 18:42 d
lrwxrwxrwx 1 root root 1 Feb 7 18:53 g -> a
[root@VM-0-7-centos ~]# rm -fr *
[root@VM-0-7-centos ~]# ll
total 0
[root@VM-0-7-centos ~]#
10.vi/vim 编辑文件
day02
11.cat,head,tail,more,less查看文件
语法:
语法 | 介绍 |
---|---|
cat 文件名 | 查看并展示文件所有内容 |
cat 文件名 -n | 左边标注行号,查看并展示文件所有内容 |
head 文件名 | 默认显示前10行 |
head -n 文件名 | 显示前n行的内容,且参数-n需要写在文件名前面 |
tail 文件名 | 默认显示倒数第10行的文件内容 |
tail -n 文件名 | 显示倒数n行的内容 |
tail -f 文件名 | 动态的查看倒数10行的文件内容 |
tail -nf 文件名 | 动态的查看倒数n行的文件内容 |
more 文件名 | 以百分比的方式查看文件内容 |
less 文件名 | 以页的方式查看文件内容 |
eg:
cat
[root@VM-0-7-centos ~]# cat 1
son-shang
然后怎样
...
他一个人
全世界失眠
十年(明年今日国语版)
[root@VM-0-7-centos ~]# cat 1 -n
1 son-shang
2 然后怎样
...
235 他一个人
236 全世界失眠
237 十年(明年今日国语版)
head
[root@VM-0-7-centos ~]# head 1
son-shang
然后怎样
稳稳的幸福(尤克里里谱)
无条件
喜帖街(吉他弹唱谱)
陳奕迅 - 單車 -
红玫瑰
十年(Chord Solo)
一个人港剧《洗冤录》主题曲
最冷一天
[root@VM-0-7-centos ~]# head -5 1
son-shang
然后怎样
稳稳的幸福(尤克里里谱)
无条件
喜帖街(吉他弹唱谱)
tail
[root@VM-0-7-centos ~]# tail 1
情深怎么办
单车
shall we talk
时光倒流二十年
寂寞让你更快乐
爱情转移(富士山下)
爱情转移(富士山下)
他一个人
全世界失眠
十年(明年今日国语版)
[root@VM-0-7-centos ~]# tail -15f 1
K歌之王
你的背包
幸福摩天轮
情深怎么办
婚礼的祝福
情深怎么办
单车
shall we talk
时光倒流二十年
寂寞让你更快乐
爱情转移(富士山下)
爱情转移(富士山下)
他一个人
全世界失眠
十年(明年今日国语版) #动态实时显示写入的数据,按Ctrl+C退出
more
[root@VM-0-7-centos ~]# more 1
son-shang
然后怎样
稳稳的幸福(尤克里里谱)
...
浮夸
对不起谢谢
岁月如歌吉他
--More--(13%)
less
[root@VM-0-7-centos ~]# less 1
son-shang
然后怎样
稳稳的幸福(尤克里里谱)
...
对不起谢谢
岁月如歌吉他
1
注意:more及less的翻页按键
按键 | 适用命令 |
---|---|
Enter,一行一行往下翻 | more,less |
空格键,一页一页往下翻 | more,less |
B,一页一页往上翻 | more,less |
Q,退出查看 | less |
12.find 查找文件
语法:
按键 | 适用命令 |
---|---|
find 路径 -name 文件名 | 查询路径下名字为文件名的所有文件,包含普通文件跟目录文件 |
find 路径 -name ‘*’ | 查询路径下所有的文件,包含以.开头的隐藏文件 |
find 路径 -name ‘*a’ | 查询路径下以a结尾的所有文件 |
find 路径 -name ‘[1-3]*’ | 查询路径下以1~3开头的所有文件 |
find 路径 -type f -name 文件名 | 查询路径下名字为文件名的所有普通文件 |
eg:
find 路径 -name
[root@VM-0-7-centos /]# find /root -name a
/root/a
find 路径 -name ‘*’
[root@VM-0-7-centos /]# find /root -name '*'
/root
/root/3.txt
/root/1
/root/2.txt
/root/4.txt
...
/root/.tcshrc
/root/a
/root/a/e
/root/.cshrc
/root/.viminfo
/root/d
/root/1.txt
find 路径 -name ‘*a’
[root@VM-0-7-centos /]# find /root -name '*.txt'
/root/3.txt
/root/2.txt
/root/4.txt
/root/1.txt
find 路径 -name ‘[1-3]*’
[root@VM-0-7-centos /]# find /root -name '[1-3]*'
/root/3.txt
/root/1
/root/2.txt
/root/1.txt
find 路径 -type f -name 文件名
[root@VM-0-7-centos /]# find /root -type f -name 1
/root/1
13.cp 拷贝
语法:
命令语法 | 介绍 |
---|---|
cp 源文件/路径名 文件名/路径名 | 复制一个文件,并取名,如果存在取名的同名文件,则提示是否覆盖文件内容,如果不存在同名,则新生成取名文件 |
cp 源文件/路径名 文件夹名/路径名 | 复制一个文件到文件夹内,如果不存在同名文件,则名称不变,如果存在同名文件,提示是否覆盖文件内容 |
cp -r 源文件/路径名 文件夹名/路径名 | 复制一个文件夹到另一个文件夹内,如果存在同名的,则直接复制到该文件夹下,如果不存在同名的,则生成一个新的文件夹 |
eg:
cp 源文件/路径名 文件名/路径名
[root@VM-0-7-centos ~]# ll
total 8
-rw-r--r-- 1 root root 0 Feb 8 18:13 1.txt
-rw-r--r-- 1 root root 0 Feb 8 18:13 2.txt
-rw-r--r-- 1 root root 0 Feb 8 18:13 3.txt
drwxr-xr-x 2 root root 4096 Feb 8 18:14 a
drwxr-xr-x 2 root root 4096 Feb 8 18:14 b
[root@VM-0-7-centos ~]# vim 1.txt
[root@VM-0-7-centos ~]# cat 1.txt
111
[root@VM-0-7-centos ~]# cat 2.txt
[root@VM-0-7-centos ~]# cp 1.txt 2.txt # 复制文件1.txt并命名为2.txt
cp: overwrite `2.txt'? y #因为存在2.txt文件,则跳出提示,是否覆盖2.txt的内容
[root@VM-0-7-centos ~]# cat 2.txt
111
[root@VM-0-7-centos ~]# cp 1.txt 4.txt #复制文件1.txt并命名为4.txt
[root@VM-0-7-centos ~]# ll
total 20
-rw-r--r-- 1 root root 4 Feb 8 18:15 1.txt
-rw-r--r-- 1 root root 4 Feb 8 18:17 2.txt
-rw-r--r-- 1 root root 0 Feb 8 18:13 3.txt
-rw-r--r-- 1 root root 4 Feb 8 18:18 4.txt
drwxr-xr-x 2 root root 4096 Feb 8 18:14 a
drwxr-xr-x 2 root root 4096 Feb 8 18:14 b
cp 源文件/路径名 文件夹名
[root@VM-0-7-centos ~]# cp 1.txt a #复制文件1.txt到a文件夹中
[root@VM-0-7-centos ~]# cp 1.txt a
cp: overwrite `a/1.txt'? y #复制文件1.txt到a文件夹中,因为存在同名1.txt文件,则跳出提示,是否覆盖
[root@VM-0-7-centos ~]# ll a
total 4
-rw-r--r-- 1 root root 4 Feb 8 18:20 1.txt
cp -r 源文件/路径名 文件夹名/路径名
[root@VM-0-7-centos ~]# cp -r a b/c #复制a文件夹到b文件夹,并命名为c
[root@VM-0-7-centos ~]# ll b/c
total 4
-rw-r--r-- 1 root root 4 Feb 8 18:22 1.txt
[root@VM-0-7-centos ~]# cp -r a b/c #复制a文件夹到b文件夹,并命名为c,因为存在c文件夹,则直接将a复制到c文件夹中
[root@VM-0-7-centos ~]# ll b/c
total 8
-rw-r--r-- 1 root root 4 Feb 8 18:22 1.txt
drwxr-xr-x 2 root root 4096 Feb 8 18:22 a
14.>、>> 输出重定向
语法:
命令语法 | 介绍 |
---|---|
命令 > 文件名 | 将命令执行的正确输出结果输出到文件中,如果该文件里面有数据则会清空数据,重新写入 |
命令 >> 文件名 | 将命令执行的正确输出结果输出到文件中,如果该文件里面有数据,则会在原有数据后面继续写入 |
命令 2> 文件名 或者 命令 2>> 文件名 | 将命令执行的错误提示结果,输出到文件中 |
命令 > 文件名 2>&1 或者命令 >> 文件名 2>&1 | 将命令执行的结果(包含正确跟错误的提示),输出到文件中 |
eg:
命令 > 文件名
[root@VM-0-7-centos ~]# touch 1.txt 2.txt 3.txt
[root@VM-0-7-centos ~]# vim 1.txt
[root@VM-0-7-centos ~]# cat 1.txt
111111
[root@VM-0-7-centos ~]# cat 1.txt > 2.txt;cat 2.txt
111111
命令 >> 文件名
[root@VM-0-7-centos ~]# cat 1.txt >>2.txt;cat 2.txt
111111
111111
命令 2> 文件名 或者 命令 2>> 文件名
[root@VM-0-7-centos ~]# cat 4.txt 2> 3.txt ;cat 3.txt
cat: 4.txt: No such file or directory
[root@VM-0-7-centos ~]# cat 4.txt 2>>3.txt ;cat 3.txt
cat: 4.txt: No such file or directory
cat: 4.txt: No such file or directory
命令 > 文件名 2>&1 或者命令 >> 文件名 2>&1
[root@VM-0-7-centos ~]# cat 1.txt >>3.txt 2>&1;cat 3.txt
cat: 4.txt: No such file or directory
cat: 4.txt: No such file or directory
111111
[root@VM-0-7-centos ~]# cat 4.txt >>3.txt 2>&1;cat 3.txt
cat: 4.txt: No such file or directory
cat: 4.txt: No such file or directory
111111
cat: 4.txt: No such file or directory
[root@VM-0-7-centos ~]# cat 1.txt 4.txt >3.txt 2>&1;cat 3.txt
111111
cat: 4.txt: No such file or directory
day03
15. mv 剪切、重命名
语法:
命令 | 介绍 |
---|---|
mv 原文件名(文件夹) 新文件名(文件夹) | 重命名一个文件(文件夹),如果有同名,则提示是否覆盖 |
mv 原文件(文件夹) 文件夹名 | 剪切一个文件(文件夹)到另一个文件夹中,如果有同名,则提示是否覆盖 |
mv 原文件(文件夹)名 文件夹/新文件(文件夹)名 | 剪切一个文件(文件夹)到另外一个文件夹中,并重命名 |
eg:
mv 原文件名(文件夹) 新文件名(文件夹)
[root@VM-0-7-centos ~]# rm -fr *
[root@VM-0-7-centos ~]# touch 1.txt 2.txt;mkdir a b c
[root@VM-0-7-centos ~]# mv 1.txt 3.txt;ll
total 12
-rw-r--r-- 1 root root 0 Feb 9 17:18 2.txt
-rw-r--r-- 1 root root 0 Feb 9 17:18 3.txt
drwxr-xr-x 2 root root 4096 Feb 9 17:18 a
drwxr-xr-x 2 root root 4096 Feb 9 17:18 b
drwxr-xr-x 2 root root 4096 Feb 9 17:18 c
[root@VM-0-7-centos ~]# mv 3.txt 2.txt
mv: overwrite `2.txt'? y
[root@VM-0-7-centos ~]# ll
total 12
-rw-r--r-- 1 root root 0 Feb 9 17:18 2.txt
drwxr-xr-x 2 root root 4096 Feb 9 17:18 a
drwxr-xr-x 2 root root 4096 Feb 9 17:18 b
drwxr-xr-x 2 root root 4096 Feb 9 17:18 c
mv 原文件(文件夹) 文件夹名
[root@VM-0-7-centos ~]# mv 2.txt a;ll;ll a
total 12
drwxr-xr-x 2 root root 4096 Feb 9 17:19 a
drwxr-xr-x 2 root root 4096 Feb 9 17:18 b
drwxr-xr-x 2 root root 4096 Feb 9 17:18 c
total 0
-rw-r--r-- 1 root root 0 Feb 9 17:18 2.txt
mv 原文件(文件夹)名 文件夹/新文件(文件夹)名
[root@VM-0-7-centos ~]# mv a b/d;ll;ll b/d
total 8
drwxr-xr-x 3 root root 4096 Feb 9 17:20 b
drwxr-xr-x 2 root root 4096 Feb 9 17:18 c
total 0
-rw-r--r-- 1 root root 0 Feb 9 17:18 2.txt
16.useradd新建用户,groupadd 新建用户组,passwd账号设置密码
语法:
命令 | 介绍 |
---|---|
useradd/useradd 用户名 | 新建用户,会在/home目录下创建一个用户目录;在/etc/passwd下面能查看用户 |
passwd 用户名 | 设置账号密码 |
groupadd 用户组名 | 新建用户组,在/etc/group 下面查看用户组 |
useradd 用户名 -p 用户组名 | 新建用户到用户组内 |
su 用户名 | 管理员账号登陆时,切换为其他用户 |
exit | 退出当前账号,返回管理员账号 |
eg:
useradd/useradd 用户名
[root@VM-0-7-centos ~]# useradd whw #新建账号whw
[root@VM-0-7-centos ~]# vim /etc/passwd #查看新建的账号,最后一行是最新建的
su 用户名、exit
[root@VM-0-7-centos ~]# su whw #切换whw账号
[whw@VM-0-7-centos root]$ exit #退出当前账号,返回管理员账号
exit
[root@VM-0-7-centos ~]#
passwd 用户名
[root@VM-0-7-centos ~]# passwd whw #给whw账号设置密码
Changing password for user whw.
New password: #输入密码---密码不显示
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password: #再次确认密码---密码也不显示
passwd: all authentication tokens updated successfully.
groupadd 用户组名
[root@VM-0-7-centos ~]# groupadd zu1 #新建用户组zu1
[root@VM-0-7-centos ~]# cat /etc/group #查看用户组是否新建成功
....
rpcuser:x:29:
nfsnobody:x:65534:
zs:x:501:
whw:x:503:
zu1:x:504:
useradd 用户名 -p 用户组名
[root@VM-0-7-centos ~]# useradd hhx -p zu1 #新建用户hhx到用户组zu1中
[root@VM-0-7-centos ~]# cat /etc/passwd
...
zs:x:500:501::/home/zs:/bin/bash
whw:x:502:503::/home/whw:/bin/bash
hhx:x:503:505::/home/hhx:/bin/bash
17.userdel删除用户,groupdel删除用户组
语法:
命令 | 介绍 |
---|---|
userdel 用户名 | 删除用户,但是不删除用户家目录 |
userdel -fr 用户名 | 删除用户,同时删除用户家目录 |
groupdel 用户组名 | 删除用户组 |
eg:
userdel 用户名
[root@VM-0-7-centos ~]# useradd whw1 #新建用户whw1
[root@VM-0-7-centos ~]# userdel whw1;ll /home #删除用户whw1,并查看用户家目录
total 12
drwx------ 2 hhx hhx 4096 Feb 9 21:38 hhx
drwx------ 2 whw whw 4096 Feb 9 20:53 whw
drwx------ 2 504 506 4096 Feb 9 21:50 whw1 #whw1家目录存在,未删除
[root@VM-0-7-centos ~]# su whw1 #切换账号,提示账号不存在,已删除
su: user whw1 does not exist
userdel -fr 用户名|删除用户,同时删除用户家目录
[root@VM-0-7-centos ~]# userdel -fr whw #删除用户whw
[root@VM-0-7-centos ~]# ll /home #查看用户家目录,用户whw的家目录已删除
total 8
drwx------ 2 hhx hhx 4096 Feb 9 21:38 hhx
drwx------ 2 504 506 4096 Feb 9 21:50 whw1
groupdel 用户组名
[root@VM-0-7-centos ~]# groupdel zu1 #删除zu1用户组
[root@VM-0-7-centos ~]# cat /etc/group #查看用户组,显示已经删除
day04
18.alias 取别名,unalias取消别名
语法:
命令 | 介绍 |
---|---|
alias 别名=‘命令’ | 给命令取一个别名,后续直接输入别名即可执行命令 |
unalias 别名 | 取消别名 |
eg:
alias 别名=‘命令’
[root@VM-0-7-centos ~]# alias lla='ls -al'
[root@VM-0-7-centos ~]# lla
total 112
dr-xr-x---. 6 root root 4096 Feb 9 20:52 .
dr-xr-xr-x. 24 root root 4096 Feb 10 11:24 ..
drwxr-xr-x 3 root root 4096 Feb 9 17:20 b
-rw-r--r-- 1 root root 47625 Feb 10 11:24 .bash_history
-rw-r--r--. 1 root root 18 May 20 2009 .bash_logout
-rw-r--r--. 1 root root 176 May 20 2009 .bash_profile
-rw-r--r--. 1 root root 176 Sep 23 2004 .bashrc
drwxr-xr-x 2 root root 4096 Feb 9 17:18 c
-rw-r--r--. 1 root root 100 Sep 23 2004 .cshrc
-rw------- 1 root root 35 Feb 8 14:56 .lesshst
drwxr-xr-x 2 root root 4096 Dec 27 10:11 .pip
-rw-r--r-- 1 root root 73 Dec 27 10:11 .pydistutils.cfg
drwx------ 2 root root 4096 Mar 13 2018 .ssh
-rw-r--r--. 1 root root 129 Dec 4 2004 .tcshrc
-rw------- 1 root root 4446 Feb 9 20:52 .viminfo
unalias 别名
[root@VM-0-7-centos ~]# unalias lla
[root@VM-0-7-centos ~]# lla
-bash: lla: command not found
19. wc 统计文件的行数,字数,字节数
语法:
命令 | 介绍 |
---|---|
wc 文件名 | 统计行数,字数,字节数 |
参数 | 介绍 |
---|---|
-l | 统计行数 |
-m | 统计字符数 |
-c | 统计字节数 |
-L | 统计最长一行的长度 |
eg:
[root@VM-0-7-centos b]# cat 1.txt
111-222-333-
444 555 666
777 888 999
[root@VM-0-7-centos b]# wc 1.txt
3 7 37 1.txt #统计行数,字数,字节数
[root@VM-0-7-centos b]# wc -l 1.txt
3 1.txt #统计行数
[root@VM-0-7-centos b]# wc -m 1.txt
37 1.txt #统计字符数
[root@VM-0-7-centos b]# wc -c 1.txt
37 1.txt #统计字节数
[root@VM-0-7-centos b]# wc -L 1.txt
12 1.txt #统计最长一行的长度
[root@VM-0-7-centos b]#
20.| 管道符
语法:
命令1 | 命令2 ---- 命令1的出参作为命令2的入参
eg:
[root@VM-0-7-centos b]# ll
total 8
-rw-r--r-- 1 root root 37 Feb 10 13:02 1.txt
drwxr-xr-x 2 root root 4096 Feb 9 17:19 d
[root@VM-0-7-centos b]# ll | wc -l
3
21.ps 进程(正在运行的程序)
常见语法 | 介绍 |
---|---|
ps -ef | 以长格式显示所有进程 |
eg:
[root@VM-0-7-centos b]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 2020 ? 00:01:27 /sbin/init
root 2 0 0 2020 ? 00:00:01 [kthreadd]
root 3 2 0 2020 ? 00:00:00 [migration/0]
......