2023年7月26日发(作者:)
Linux基本操作命令(⼀)⽬录
1.⽂件和⽬录操作命令2.⽤户和⽤户组操作命令编辑器操作命令4.打包和解压操作命令5.系统操作命令
为什么要学习linux?
是⼀款操作系统经常来作为服务器操作系统来⽤作⽤:1.查看⽇志(任何操作的⽇志)2.定位bug(查看⽇志,⽇志级别:info/)3.搭建环境(搭建测试环境)以上三条就是测试⼈员会在⼯作经常要⽤的三点
需要的软件
6.8----linux操作系统 5---远程连接⼯具其它还有PYTT ---windows与linux传输⼯具
⽂件和⽬录操作命令
终端共有6个,分别tty1---tty6 通过ctrl + alt+F1~F6进⾏切换
2.[root@localhost ~]# 后光标闪烁表⽰可以执⾏命令/编辑 root===表⽰当前登录的⽤户名 @----表⽰在 localhost---主机域名 ~ ---- 表⽰root⽤户的家⽬录 root⽤户的家⽬录路径:/root 普通⽤户的家⽬录 ~普通⽤户的家⽬录 /home/普通⽤户的⽤户名⽬录 # ---- 表⽰是root $ ---表⽰是普通⽤户
3.快捷键 ctrl+c 终⽌当前命令
ctrl+l 清屏 ctrl+D =exit 当前⽤户退出 ctrl+z 暂停当前命令 fg---恢复执⾏ TAB键----⾃动补全如果输⼊的字符串是唯⼀的,单击tab键就会⾃动补全如果输⼊的字符串不是唯⼀的,双击tab键显⽰所有以输⼊的字符开头的⽬录或⽂件linux命令⼀样的可以⾃动补全注意点:⾃动补全只补全当前⽬录下存在的⽬录或⽂件名称
4.⽬录/----表⽰根⽬录常⽤⽬录 /home 普通⽤户的家⽬录 /root 是root⽤户的家⽬录 不管是普通⽤户还是root 登录成功后,所在位置都是在各⾃的家⽬录下 /etc 配置⽂件位置(⽤户、密码、服务的配置⽂件等) /opt 保存第三⽅软件安装位置 /usr/local 安装第三⽅软件的位置 /tmp 临时⽂件,⾃动清空 不管是存放软件包或者⽂件时,都去新建相应的⽬录,最好是当前登录⽤户家⽬录下⾯,不要随意删除别⼈的⽂件
5.绝对路径和相对路径 什么是绝对路径?什么是相对路径? 绝对路径是从根⽬录开始的完整路径 相对路径是从当前位置(⽤户所在的路径位置)开始的路径 . 表⽰当前⽬录 .. 表⽰上⼀级⽬录
pwd
pwd --查看当前位置的路径(是从根⽬录开始)
cd
cd ----切换⽬录
ls
---list---显⽰当前⽬录或指定⽬录下⽂件和⽬录经常使⽤的选项:ls -l ==ll ---查看当前⽬录下的⽂件和⽬录,以较长格式显⽰⽂件和⽬录显⽰格式讲解:第⼀个 -/d/l 表⽰⽂件类型 - ⼆进制⽂件 d ⽬录 l 软链接第⼆个部分 rwxrwxrwx 表⽰各类⽤户的权限r -read 读权限 w-wirte 写权限 x ---执⾏权限r-x 表⽰只有读和执⾏权限
rwx rwx rwx
第⼀个rwx表⽰⽂件或⽬录所属⽤户的权限第⼆个rwx表⽰⽂件或⽬录所属组的权限第三个rwx表⽰⽂件或⽬录其他⽤户的权限第三个部分 root 表⽰⽂件或⽬录所属⽤户的⽤户名第四个部分 root 表⽰⽂件或⽬录所属组的组名第五个部分 12288 表⽰⽂件或⽬录的⼤⼩第六个部分 Feb 25 18:41 表⽰⽂件或⽬录的最后修改时间第七个部分 ⽂件或⽬录的名称
-a ==ls --all 查看当前⽬录下的所有⽂件和⽬录,包括隐藏⽂件
-h 表⽰以容易理解的格式显⽰⽂件的⼤⼩, 必须与-l⼀起使⽤,才能有所体现
-i 显⽰⽂件和⽬录的i节点每⼀个⽂件和⽬录的i节点都是唯⼀的,我们可以通过i节点来查找⽂件和⽬录
mkdir
mkdir --创建新⽬录格式:mkdir [-p] 路径/新⽬录名1.在当前⽬录下创建新⽬录2.在指定的⽬录下创建新⽬录3.-p, --parents no error if existing, make parent directories as needed ---递归创建⽬录 rmdir
rmdir ---删除空⽬录1.如果是⽬录是⾮空,不能删除[root@localhost cc]# rmdir test/rmdir: failed to remove `test/': Directory not empty'2.不能删除⾮⽬录[root@localhost ~]# rmdir ir: failed to remove `': Not a directory'
rm
rm ---删除⽂件和⽬录⽤法:rm [选项]... ⽂件...不管是⽂件还是⽬录,经常使⽤就是 rm -rf 要删除的东西(可以是多个,中间⽤空格隔开)[root@localhost tmp]# rm -rf cc test
rm -rf *---删除当前⽬录下所有⽂件和⽬录
rm -rf te*--删除以te开头的所有⽂件和⽬录删除以.1og结尾的内容:rm -rf *.log使⽤rm命令时,⼀定确保这个⽂件和⽬录确定要删除,在liunx系统⾥⾯删除是不可逆的,没有回收站
1.删除⽂件[root@localhost tmp]# rm ifcfg-eth0rm:是否删除普通⽂件 "ifcfg-eth0"?y[root@localhost tmp]# rm -f --删除不询问
2.删除⽬录--- -r 同时会询问是否删除[root@localhost test_dir]# rm -r test/rm: descend into directory `test'? yrm: remove directory `test/testt'? yrm: remove directory `test'? y'
3.如果不想在删除过程⼀直有询问,可以使⽤ -f---强制删除(需要更加谨慎) force[root@localhost test_dir]# rm -rf test/强制删除,并且没有询问如果有些⽂件使⽤rm 不能删除,我们也可以加上-f,同样的含义强制删除,并且没有询问
cp
cp --复制⽂件或⽬录⽤法:cp [选项] 原⽂件或⽬录 ⽬标⽬录语法:cp -rp [原⽂件或⽬录][⽬标⽬录]
1.复制⽂件:[root@localhost test_dir]#cp /etc/sysconfig/network-scripts/ifcfg-eth0 /tmp/test[root@localhost test_dir]#cp /etc/sysconfig/network-scripts/ifcfg-eth0 ..[root@localhost test_dir]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /tmp
2.-p 保留原⽂件或⽬录的属性,属性就是ls -l所展⽰的东西[root@localhost tmp]# cp -p /etc/sysconfig/network-scripts/ifcfg-eth0 /tmp
3.-r 复制⽬录[root@localhost tmp]# cp -r ./test_dir/test .结论:cp 复制⽂件或⽬录,复制成功后,原⽂件或⽬录还存在在复制的过程中,还可以改名
mv
mv与cp 最⼤的区别就是⼀个不保留原⽂件,⼀个保留原⽂件
1.剪切--不改名[root@localhost test_dir]# mv ../ifcfg-eth0 .
2.剪切-改名[root@localhost tmp]# mv test_dir/ifcfg-eth0 ./
3.改名[root@localhost test_dir]# mv test tests
作⽤:⽤来备份场景:放了⼀个lib⽂件运⾏,开发说你替换⼀下lib⽂件但是我们保证开发的lib的⽂件就是OK ,那这种情况最好备份mv touch
touch -创建空⽂件[root@localhost test]# touch 这个命令不经常⽤,创建⽂件是vim编辑器
cat
cat 显⽰⽂件内容[root@localhost test]# cat ⼀次性将⽂件内容完全展⽰出来,并且退出从⽽可得出cat不适合查看⽂件很⼤的⽂件,cat只适合于查看⽂件内容偏⼩的⽂件
tac
tac 倒着显⽰⽂件内容[root@localhost test]# tac 适合查看⽂件内容偏⼩的,并且将最⼀⾏显⽰在最前⾯
more
more 分页显⽰⽂件的内容空格或f 向下翻页enter键 换⾏(⼀⾏⼀⾏的显⽰)同时在页⾯最下⽅的位置显⽰⽂件显⽰的当前进度q或Q 退出缺点:只能向下翻页,不能向上查看
less
less 分页显⽰⽂件的内容(向上翻页)空格或f 向下翻页enter键 换⾏q或Q 退出pgup/pgdn 向上/向下⼀页⼀页切换⽅向键 向上/向下⼀⾏⼀⾏切换
在⽂件中查找指定字符串时:1.先输⼊ /2.在/后⾯输⼊要查找的指定字符串⽐如:/error3.回车⽬前显⽰的⼀个指定字符串是从当前位置查找到的第⼀个,可以按“n”继续向下查找其它的
此⽅法经常⽤于在⽇志定位问题,或在修改⽂件要修改指定内容less是显⽰⽂件内容命令中⽤得最多的
head
head --显⽰⽂件前⾯⼏⾏1.在不跟任何选项和参数时,默认显⽰⽂件的前⼗⾏[root@localhost test]# head
2.只显⽰⽂件的前⾯5⾏(⾏数前的-很重要)[root@localhost test]# head -5
tail
tail 显⽰⽂件最后的⼏⾏1.默认显⽰⽂件的最后⼗⾏[root@localhost test]# tail
2.只显⽰⽂件的最后三⾏ -n[root@localhost test]# tail -3 ==[root@localhost test]# tail -n 3
3.-f 动态显⽰⽂件的最后⼏⾏(经常⽤)tail -400f 动态就是显⽰⽂件新增的内容
⽤于场景:主要⽤于实时动态显⽰操作⽇志同样是⽤于定位bug
chmod
chmod -改变⽂件或⽬录的权限(读写执⾏)rw- r-- r--user group other所有者 所有组 其它⼈+ 增加 - 删除 = 等于
1.将⽂件的写权限删除[root@localhost test]# chmod u-w
2.给所有组增加执⾏的权限[root@localhost test]# chmod g+x
3.给其它⼈赋于读写执⾏的权限[root@localhost test]# chmod o=rwx
4.错误⽅式chmod u=rw o=r 这种⽅式不⾏,不能同时修改所有者、所有组和其它⼈的权限
以上根据⽤户来+-=三种⽅式来修改权限,显得过于⿇烦解决⽅法:r=4 w=2 x=14+2+1=7 ⽤7这⼀个数字来代表所有权限
1.给所有者赋读写权限,所有组为读执⾏权限 其它⼈赋读权限[root@localhost test]# chmod 654 通过数字的⽅式同时修改不同⽤户的权限
2.给所有⽤户都赋于读写权限6[root@localhost test]# chmod 666 ⽬前更改权限最简便的⽅法,也是最常⽤的
3.给⽬录赋权[root@localhost test]# chmod 764 test12/同时在⽂件或⽬录前可以加上绝对路径或相对路径 4.-R 递归修改⽂件或⽬录的权限(经常使⽤)[root@localhost test]# chmod -R 644 ./test12/test是同时将⽬录下⾯所有的⽂件和⼦⽬录中内容权限⼀并修改
chmod root和普通⽤户都有权限
chown
chown 改变⽂件的所有者(经常使⽤)1.改变test_⽂件的所有者为root[root@localhost test]# chown root test_
2.普通⽤户改变的所有者[test@localhost ~]$ chown test wn: changing ownership of `': Operation not permitted'
chown普通⽤户是没权限进⾏操作这个命令,只有root才有权限去改变⽂件或⽬录的所有者
chgrp
chgrp 改变⽂件和⽬录的所有组
[root@localhost test]# chgrp test test但是这个命令我们⼀般不⽤,因为很少会去只改变⽂件和⽬录的所有组
如果要改变所有组,⼀般会同时改变所有者和所有组如果是同时改变所有者和所有组,可以⽤chown
经常使⽤的⽅式[root@localhost test]# chown wq:wq test
umask
umask ---显⽰⽂件和⽬录的默认权限775/755 创建⼀个⽬录664/644 创建⼀个⽂件022 umask---取反--7557-0=7 7-2=5 7-2=5代表⼀个⽬录初始的权限为755,⼀个⽂件的初始权限为644(⽂件没有可执⾏权限) find
find ---⽂件搜索格式:find [搜索范围][匹配条件]
1.根据name来搜索(经常⽤) a.精确搜索[root@localhost test]# find /etc -name services b.模糊搜索init开头的⽂件和⽬录[root@localhost test]# find /etc -name init* c.模糊搜索init结尾的⽂件和⽬录[root@localhost test]# find /etc -name *init d.模糊搜索包含init的⽂件和⽬录[root@localhost test]# find /etc -name *init* e.模糊搜索以init开头的后⾯只有三个字符串的⽂件或⽬录 * 代表⼀组字符串 代表⼀个字符[root@localhost test]# find /etc -name initlinux对⼤⼩很敏感,⽽且空格也敏感[root@localhost test]# find /etc -name INIT f.根据name搜索时,不区分⼤⼩写⽤选项 -iname[root@localhost test]# find /etc -iname INIT/etc/inittab[root@localhost test]# find /etc -name INIT没有搜索出任何数据
2.根据⽂件⼤⼩搜索 a.⼤于4096的⽂件 +4096[root@localhost test]# find /etc -size +4096 b.⼩于4096的⽂件 -4096[root@localhost test]# find /etc -size -4096 c.等于4096的⽂件[root@localhost test]# find /etc -size 4096 d.⼤于4096同时⼩于641020的⽂件 -a[root@localhost test]# find /etc -size +4096 -a -size -641020 e.⼤于4096或者⼩于641020的⽂件[root@localhost test]# find /etc -size +4096 -o -size -641020 3.根据⽂件所有者去搜索[root@localhost test]# find /home/test -user test[root@localhost test]# find -user test如果不写搜索范围,默认搜索从当前⽬录开始,搜索下⾯匹配的⽂件和⽬录
4.根据⽂件属性搜索 a.搜索5分钟之内内容被修改的⽂件[root@localhost test]# find . -mmin -5⽂件内容被修改的同时,⽂件属性也会被修改 b.搜索3分钟之内⽂件属性被修改的⽂件[root@localhost test]# find . -cmin -3amin cmin mmin后⾯接分钟atime ctime mtime后⾯接⼩时
5.根据⽂件类型来搜索- ⼆进制⽂件f d ⽬录 l 软链接1.搜索⽬录---⽂件类型为d[root@localhost test]# find . -type d2.搜索当前⽬录下⽂件类型为⽂件 f[root@localhost test]# find . -type - ---错误find: -type 的参数未知: -[root@localhost test]# find . -type f ---正确3.搜索当前⽬录下的⽂件类型为软链接 l[root@localhost test]# find . -type l4.在根⽬录下搜索⽂件名称为services的⽂件[root@localhost test]# find / -name services -type f
grep
grep --搜索⽂件的内容1.搜索这个⽂件中包含qwer字符串的⾏[root@localhost test]# grep qwer ./
2.不区分⼤⼩写进⾏搜索 选项 -i[root@localhost test]# grep -i test
3.搜索不包含123的⽂件内容[root@localhost test]# grep -v 123 4.搜索不包含test的⽂件内容,不区分⼤⼩写[root@localhost test]# grep -iv test
5.搜索不是以1开头的⽂件内容[root@localhost test]# grep -v ^1
6.搜索以1开头的⾏[root@localhost test]# grep ^1 ^ 表⽰以什么开头
在shell脚本 #表⽰注释7.屏蔽掉注释⾏ --[root@localhost test]# grep -v ^#
8.经常使⽤的⼀种⽅法---管道 |[root@localhost test]# ps -ef |grep javaps -ef 查询进程|管道符 command1 | command2将command1的输出作为command2的输⼊[root@localhost test]# ls -l |grep test
ln
ln --链接命令链接分为软链接和硬链接
1.给⽬录创建软链接 -s[root@localhost tmp]# ln -s /tmp/test/test12/test ./test1lrwxrwxrwx. 1 root root 21 Apr 14 21:06 test1 -> /tmp/test/test12/test软链接的⼤⼩⾮常⼩,只是⼀个链接软链接相当于windows下⾯的快捷⽅式
2.给⽂件创建软链接[root@localhost tmp]# ln -s /tmp/test/test12/test/ .软链接的⽬标⽂件和原⽂件的i节点不同
3.给⽂件创建⼀个硬链接[root@localhost tmp]# ln /tmp/test/test12/test/ ./硬链接的i节点相同[root@localhost tmp]# ln /tmp/test/test12/test ./test_dirln: `/tmp/test/test12/test': hard link not allowed for directory'硬链接不能针对⽬录使⽤,只能对⽂件使⽤
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690363029a338082.html
评论列表(0条)