基础命令

1 文本管理命令    1

1.1 cp命令(copy    1

2 查看文件命令    2

2.1 cat    2

2.2 more    3

2.3 less    3

2.4 head    3

2.5 tail    3

3 文件下载命令    4

3.1 wget(可以下载源代码说明网站正常运行)    4

3.2 curl(可以下载源代码说明网站正常运行)    5

4 查找命令    6

4.1 locate    6

4.2 where    6

4.3 whereis    6

4.4 sort    7

 

  1. 文本管理命令

  2. cp命令(copy)

i:交互式复制,覆盖之前,提醒用户确认;

        -f:强制覆盖目标文件;

        -r(-R):递归复制目录;

        -d:复制符号链接文件本身,而非其指向的源文件;

        -a:-dR --preserve=all,archive,用于实现归档;

        --preserv=

                mode:权限

                ownership:属主和属组

                timestamps:时间戳

                context:安全标签

                xattr:扩展属性

                links:符号链接

                all:上述所有属性

    1、单源文件复制:cp [OPTION]... [-T] SOURCE DEST

  1. 如果目标不存在,则事先创先此文件,并复制源文件的内容至目标文件中;
  2. 如果目标存在,并且是非目录文件,则覆盖目标文件;
  3. 如果目标存在,并且是目录文件,则先在目标目录下创建一个与源文件同名的文件,并复制源文件的内容。
  1. 多源文件复制
    1. 如果目标不存在,错误;
    2. 如果目标存在,并且是非目录,错误;
    3. 如果目标存在,并且是目录,分别复制每个文件至目标目录中,并保持原名。
  2. 目录复制:
    1. 如果目录不存在,则先创建目录,并按照文件复制的方式,将目录内的文件复制到目标目录中
    2. 如果目标存在,并且是文件,错误
    3. 如果目标存在,并且是目录,将整个目录都复制到目标目录中
    4. 例如:将一个目录a中的所有内容,复制到另一个目录b

[root@oldboy ~]# cp -r a b

[root@oldboy ~]# ll b

总用量 0

drwxr-xr-x 2 root root 57 8月 7 17:10 a

-rw-r--r-- 1 root root 0 8月 7 17:09 song1.mp3

-rw-r--r-- 1 root root 0 8月 7 17:09 song2.mp3

-rw-r--r-- 1 root root 0 8月 7 17:09 song3.mp3

 

[root@oldboy ~]# cp -r a/* b

[root@oldboy ~]# ll b/

总用量 0

-rw-r--r-- 1 root root 0 8月 7 17:09 song1.mp3

-rw-r--r-- 1 root root 0 8月 7 17:09 song2.mp3

-rw-r--r-- 1 root root 0 8月 7 17:09 song3.mp3

-rw-r--r-- 1 root root 0 8月 7 17:12 text1.txt

-rw-r--r-- 1 root root 0 8月 7 17:12 text2.txt

-rw-r--r-- 1 root root 0 8月 7 17:12 text3.txt

  1. 查看文件命令

  2. cat

1、cat:将文件内容打印到显示器上

[root@oldboy ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

2、cat:将编辑的文本保存到指定的文件

[root@oldboy ~]# cat >text.txt<<EOF

> hello!

> my name is majinhai

> nice to meet you!

> EOF

[root@oldboy ~]# cat text.txt

hello!

my name is majinhai

nice to meet you!

  1. more

    1. more分页浏览
      1. 回车一次,滚动一行
      2. 空格翻页
      3. 不能往上翻页,只能往下翻

[root@oldboy ~]# more text.txt

hello!

my name is majinhai

nice to meet you!

  1. less

    1. less分页浏览
    2. 可以反复查看
    3. 回车滚动一行,上下键,pguppgdn、空格翻页
    4. 屏幕右下角显示阅读到百分比内容
    5. q键退出

hello!

my name is majinhai

nice to meet you!

text.txt (END)

  1. head

    -n:表示查看前n行

    1. head从文件头部开始浏览
    2. 默认显示前10

[root@oldboy ~]# head -5 /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

  1. tail

    -n:表示查看后行

    -f:跟踪

            ctrl+c:结束

    1. 从文件尾部开始看,默认10行,适合查看文件的更新信息(/etc/passwd,日志文件)

[root@oldboy ~]# tail -5 /etc/passwd

polkitd:x:999:998:User for polkitd:/:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

majinhai:x:1000:1000::/home/majinhai:/bin/bash

user1:x:1001:1001::/home/user1:/bin/bash

[root@oldboy ~]#

  1. 文件下载命令

  2. wget(可以下载源代码说明网站正常运行)

    1、安装wget

[root@oldboy ~]# yum install wget

2、下载www.baidu.com首页源码

[root@oldboy ~]# wget https://www.baidu.com

--2018-08-07 17:48:04-- https://www.baidu.com/

正在解析主机 www.baidu.com (www.baidu.com)... 111.13.100.92, 111.13.100.91

正在连接 www.baidu.com (www.baidu.com)|111.13.100.92|:443... 已连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:2443 (2.4K) [text/html]

正在保存至: "index.html"

 

100%[===================================================>] 2,443 --.-K/s 用时 0s

 

2018-08-07 17:48:04 (131 MB/s) - 已保存 "index.html" [2443/2443])

 

-O(这里是大写):指定下载地址,更改名称

[root@oldboy ~]# wget -O /root/intex1.html https://www.baidu.com

--2018-08-07 17:55:49-- https://www.baidu.com/

正在解析主机 www.baidu.com (www.baidu.com)... 111.13.100.92, 111.13.100.91

正在连接 www.baidu.com (www.baidu.com)|111.13.100.92|:443... 已连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:2443 (2.4K) [text/html]

正在保存至: "/root/intex1.html"

 

100%[===================================================>] 2,443 --.-K/s 用时 0s

 

2018-08-07 17:55:49 (703 MB/s) - 已保存 "/root/intex1.html" [2443/2443])

--spider:网络爬虫(不会下载到到本地,会显示成功)

[root@oldboy ~]# wget --spider www.baidu.com

开启 Spider 模式。检查是否存在远程文件。

--2018-08-07 19:22:13-- http://www.baidu.com/

正在解析主机 www.baidu.com (www.baidu.com)... 111.13.100.91, 111.13.100.92

正在连接 www.baidu.com (www.baidu.com)|111.13.100.91|:80... 已连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:277 [text/html]

存在远程文件且该文件可能含有更深层的链接,

但不能进行递归操作 -- 无法获取。

-T:超时时间

-t:请求次数

-q:静默下载(不输出下载内容)

[root@oldboy ~]# wget -q https://www.baidu.com

-rw-r--r-- 1 root root 2443 1月 23 2017 index.html

 

  1. curl(可以下载源代码说明网站正常运行)

    -o(这里是小写):指定下载路径和名称

[root@oldboy ~]# curl -o /root/123.html https://www.baidu.com

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 2443 100 2443 0 0 28353 0 --:--:-- --:--:-- --:--:-- 28406

[root@oldboy ~]# ll

-rw-r--r-- 1 root root 2443 8月 7 18:07 123.html

  1. 查找命令

  2. locate

-i:忽略大小写

  1. 安装locate

[root@oldboy ~]# yum install mlocate

跟新数据文件

[root@oldboy ~]# updatedb

2、查找文件或者目录,查看/root/下以t开头的文件

[root@oldboy ~]# locate /root/t

/root/test.txt

/root/text.txt

  1. which

    查找系统PATH变量目录下的命令(绝对路径)

    1、示例:which ls

[root@oldboy ~]# which ls

alias ls='ls --color=auto'

    /usr/bin/ls

2、通过rpm -qf /usr/bin/ls 可以查到命令的安装包(在命令损坏时,重新安装)

[root@oldboy ~]# rpm -qf /usr/bin/ls

coreutils-8.22-21.el7.x86_64

  1. whereis

查找文件索引数据库下的命令、源文件、man文件。非PATH变量查找,所以查找的面比which要广

    示例:whereis ls

[root@oldboy ~]# whereis ls

ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

    -b:只显示路径

whereis -b ls

[root@oldboy ~]# whereis -b ls

ls: /usr/bin/ls

 

  1. sort

    排序

[root@oldboy ~]# sort number.txt

192.168.0.151 00:0F:AF:85:6C:F6

192.168.0.152 00:0F:AF:83:1F:65

192.168.2.20 00:0F:AF:85:55:DE

192.168.2.21 00:0F:AF:85:6C:09

192.168.2.22 00:0F:AF:85:5C:41

192.168.3.1 00:0F:AF:81:19:1F

192.168.3.2 00:0F:AF:85:6C:25

192.168.3.3 00:0F:AF:85:70:42

-r:倒序

[root@oldboy ~]# sort -r number.txt

192.168.3.3 00:0F:AF:85:70:42

192.168.3.2 00:0F:AF:85:6C:25

192.168.3.1 00:0F:AF:81:19:1F

192.168.2.22 00:0F:AF:85:5C:41

192.168.2.21 00:0F:AF:85:6C:09

192.168.2.20 00:0F:AF:85:55:DE

192.168.0.152 00:0F:AF:83:1F:65

192.168.0.151 00:0F:AF:85:6C:F6

-n:按数字排序

-t:指定分隔符

-k:指定第几列,指定第几列几个字符(1.1 3.1,3.3)

[root@oldboy ~]# sort -t. -k3.1,3.1n -k4.1,4.3n number.txt

192.168.0.151 00:0F:AF:85:6C:F6

192.168.0.152 00:0F:AF:83:1F:65

192.168.2.20 00:0F:AF:85:55:DE

192.168.2.21 00:0F:AF:85:6C:09

192.168.2.22 00:0F:AF:85:5C:41

192.168.3.1 00:0F:AF:81:19:1F

192.168.3.2 00:0F:AF:85:6C:25

192.168.3.3 00:0F:AF:85:70:42

  1. uniq

    -c:计算重复的行

    示例:过去敲过的命令最多的前10个,列出来

[root@oldboy ~]# history | awk '{print $2}' | sort | uniq -c | sort -n | tail -10

19 yum

21 touch

22 history

22 wget

23 systemctl

25 ls

31 cd

35 rm

38 cat

121 ll

 

  1. cut

截取某一段字符

    -d:指定分隔符

    -f:数字,指定截取第几列 例如-f3,6 截取3列和6列

    -c:安字符截取,空格也算

原文地址:https://www.cnblogs.com/majinhai/p/9534056.html