基础命令

文件管理命令

mkdir

- -m:在创建目录的时候设置权限
- -p:递归创建目录
- -v:显示创建目录的过程
mkdir zls{a..Z}
mkdir zls{A..z}

查看内置还是外置命令

type

[root@XX ~]#type wc
wc is /usr/bin/wc            #小老弟这是内置命令
[root@XX ~]#type echo
echo is a shell builtin        # 这是外置命令

enable

enable cmd 启动内部命令
enable -n cmd 禁用内部命令
enable -n 不接内部命令则为查看所有禁用的内部命令
[root@XX ~]#enable -n echo 
[root@XX ~]#enable -n enable -n echo 
[root@XX ~]#enable echo 
[root@XX ~]#enable -n

touch

#1. mkdir zls
#2. touch zls

## 不会报错,虽然zls目录已经存在,但是linux中一切皆文件,修改'3种时间'

[root@zls ~]# touch /zlsxxx/abc
touch: cannot touch ‘/zlsxxx/abc’: No such file or directory

## touch创建文件的时候,必须要在已经存在的目录中
## 如果目录权限不够,touch也会报错


## 当我使用root用户,在已存在的目录中touch文件,永远都不会报错。

tree

-d:只显示目录,不显示文件
-L:显示目录的层级

cp

3个语法:copy
#cp -T 源文件 目标文件(必须起个名字)
#cp -t 目标路径(也可以写名字) 源文件

[root@zls ~]# cp /etc/hosts /opt/
cp: overwrite ‘/opt/hosts’? 
是否要覆盖

注意:cp命令,无法直接拷贝目录,会修改文件的属性

cp 源文件 目标路径(也可以改名)
-a:相当于 -r -p ,既做到了递归拷贝,又保持了文件的属性
-i:如果拷贝过程中,出现重复文件名,则询问是否要覆盖
-r:递归拷贝文件  -R --recursive
-p:在拷贝文件的过程中,保持文件原有的属性
-v:显示拷贝的过程
-t:将源文件和目标,反过来
-f:强制拷贝

mv

语法:move
Usage: mv [OPTION]... SOURCE... DIRECTORY
mv [选项] 源文件 目标目录(改名)

[root@zls ~]# mv zls zls1
1.zls1是否存在,如果存在,并且是个目录,则将zls文件移动到zls1目录中
2.zls1是否存在,如果不存在,则将zls文件改名为zls1
3.zls1是否存在,如果存在,并且是个文件,则会询问是否要覆盖

注意:
1.mv命令,可以直接移动目录,不需要递归,mv不会修改文件的属性
2.mv 后面可以写 n个文件,但是最后一个必须是目录
3.文件不能覆盖目录,但是可以放入目录里面,目录不能覆盖文件,并且也不能放到文件里面

-i:在移动的过程中,如果存在相同的文件名,则询问是否要覆盖
-f:强制移动
-t:将源文件和目标,反过来

rm

语法:remove
Usage: rm [OPTION]... FILE...
rm [选项] 文件名

-d:删除目录(空目录)
-r:递归删除 -R, --recursive
-f:force 强制删除
-i:在删除文件之前,会询问是否真的要删除(一个一个文件问)
-I:也询问,但是只问一次
-v:显示删除的过程

注意:
1.在Linux中,没有回收站,所以rm会永久删除文件
2.删除文件的时候,尽量使用 rm -f
3.如果文件或者目录不存在,也不会报错

rm -f file
rm -fr /dir

rm -f zls*
rm -f zls1*

------
rm -fr /tmp/*  #删除/tmp目录下面的所有内容,但是/tmp目录本身还在
rm -fr /tmp/   #删除/tmp目录下面的所有内容,包括/tmp目录本身也会被删除
------ 区别很大

cat

语法:cat
Usage: cat [OPTION]... [FILE]...
cat [选项] 文件名

-A:相当于-v -E -T
-n:查看文件并显示出文件的行数(空行也编号)
-T:以^I,标注文件中的TAB键
-t:相当于 -v -T
-E:以$符,标注一行的结尾
-e:相当于 -E -v
-b:空行不给编号

[root@zls ~]# cat >> menu <<eof
> 1.苹果
> 2.梨
> 3.香蕉
> eof

tac 把文件反过来看

系统文件查看命令-more


语法:more [选项] 文件
回车:按行往下看
空格:按页往下看
n:向下查找
/:/xxx 搜索文件中含有xxx的内容行
q:退出

系统文件查看命令-less


语法:less [选项] 文件
回车:按行往下看
空格:按页往下看
/:搜索内容,并高亮显示
n:向下查找关键字
N:向上查找关键字
gg:回到第一行
G:跳转到最后一行
冒号:指定翻页行数
#查看文件内容
[root@aaa ~]# less /etc/services

系统文件查看命令-head


语法:head [选项] 文件
作用:查看一个文件的前N行,默认是前十行。

#显示/etc/services文件的前十行内容
[root@localhost ~]# head /etc/services
-n:指定显示文件多少行内容(n可以省略)
-10
-11

不指定行数,默认情况下,只显示10行,
`head /etc/service` = `head -n 10 /etc/service` = `head -10 /etc/service`
#查看文件头三行
[root@aaa ~]# head -3 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
#查看文件前10行
[root@aaa ~]# head -3 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
[root@aaa ~]# head  /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports

系统文件查看命令-tail


语法:tail [选项] 文件
作用:查看一个文件的后N行,默认是后十行

#显示/etc/services文件的后十行内容
[root@localhost ~]# tail /etc/services
-n:指定显示文件后数多少航内容(n可以省略)

#follow,追踪文件末尾
[root@localhost ~]# tail -f /var/log/messages
-f:追踪文件的末尾,实时查看日志的新内容
-c:显示后数N个字符,(空格也算字符)
-F: -f --retry

tailf  =  tail -f

不指定行数,默认情况先,只显示10行
`tail /etc/services` = `tail -n 10 /etc/services` = `tail -10 /etc/services`
#查看文件后10行
[root@aaa ~]# tail /etc/services
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker
#查看文件后3行
[root@aaa ~]# tail -3 /etc/services
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker
#实时查看文件信息
[root@aaa ~]# tail -f /var/log/messages 
Mar 23 14:01:01 aaa systemd: Created slice User Slice of root.
Mar 23 14:01:01 aaa systemd: Started Session 5 of user root.
Mar 23 14:01:01 aaa systemd: Removed slice User Slice of root.
Mar 23 14:40:29 aaa systemd: Created slice User Slice of root.
Mar 23 14:40:29 aaa systemd: Started Session 6 of user root.
Mar 23 14:40:29 aaa systemd-logind: New session 6 of user root.
#显示后数5个字符
[root@aaa ~]# tail -c 5 /etc/services
oker
#实时查看文件内容等于tail -f
[root@aaa ~]# tailf /etc/services
3gpp-cbsp       48049/tcp               # 3GPP Cell Broadcast Service Protocol
isnetserv       48128/tcp               # Image Systems Network Services
isnetserv       48128/udp               # Image Systems Network Services
blp5            48129/tcp               # Bloomberg locator
blp5            48129/udp               # Bloomberg locator
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker

文件上传下载命令


[root@localhost ~]# yum install -y lrzsz

rz:上传
[root@localhost tmp]# rz

sz:下载 (把虚拟机里面的文件,下载到我们的物理机上windows)
[root@localhost tmp]# sz a.txt

# wget
-O : 指定我要下载的位置,文件名

文件查找命令


# 查找文件或目录locate
yum install -y mlocate
updatedb

[root@localhost opt]# locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells

# 查找命令的命令
[root@localhost opt]# which netstat

$():执行命令的结果

``:执行命令的结果

# 查找命令 
[root@localhost ~]# type -a  ls
ls 是 `ls --color=auto' 的别名
ls 是 /usr/bin/ls

# 查找命令
[root@localhost ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz

例:

$()调用内容
[root@oldboy mlocate]# ping $(hostname -I)
PING 10.0.0.200 (10.0.0.200) 56(84) bytes of data.
64 bytes from 10.0.0.200: icmp_seq=1 ttl=64 time=0.062 ms
64 bytes from 10.0.0.200: icmp_seq=2 ttl=64 time=0.058 ms

查看命令位置
[root@oldboy mlocate]# type -a netstat
netstat is /bin/netstat
netstat is /usr/bin/netstat
[root@oldboy mlocate]# which netstat
/bin/netstat
[root@oldboy mlocate]# whereis netstat
netstat: /usr/bin/netstat /usr/share/man/man8/netstat.8.gz

字符串处理的命令


字符排序命令-sort

用法:sort [选项]... [文件]...
[root@localhost ~]# cat sort.txt
b:3
c:2
a:4
e:5
d:1
f:11

[root@localhost ~]# sort sort.txt
a:4
b:3
c:2
d:1
e:5
f:11

注意:什么都不加,默认按照首字母排序

-t:指定分隔符(默认空格为分隔符)
-k:指定按照哪一列排序
-n:按照阿拉伯数字大小排序(默认是按照数字的首个字符排序)
-r:倒叙
[root@localhost ~]# sort -t ':' -k 2 -n sort.txt
d:1
c:2
b:3
a:4
e:5
f:11

[root@localhost ~]# sort  -nrk 2 -t ':' sort.txt

字符去重命令-uniq

用法:uniq [选项]... [文件]

注意:去重,必须在排好序之后才能去重
-c:count 统计,计数,重复的值有多少

[root@localhost ~]# sort uniq.txt
123
123
456
456
789
abc
abc
abc
abc

[root@localhost ~]# sort uniq.txt|uniq
123
456
789
abc

[root@localhost ~]# sort uniq.txt|uniq -c
      2 123
      2 456
      1 789
      4 abc     

例:

新建一个文本文件,将文件中的内容进行排序,然后去重,注意:必须先排序才支持去重
[root@oldboy ~]# sort aaa.txt|uniq -c
      1 
    246 123
    123 123123
    123 123456
    123 345
    123 567

字符截取命令-cut

用法:cut [选项]... [文件]...
-d:指定分隔符
-f:指定区域
-c:按照字符截取

[root@localhost ~]# cut  -d '.' -f 1-4 ip
[root@localhost ~]# cut -d ' ' -f 2,6 info.txt |cut -d ',' -f 2
18 133411023

例:

以/etc/passwd为例,以冒号为分隔符,取出第三列的内容
[root@oldboy ~]# cut -d ':' -f 3 /etc/passwd
0
1
2
81
999
74
89
1000

字符替换命令-tr

#问题发现最好在替换的时候选择字符长度一致字符串,否则建议使用sed命令
[root@localhost ~]# cat info.txt
I'm zls,18 years old QQ 133411023

[root@localhost ~]# cat info.txt |tr 'QQ' 'qq'
I'm zls,18 years old qq 133411023

统计命令-wc

[root@localhost ~]# wc /etc/services
 11176  61033 670293 /etc/services
 
[root@localhost ~]# wc -l /etc/services
11176 /etc/services

[root@localhost ~]# wc -w /etc/services
61033 /etc/services

[root@localhost ~]# wc -c /etc/services
670293 /etc/services

注意:在wc命令,什么选项都不加的情况下,统计出文件的行数,单词数,和字符数

-l:按照行数统计
-w:按照单词数统计
-c:按照字符数统计
原文地址:https://www.cnblogs.com/tcy1/p/12525927.html