Linux基础命令详解
【摘要】 Linux操作系统的诞生、发展和成长过程始终依赖着五个重要支柱:Unix操作系统、MINIX操作系统、GNU计划、POSIX标准和Internet网络
Linux操作系统
快捷键
# ctrl shift +
# ctrl -
# ctrl shift c
# ctrl shift v
# ctrl l / clear
# ctrl shift t
# alt 1
# ctrl alt ←
# ctrl c
# ctrl d
# ctrl z
ctrl w 删除前面单词
ctrl d 删除后面字母
# ctrl alt f1 //切换图形化
# ctrl alt f2-f6 //切换字符
su,su -
# [centos@localhost ~]$ su user1
Password:
[user1@localhost centos]$
# [centos@localhost ~]$ su - user1
Password:
Last login: Tue Sep 14 00:51:14 PDT 2021 on pts/0
[user1@localhost ~]$
前导符
[root@loaclhost~] #
root:用户名
@:间隔符
loaclhost:主机名
~:表示当前工作目录,~表示家目录
#:当前用户身份,#代表管理员,$代表普通用户
tab
命令补全,常用tab键,如果按一次出不来就按两次,此时表示命令不唯一,如果超过两次还没有出来就表示命令错误。
man
man + 命令 ----- man ls
history
# history
!num ---- !60
!cmd ---- !rm
alias
[root@localhost ~]# alias wk='pwd'
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
alias wk='pwd'
[root@localhost ~]# wk
/root
[root@localhost ~]# pwd
/root
作业:将自己名字(全称拼音)设置成history,并查看
基本命令
ls
Linux下一切皆文件
命令格式:
ls
# ls //显示当前路径下的文件
命令格式:
ls [OPTION]
# ls -a //显示所有文件(包含以.或..开头的隐藏文件)
# ls --all
# ls -l //长格式显示文件信息
-rw-------. 1 root root 2770 Sep 7 01:36 anaconda-ks.cfg
文件类型:
-:普通文件(黑色)
d:目录/文件夹(蓝色)
l:链接文件(天蓝色)
rw-------:文件权限
1:节点数
root:用户名
root:组名
2770:文件大小
Sep 7 01:36:最后一次修改时间
anaconda-ks.cfg:文件夹
# ls -lh 人性化显示文件大小,必须配合-l使用
# ls -ld 显示自身属性,必须配合-l使用
命令格式:
ls [选项]... [FILE]
查看文件和文件夹的长格式信息(文件夹的长格式信息要多加一个d)
[root@localhost ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 2770 Sep 7 01:36 anaconda-ks.cfg
[root@localhost ~]# ls -l Documents
total 0
[root@localhost ~]# ls -ld Documents
drwxr-xr-x. 2 root root 6 Sep 14 22:32 Documents
# pwd(print working directory) 打印当前工作目录
绝对、相对路径
绝对:从/开始,具有唯一性
湖北省武汉市洪山区光谷广场
相对:从半路开始
洪山区光谷广场
cd 更改目录
cd (change directory)
# cd /etc/sysconfig/network-scripts/
# cd .. 返回上一层目录
# cd /tmp
# cd /var
# cd /etc
# cd - 返回上一次进入的目录(相当于遥控器的返回键)
1 115 117
/tmp /var /etc cd
# cd 返回到家目录
cat、more、less 查看文件内容
cat 只适合查看短文件
命令格式:cat [选项] 文件名
# cat /etc/passwd
命令格式:more 文件名
more 只能下翻,不能上翻
# more /etc/passwd
命令格式:less 文件名
less 既可上翻,又可下翻
# less /etc/passwd
head、tail 查看文件内容
命令格式:head [选项] 文件名
# head /etc/passwd
# head -n 3/etc/passwd
命令格式:tail [选项] 文件名
# tail /etc/passwd
# tail -n /etc/passwd
touch:1.创建文件;2.刷新时间戳
命令格式:touch 文件名
[root@localhost ~]# touch 1.txt
[root@localhost ~]# ls -l 1.txt
-rw-r--r--. 1 root root 0 Sep 15 01:37 1.txt
[root@localhost ~]# touch 1.txt
[root@localhost ~]# ls -l 1.txt
-rw-r--r--. 1 root root 0 Sep 15 01:39 1.txt
mkdir:创建文件夹
(建议创建在/下面)
命令格式: mkdir [选项] 目录名
# mkdir /test2
# mkdir /test2/test20
# ls -ld /test2/test20/
# mkdir /test3/test30 || no
# echo $?
# mkdir -p /test3/test30 || yes
rmdir:删除空文件夹
命令格式:mkdir 文件名
[root@localhost /]# ls /test2/test20/
[root@localhost /]# ls /test3/test30/
[root@localhost /]# ls /test40/
[root@localhost /]# rmdir /test2
rmdir: failed to remove ‘/test2’: Directory not empty
[root@localhost /]# rmdir /test2/test20/
[root@localhost /]# rmdir /test40
rm
-r 递归
-f 强制
命令格式:rm [选项] 文件名
[root@localhost ~]# ls /test3/test30
[root@localhost ~]# rm /test3
rm: cannot remove ‘/test3’: Is a directory
[root@localhost ~]# rm -f /test3
rm: cannot remove ‘/test3’: Is a directory
[root@localhost ~]# rm -r /test3
rm: descend into directory ‘/test3’? y
rm: remove directory ‘/test3/test30’? y
rm: remove directory ‘/test3’? ^C
[root@localhost ~]# rm -rf /test3/
[root@localhost ~]# echo $?
0
date
[root@localhost ~]# date
Wed Sep 15 01:40:00 PDT 2021
date 月日时分年点秒
[root@localhost ~]# date 091516412021.50
Wed Sep 15 16:41:50 PDT 2021
[root@localhost ~]# date
Wed Sep 15 16:41:55 PDT 2021
dd
案例1:cpu爆满
[root@localhost ~]# dd if=/dev/zero of=/dev/null
[root@localhost ~]# top
案例2:制作指定大小的文件
[root@localhost ~]# dd if=/dev/zero of=4M bs=2M count=2
6M(bs大小) 8M(数量) 10M
[root@localhost ~]# dd if=/dev/zero of=6M bs=6M count=1
[root@localhost ~]# dd if=/dev/zero of=8M bs=4M count=2
[root@localhost ~]# dd if=/dev/zero of=10M bs=5M count=2
echo回显
[root@localhost ~]# echo "123"
123
[root@localhost ~]# echo "456"
456
[root@localhost ~]# echo 456
cp (copy)复制
默认复制文件
命令格式:
cp [选项] 源文件 目标文件
[root@localhost ~]# cp 2M /tmp/
[root@localhost ~]# cp test1 /mnt/
cp: omitting directory ‘test1’
[root@localhost ~]#
[root@localhost ~]# cp -r test1 /mnt/
[root@localhost ~]# echo "123456" > 1.txt
[root@localhost ~]# echo "456" > 2.txt
[root@localhost ~]# cat 2.txt
456
[root@localhost ~]# cp 1.txt 2.txt
cp: overwrite ‘2.txt’? y
[root@localhost ~]# cat 2.txt
mv(remove)
命令格式:
mv 源文件 目标文件
[root@localhost ~]# mv 2M /mnt
[root@localhost ~]# ls
1.txt Desktop original-ks.cfg test1
2.txt Documents Pictures Videos
4M Downloads Public
anaconda-ks.cfg Music Templates
[root@localhost ~]# ls
1.txt Desktop original-ks.cfg test1
2.txt Documents Pictures Videos
4M Downloads Public
anaconda-ks.cfg Music Templates
[root@localhost ~]# mv 4M /mnt/10M
破解密码
1.重启虚拟机
2.按↑↓键暂停到启动界面吗,按e进入启动程序
3.找到linux16,并按end跳到行尾,添加rd.break
4.按ctrl x进入系统
5.mount -o remount,rw /sysroot //以读写方式重新挂载系统
6.chroot /sysroot //切换到/sysroot
7.echo "123456" | passwd --stdin root //设置root的密码为123456
8.touch /.autorelabel //给selinux打上下文关系标签
9.exit //退出
10.reboot //重启
|(管道符):将左边的执行结果传到右边
wc统计
命令格式:wc [选项] 文件名
-l 统计行数
-c 统计字符数
-w 统计单词
grep:过滤
命令格式:grep 关键字 文件名
[root@localhost ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
centos:x:1000:1000:Centos7:/home/centos:/bin/bash
1. vim 1.txt
2. i
3. 输入内容
4. esc
5. :wq
原文件:
[root@localhost ~]# cat 1.txt
123 we r tt uy u
root
#root
ROOT
-----------------------------------------------------
-i 忽略大小写
[root@localhost ~]# grep root 1.txt
root
#root
[root@localhost ~]# grep -i root 1.txt
root
#root
ROOT
-----------------------------------------------------
-v 取反
[root@localhost ~]# grep -v root 1.txt
123 we r tt uy u
ROOT
-----------------------------------------------------
^$ 过滤空行
[root@localhost ~]# grep ^$ 1.txt
-----------------------------------------------------
[root@localhost ~]# grep -v ^$ 1.txt
123 we r tt uy u
root
#root
ROOT
----------------------------------------------------
[root@localhost ~]# grep -v "^$|^#" 1.txt
123 we r tt uy u
root
#root
ROOT
[root@localhost ~]# grep -Ev "^$|^#" 1.txt
123 we r tt uy u
root
ROOT
1.关键字
2.^关键字
3.关键字$
4.-i
5.-v
6.^$ -v
作业:
1.统计root家目录下有多少文件
[root@localhost ~]# ls | wc -l
11
2.统计anaconda-ks.cfg文件有多少单词和字符
[root@localhost ~]# wc -w anaconda-ks.cfg
283 anaconda-ks.cfg
[root@localhost ~]# wc -c anaconda-ks.cfg
2770 anaconda-ks.cfg
3.过滤出/etc/shadow文件下带centos的行
[root@localhost ~]# grep centos /etc/shadow
centos:$1$c1ZA1AN5$lMitJP.Gyltx2Vo8on8NP0:18877:0:99999:7:::
4.过滤出/etc/shadow文件下以centos开头的行
[root@localhost ~]# grep ^centos /etc/shadow
centos:$1$c1ZA1AN5$lMitJP.Gyltx2Vo8on8NP0:18877:0:99999:7:::
基本命令作业
1.将环境的主机名改为你的姓名,如wangke
2.进入/tmp目录并显示当前工作目录
3.查看/etc/group文件的前3行和后5行
4.在/root下创建10.txt和100.txt,并用长格式显示文件信息
5.修改系统时间,与windows时间保持一致
6.创建文件夹qwer,并在qwer下创建qaz的文件夹
7.在/qwer/qaz下面创建xinchuang20303-4的文件
8.删除qwer目录
9.创建wangke文件夹,并删除
10.将/root下的10.txt复制到/tmp下
11.将/root下的100.txt3到/mnt下并重命名为200.txt
12.过滤出/etc/login.defs的空行和注释行
13.破解密码,将系统密码改为qwer,(把输入的命令截图)
参考答案
# cd
# head -n 3 /etc/group
# tail -n 5 /etc/group
# pwd
# touch {10,100}.txt
# ls
# ls -l 10.txt
# ls -l 100.txt
# date
# date 100613532021.45
# date
# mkdir /qwer/qaz
# mkdir -p /qwer/qaz
# touch /qwer/qaz/xinchuang20303-4
# cd /qwer/qaz/
# ls
# rm -rf /qwer/
# cd
# mkdir /wangke
# rmdir /wangke/
# ls
# ls | grep 10/txt
# ls | grep 10.txt
# cp 10.txt /tmp/
# cd /tmp/
# ls | grep 10.txt
# cd
# mv /root/100.txt /mnt/200.txt
# pwd
# ls | grep 100.txt
# cd /mnt/
# ls | grep 200.txt
# cd
# grep "^$|^#" /etc/login.defs
# grep -E "^$|^#" /etc/login.defs
# grep -E -v "^$|^#" /etc/login.defs
# history -a
通配符
*:匹配一个或多个任意字符
[root@wangke mnt]# ls
1.txt a.txt b.txt
ab.txt A.txt B.txt
[root@wangke mnt]# ls *.txt
1.txt a.txt b.txt
ab.txt A.txt B.txt
?:匹配单个字符
[root@wangke mnt]# ls ?.txt
1.txt A.txt B.txt
a.txt b.txt
[root@wangke mnt]# ls ??.txt
ab.txt
[]:匹配[]内的任意单个字符
[root@wangke mnt]# ls [abc].txt
a.txt b.txt
[^]、[!]:不匹配[]的任意单个字符
[root@wangke mnt]# ls
1.txt a.txt b.txt
ab.txt A.txt B.txt
[root@wangke mnt]# ls [ac].txta.txt
[root@wangke mnt]# ls [!ac].txt
1.txt A.txt b.txt B.txt
[root@wangke mnt]# ls [^ac].txt
1.txt A.txt b.txt B.txt
{}
{a..f}:a到f
[root@wangke mnt]# touch {a..f}.txt
[root@wangke mnt]# ls
a.txt b.txt c.txt d.txt e.txt f.txt
{A,C}:A和C
[root@wangke mnt]# touch {A,C}.txt
[root@wangke mnt]# ls
a.txt b.txt C.txt e.txt
A.txt c.txt d.txt f.txt
vim:创建、查看、编辑文件
vi和vim的区别
vim的vi的升级版
vim有高亮显示
vim的三种模式
命令模式:直接用vim打开文件就是命令模式,标识符:1.光标闪动,2.左下角有文件名、行数、字符数
输入模式:按aio任意字符即可进入输入模式,标识符:输入/中,insert/英文
末行模式:最后一行输入保存退出内容,标识符:”:(冒号)“
三种模式的切换
命令模式 aio ---->输入模式
<-----Esc
命令模式 :------>末行模式
<------Esc
aio
命令模式
a:光标所在字符后面输入
A:光标所在行行尾输入
I:光标所在行行首输入
i:光标所在字符前面输入
o:光标所在行下一行输入
O:光标所在行上一行输入
保存、退出
末行模式
w(保存)
q(退出)
wq/x(保存退出)
!表示强制
命令模式
ZZ(大写)(保存退出)
光标的移动
# vim /etc/passwd
命令模式
h:左
j:下
k:上
l:右
上下左右方向键
行内跳转
命令模式
^/home:到行首
$/end:到行尾
w:单词跳转
行间跳转
命令模式
gg:跳转到首行
G:跳转到尾行
3gg:跳转到第3行
末行模式
:20:跳转到到第20行
:%:跳转到最后一行
页面跳转
pageup:向上翻页
pagedown:向下翻页
复制
命令模式
yy:复制光标所在行
2yy:复制光标所在的2行,包含光标所在行
yw:复制单词
末行模式
:3,6y:复制3到6行
:%:复制全文
:7y:复制第7行
粘贴
命令模式
p(小写):光标下一行粘贴
P(大写):粘贴到光标上一行
apache网站
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
:set nu
G
ww
dw
a
将ONBOOT=NO改为ONBOOT=yes
[root@localhost ~]# systemctl restart network
1.ping通百度
2.安装软件
[root@localhost ~]# yum repolist
[root@localhost ~]# yum install -y httpd
3.关闭防火墙
[root@localhost ~]# systemctl stop firewalld.service
4.关闭selinux
[root@localhost ~]# setenforce 0
5.开启服务
[root@localhost ~]# systemctl restart httpd
6.自定义网站
[root@localhost ~]# echo 123456 > /var/www/html/index.html
删除
命令模式
dd:删除光标所在行
dw:删除单词
5dd:删除5行(包含光标所在行)
x/del:删除光标所在字符
D:删除光标所在行后半部分的字符(包含光标所在字符)
dgg:删除光标之前的所有行
dG:删除光标之后的所有行
末行模式:
:4d:删除第4行
:1,9d:删除1到9行
:%d:删除全文
撤回
命令模式
u:一步步撤回
查找
命令模式
/root:查找root
n:往下查找
N:往上查找
?root:
n:往上查找
N:往下查找
行号
末行模式
:set nu
:set nonu
:noh:取消高亮
替换
末行模式
:s/root/qq 将光标所在行的第一个root替换为qq
:s/root/qq/g 将光标所在行的所有root都替换为qq
:1,3s/root/qq/g 将1到3行的所有root都替换为qq
:%s/root/qq/g
批量注释
1.Ctrl v 进入可视化块模式
2.选中你要注释的行
3.按I(大写)
4.按#
5.esc(两下)
取消批量注释
1.ctrl v
2.选中你要取消注释的行
3.按d
读入、写入
末行模式
:r /etc/hosts 将/etc/hosts文件的内容读入当前文件里面光标所在行下一行
:w /tmp/2.txt 将此文件内容另存为到/tmp/2.txt文件里面
vim练习
1.创建1.txt并在里面写上两行内容(自定义)
2.将/etc/hosts文件内容读入此文件
3.将此文件另存为到/mnt/qq.txt
自动设置行号
1.vim /etc/vimrc
2.G
3.o
添加set number
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区),文章链接,文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
- 点赞
- 收藏
- 关注作者
评论(0)