zoukankan      html  css  js  c++  java
  • Linux常用命令练习

    linux基础的一些练习题 转自:https://www.cnblogs.com/clsn/p/7497412.html

    1.1 第1题

    创建一个目录 /data

    1
    [root@clsn ~]# mkdir /data

    查看

    1
    2
    3
    [root@clsn ~]# #查看目录里面的内容
    [root@clsn ~]# ls /data/
    [root@clsn ~]# ls -l /data/

    1.2 第2题

    在/data 下面创建 clsn.txt

    1
    2
    3
    4
    [root@clsn data]# touch /data/clsn.txt
    [root@clsn data]# ls -l /data/
    total 0
    -rw-r--r--. 1 root root 0 Aug 15 20:20 clsn.txt

    1.3 第3题

    为clsn.txt增加内容"I am studying linux."

    1.3.1 方法一 vim

    1
    vim /data/clsn.txt

    使用方法:

     1 1.编辑模式 编辑这个文件
     2 
     3 i
     4 
     5 写入你的内容
     6 
     7 2.退出编辑模式
     8 
     9 esc
    10 
    11 3.保存并退出
    12 
    13 :wq 

    1.3.2 查看文件内容

    1
    2
    [root@clsn ~]# cat /data/clsn.txt
    I am studying linux.

    1.3.3 方法二 echo

    1
    [root@clsn ~]# echo clsnedu >>/data/clsn.txt

    查看

    1
    2
    3
    [root@clsn ~]# cat /data/clsn.txt
    I am studying linux.
    clsnedu

    >>  追加,把内容文字放到文件的末尾

    1.3.4 方法三 cat

    追加多行文件

    1
    2
    3
    4
    5
    6
    cat >>/data/clsn.txt<<EOF
    I
    am
    clsn
    student
    EOF

    1.4 第4题

    把clsn.txt文件复制到/tmp下。

    1
    2
    3
    4
    5
    [root@clsn ~]# cp /data/clsn.txt  /tmp/
    [root@clsn ~]# ls -l /tmp/
    total 4
    -rw-r--r--. 1 root root 66 Aug 11 21:07 clsn.txt
    -rw-------. 1 root root  0 Aug 10 18:29 yum.log

    1.4.1 复制目录

    1
    2
    3
    4
    5
    6
    7
    [root@clsn ~]# cp -r /data/ /tmp/
     
    [root@clsn ~]# ls -l /tmp/
    total 8
    drwxr-xr-x. 2 root root 4096 Aug 11 21:31 data
    -rw-r--r--. 1 root root   66 Aug 11 21:07 clsn.txt
    -rw-------. 1 root root    0 Aug 10 18:29 yum.log

    -r 递归参数,复制目录及其下的所有文件

    1.4.2 备份文件

    1
    2
    3
    4
    5
    6
    7
    8
    [root@clsn ~]# cp /data/clsn.txt /data/clsn.txt.bak
     
    [root@clsn ~]# ls -l /data/
    total 16
    -rw-r--r--. 1 root root 14 Aug 11 20:55 doc-tan.txt
    -rw-r--r--. 1 root root 15 Aug 11 20:52 lidao.txt
    -rw-r--r--. 1 root root 66 Aug 11 20:38 clsn.txt
    -rw-r--r--. 1 root root 66 Aug 11 21:52 clsn.txt.bak

    1.5 第5题

    把 /data 移动到 /root目录下面

    1
    [root@clsn ~]# mv /data/ /root/

    检查

    1
    2
    3
    4
    5
    6
    7
    [root@clsn ~]# ls -l /root/
    total 48
    -rw-------. 1 root root  1073 Aug 10 18:40 anaconda-ks.cfg
    drwxr-x---. 2 root root  4096 Aug 10 18:50 anaconda-screenshots
    drwxr-xr-x. 2 root root  4096 Aug 11 21:52 data
    -rw-r--r--. 1 root root 21736 Aug 10 18:40 install.log
    -rw-r--r--. 1 root root  5890 Aug 10 18:38 install.log.syslog

    -a  == -pdr

    -p  == 属性不变

    -r  == 递归

    1.6 第6题

    进入/root目录下,删除clsn.txt文件。

    首先进入到目录中

    1
    2
    3
    4
    [root@clsn ~]# cd /root/
    [root@clsn ~]# ls
    anaconda-ks.cfg  install.log         clsn.txt
    find             install.log.syslog  test.txt

    使用rm命令删除,提示是否删除,输入y回车可以删除

    1
    2
    [root@clsn ~]# rm clsn.txt
    rm: remove regular file `clsn.txt'?

          不提示:

    1
    2
    [root@clsn ~]# m clsn.txt
    [root@clsn ~]# /bin/rm clsn.txt

    1.7 第7题

    接第6题,退回上一级目录,删除data目录。

    1
    2
    3
    4
    [root@clsn data]# cd  ..
     
    [root@clsn ~]# pwd
    /root

    使用rm进行删除

                -f强制删除、-r递归

    [root@clsn ~]# rm -fr data/

    检查当前目录

    [root@clsn ~]# ls -l

    total 48
    -rw-------. 1 root root  1073 Aug 10 18:39 anaconda-ks.cfg
    -rw-r--r--. 1 root root     0 Aug 16 16:28 find
    -rw-r--r--. 1 root root 21736 Aug 10 18:39 install.log
    -rw-r--r--. 1 root root  5890 Aug 10 18:37 install.log.syslog

    1.1 第8题

    输出test.txt文件内容时,不包含clsn字符串的命令

    文件内容:

    [root@znix ~]# cat /data/test.txt
    test
    liyao
    clsn

    1.1.1 方法一grep

    使用grep命令,找什么就写什么

    -v 参数是排除的意思

    [root@znix ~]# grep -v "clsn" /data/test.txt
    test
    liyao

    1.1.2 方法二head

    使用head显示文件前2行信息(-2参数是显示两行),默认显示前10行。

    [root@znix ~]# head -2 /data/test.txt
    test
    liyao

    1.1.3 方法三 sed

    sed命令使用单引号

    sed命令主要用于删除

    '/clsn/d' 表示删除clsn这个内容

    [root@znix ~]# sed '/clsn/d' /data/test.txt
    test
    liyao

    '3d'  表示删除第三行

    [root@znix ~]# sed  '3d'  /data/test.txt
    test
    liyao

    1.1.4 方法四 awk

    使用awk查找文本里的clsn/并显示:

    [root@znix ~]# awk '/clsn/' /data/test.txt
    clsn

    前面加!表示排除这个内容,不显示这个内容。

    [root@znix ~]# awk '!/clsn/' /data/test.txt
    test
    liyao

    1.2 第9题

    用一条命令完成创建目录/clsn/test,即创建/clsn目录及/clsn/test

    创建多级目录时需要添加参数 -p 表示创建多级目录。

    [root@znix ~]# mkdir -p /clsn/test
    [root@znix ~]# ls /clsn/
    alex.txt  alex.txt.bak  clsn.txt  test  test.sh  t.sh
    [root@znix ~]# ls -l  /clsn/test
    total 0

    1.3 第10题

    已知/tmp下已经存在test.txt文件,如何执行命令才能把/mnt/test.txt拷贝到/tmp下覆盖掉/tmp/test.txt,而让系统不提示是否覆盖(root权限下)。

    1.3.1 方法一:

          cp 取消别名

    [root@znix data]# cp /mnt/test.txt /tmp/

    1.3.2 方法二:

    使用命令的绝对路径

     [root@znix data]# /bin/cp /mnt/test.txt /tmp/

    1.4 第11题

    查看ett.txt文件(共100行)内第20到第30行的内容

    1.4.1 创建该文件

    使用seq命令创建一个100行的文件。

    [root@znix ~]# seq 5
    1
    2
    3
    4
    5
    [root@znix ~]#seq 100 >ett.txt

    1.4.2 方法一

    使用head找出前30行,通过管道传给tial,再使用tail找出后11行,然后输出。

    [root@znix ~]# head -30 ett.txt |tail -11
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    1.4.3 方法二

    使用sed查找20-30行,p表示行数,-n表示取消默认输出。

    [root@znix ~]# sed -n '20,30p'  ett.txt
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    1.4.4 方法三

          NR表示行,逗号表示20到30,两个等于号才是真正的等于 。

    [root@znix ~]# awk 'NR==20,NR==30' ett.txt

          &&表示和, => 20大于等于20, <=30 小于等于30

    [root@znix ~]# awk 'NR>=20 && NR<=30' ett.txt
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    1.4.5 方法四

    由于这个文件的第20行正好是20。使用grep找到20并输出,-A10 表示输出后面的10行。

    [root@znix ~]# grep -A10 "20"  ett.txt
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    1.5 第13题

    把/clsn目录及其子目录下所有以扩展名.sh结尾的文件中,文件含.clsn的字符串全部替换为znix。

    1.5.1 创建环境

    mkdir -p /clsn/test
    cd /clsn
    echo "clsn">test/del.sh
    echo "clsn">test.sh
    echo "clsn">t.sh
    touch clsn.txt
    touch alex.txt

    1.5.2 方法一

    sed生效需要增加 -i 参数。

    使用find找在/clsn 目录下的 文件,名字是.sh 结尾的,使用管道xgras  传给sed,然后使用's#要被替换的内容#替换的内容#g' 对 文件进行修改。

    [root@znix clsn]# find /clsn/ -type f -name "*.sh"|xargs sed 's#clsn#znix#g'  -i
    znix
    znix
    znix

    1.5.3 方法二

    $( ) 先执行括号里面命令,再执行前面的命令

    [root@znix clsn]# sed 's#clsn#znix#g' $(find /clsn/ -type f -name "*.sh")
    znix
    znix
    znix
  • 相关阅读:
    [Ramda] Getter and Setter in Ramda & lens
    [Angular2 Router] Index router
    [TypeScript] Using Interfaces to Describe Types in TypeScript
    [RxJS] ReplaySubject with buffer
    [Ramda] Declaratively Map Predicates to Object Properties Using Ramda where
    [Ramda] Pluck & Props: Get the prop(s) from object array
    [Ramda] Complement: Logic opposite function
    [TypeScript] Distinguishing between types of Strings in TypeScript
    [NodeJS] Use Now alias for custom sub-domains
    [CSS3] Create a fixed-fluid-fixed layout using CSS calc()
  • 原文地址:https://www.cnblogs.com/Roc-Atlantis/p/9807062.html
Copyright © 2011-2022 走看看