当前位置: 首页 > Linux

Linux常用命令汇总_1

时间:2023-04-06 12:07:55 Linux

1.使用cd进入某个目录,可以使用绝对路径和相对路径1)绝对路径cd/data2)相对路径,进入上两层目录cd../../3)相对路径,进入Go到当前目录的test子目录cd./test2.ls#-l以列表方式显示#-r反向排序#-s显示文件大小#-t按修改时间排序#-R递归显示目录下的所有文件和目录#-a显示隐藏文件和上层目录(.,..)#-F如果是文件,后面会显示斜杠3.mkdir#创建文件夹mkdirtest#递归创建,如果已经存在,直接跳转使用mkdir-p/test1/test2/test34.find命令1)在系统根目录下找一个文件find/-nametest.txt2)按类型搜索,使用-type,type后面可以文件和目录dfind/-nametest.txt-typeaffind/-nametest.txt-typed3)根据修改时间查询#find/-name*.txt-mtime-1当天#20daysfind/-name*.txt-mtime+204)Searchbysize#单位是Kfind/-name*.txt-mtime+20k#单位是Mfind/-name*.txt-mtime+20M5)Searchbyauthority,系统权限有10位,第一位代表是目录还是文件(d表示目录,-表示文件),第2到第4位是当前用户的权限,第5到第7位是当前用户的权限用户组,第8到10位为当前用户以外的用户权限,分别用rwx表示读、写、执行的权限,r编码为数字4,w编码为数字2,x编码为1,例如:drwxrw-rw-,表示766权限的目录#在根目录下搜索权限755的txt文件find/-name*.txt-perm7556)在根目录下查找所有文件修改权限,-exec可以将find找到的内容处理为后面{}中的参数。find/-typef-execchmod-R755{}\;5.grepcommand#查找文件中的内容grep'root'/etc/passwd#添加颜色,--colorgrep--color'root'/etc/passwd#添加行号,-ngrep-n--color'root'/etc/passwd#以什么开始,^grep-n--color'^root'/etc/passwd#以什么结束,$grep-n--color'root$'/etc/passwd#Reverse,-vgrep-vgreptest.txt#去掉空行,grep-v"^$"test.txt#正则匹配1)匹配一个数字grep"[0-9]"test.txt2)匹配两个连续的数字grep"[0-9][0-9]"test.txt3)匹配多次,比如匹配一个出现1到3次后跟小数点的数字egrep"[0-9]{1,3}"test。txt#[0-9]表示匹配一个数字,{1,3}表示匹配1到3次,反斜杠表示翻译,方括号表示匹配范围,[^1-9]中的尖符号表示不匹配1-9开头egrep"([0-9]{1,3}\.){3}"test.txt4)统计,-c##统计以字符"test"开头的行数grep-c'test'test.txt6.awk,主要用来统计1)打印列,#打印第一列awk"{print$1}"/etc/passwd#打印最后一列,用-F指定分隔符awk-F:"{print$NF}"/etc/passwd7.sed-n:Onlyprintthelinesthatmatchthepattern#Printonlythesecondlinesed-n'2p'test.txt#默认打印所有行,匹配完再打印指定行,所以匹配的行会打印两次sed'2p'test.txt#打印指定行sed-n'1,3p'test.txt#打印匹配字符的行sed-n'/test/p'test.txt#正则匹配sed'5q'/etc/passwd#打印前5行sed-n'/r*t/p'/etc/passwd#打印匹配r有0行或多行后跟一个t字符sed-n'/.r.*/p'/etc/passwd#Printlinesthatmatchrandrfollowedbyanycharactersed-n'/o*/p'/etc/passwd#Printocharacterrepeatedanynumberoftimessed-n'/o\{1,\}/p'/etc/passwd#printo单词重复多次sed-n'/o\{1,3\}/p'/etc/passwd#打印o单词重复一到三遍在上面#之间加一行打印,默认情况下,sed使用-e,不会修改文件内容,只是把修改后的内容打印出来sed'1abest'test.txt#字符替换sed's/为replacedstring/newstring/g'test.txt替换每一行匹配的第一个字符串sed-i'1s/originalstring/newstring/'test.txt全局匹配替换所有字符串sed-i's/originalstring/newstring/g'test.txt删除所有匹配字符串的行sed-i'/matchingstring/d'test.txtspecificstring在行后插入一个新行sed-i'/specificstring/anewlinestring'test.txt在特定字符串行之前插入一个新行sed-i'/specificstring/inewlinestring'test.txt用目标字符串替换匹配行中的一个字符串sed-i'/matchingstring/s/sourcestring/targetstring/g'test.txt在文件ab.txt的最后一行之后,添加byesed-i'$abye'test.txt对于文件的第三行,替换所有匹配的字符串sed-i'3s/originalstring/newstring/g'test.txt#同时执行两个替换规则,用引号包含所有规则,用分号分隔不同的规则sed's/^/addedhead&/g;s/$/&addedtail/g'test.txt#删除指定行,只是Printing,不会真正删除,加-i会真正删除sed'2d'test.txt8.cut使用方法1)指定separatorofthefieldwith-d#指定逗号作为分隔符,取第二个字段和第3到第5个字段截取-d","-f2,3-5test.txt2)截取#-b按字节截取#-c2个字符为一个汉字,截取#-c为一个汉字字符截取[root@host130~]#cut-b1-4test.txttestme[root@host130~]#cut-c1-2test.txtteme9.软硬连接1)创建软连接#ln-stest.txttest1.txt[root@host130~]#ls-lrttest*-rw-r--r--1rootroot66May2414:32test.txtlrwxrwxrwx1root根8May2414:43test1.txt->test.txt2)创建硬连接#lntest.txttest2.txt[root@host130~]#ls-lrttest*-rw-r--r--2rootroot66May2414:32test.txt-rw-r--r--2rootroot66May2414:32test2.txtlrwxrwxrwx1rootroot8May2414:43test1.txt->test.tx3)修改源文件[root@host130~]#echo"whoami">test.txt[root@host130~]#cattest.txtwhoami[root@host130~]#cattest1.txtwhoami[root@host130~]#cattest2.txtwhoami4)删除源文件[root@host130~]#rmtest.txtrm:删除常规文件'test.txt'?y[root@host130~]#ls-lrttest*-rw-r--r--1rootroot7May2414:44test2.txtlrwxrwxrwx1rootroot8May2414:51test1.txt->测试。txt[root@host130~]#cattest2.txtwhoami[root@host130~]#cattest1.txtcat:test1.txt:没有那个文件或目录