码迷,mamicode.com
首页 > 其他好文 > 详细

第二周

时间:2018-07-27 14:47:48      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:文件的   原来   安全标签   std   mis   opened   结果   lse   第二周   

1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。

目录管理命令:mkdir, rmdir

        注意:路径名的基名方为命令作用的对象。基名前面的路径必须要真实存在,

mkdir命令:    

        NAME

               mkdir - make directories


        SYNOPSIS

               mkdir [OPTION]... DIRECTORY...


        DESCRIPTION

               Create the DIRECTORY(ies), if they do not already exist.


        Mandatory arguments to long options are mandatory for short options too.


               -m, --mode=MODE

                      set file mode (as in chmod), not a=rwx - umask


               -p, --parents

                      no error if existing, make parent directories as needed


               -v, --verbose

                     print a message for each created directory

示例:

~]# mkdir dir1/dir2/dir3
mkdir: cannot create directory ‘dir1/dir2/dir3’: No such file or directory
~]# mkdir -p dir1/dir2/dir3
~]# tree dir1
dir1
└── dir2
    └── dir3
2 directories, 0 files


rmdir命令:

        NAME

               rmdir - remove empty directories


        SYNOPSIS

               rmdir [OPTION]... DIRECTORY...


        DESCRIPTION

               Remove the DIRECTORY(ies), if they are empty.


               --ignore-fail-on-non-empty

                     ignore each failure that is solely because a directory is non-empty


               -p, --parents

                     remove  DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is similar to 'rmdir a/b/c a/b a'


               -v, --verbose

                     output a diagnostic for every directory processed

示例:

~]# rmdir dir1
rmdir: failed to remove ‘dir1’: Directory not empty             #只能删除空目录
~]# rmdir dir1/dir2/
rmdir: failed to remove ‘dir1/dir2/’: Directory not empty
~]# rmdir dir1/dir2/dir3/
~]# rmdir dir1/dir2/
~]# rmdir dir1/


文件查看类命令:cat, tac, head, tail, more, less

分屏查看命令more和less

    more命令

        more FILE

            特点:翻屏至文件尾部后自动退出


    less命令

        less FILE

            操作方法类似于man命令

    

    head命令

    head [OPTION]... [FILE]...

        查看文件前n行内容,默认为前10行

        -n # 或 -n# 或 -# 或 -n+# 或-n +#:显示文件的前number行

        -n -# 或 -n-#:显示文件第一行至文件倒数#+1行的内容

示例:test.txt文件共有21行

~]# head -n2 test.txt
1
2
~]# head -2 test.txt
1
2

    

    tail命令

    tail [OPTION]... [FILE]...

        查看文件的后n行,默认为后10行

        -n # 或 -n# 或 -n -# 或 -n-# 或 -#:显示文件的后#行

        -n +# 或 -n+#:显示文件第#行至文件最后一行的内容

        -f:查看文件尾部内容结束后不退出,跟随显示新增的行

            动态显示文件尾部的内容,直到按ctrl+c退出

示例:test.txt文件共有21行

~]# tail -n2 test.txt 
20
21
[root@learn ~]# tail -2 test.txt  
20
21
~]# tail -n+18 test.txt  
18
19
20
21

    

stat命令

    NAME

        stat - display file or file system status

    SYNOPSIS

        stat [OPTION]... FILE...


    文件:文件存储有两类数据

        元数据:metadata  用来描述属性的数据(文件名、大小、时间戳、索引号等)

          数据:data  文件的内容

示例:

 ~]# stat /etc/passwd
  File: ‘/etc/passwd’
  Size: 798             Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d      Inode: 17116887    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:passwd_file_t:s0
Access: 2018-07-27 12:32:25.704000038 +0800
Modify: 2018-07-24 23:05:04.257314967 +0800
Change: 2018-07-24 23:05:04.259314967 +0800
 Birth: -

    

    touch命令

    NAME

        touch - change file timestamps

    SYNOPSIS

        touch [OPTION]... FILE...

    DESCRIPTION

        Update the access and modification times of each FILE to the current time.

        -a      change only the access time

        -m     change only the modification time

        -c, --no-create

                 do not create any files

        -d, --date=STRING

                 parse STRING and use it instead of current time

                 用STRING替换当前时间

        -t STAMP

               use [[CC]YY]MMDDhhmm[.ss] instead of current time

        -h, --no-dereference

               affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)

        -r, --reference=FILE

               use this file's times instead of current time

示例:

~]# touch -d "2018-7-27 11:30:58" hello.txt
~]# ll hello.txt 
-rw-r--r--. 1 root root 0 Jul 27 11:30 hello.txt


文件管理工具:cp, mv, rm

     cp命令:copy

         源文件 目标文件  

         文件复制原理:假如源文件是一个常规文件,里面有很多数据(我们把它称为流式数据),复制文件就是,首先在目标位置(某一个路径)下先创建一个空文件,随后从原来的文件中抽取数据流(流式数据:可流式化的数据),一行一行(或者一个字节一个字节或者一位一位)的抽出来,最终给它填充到这个目标文件中去,进而目标文件越来越大,越来越大,变得和原来文件内容一样了,所以复制文件指的是复制文件的数据,而不是复制文件的元数据的,只不过,在有些特殊场景下我们可以保留他复制数据时的某些元数据,但复制通常只是针对于数据而言的。

    NAME

        cp - copy files and directories


    SYNOPSIS

        cp [OPTION]... [-T] SOURCE DEST

        cp [OPTION]... SOURCE... DIRECTORY

        cp [OPTION]... -t DIRECTORY SOURCE...

    

    单源复制:cp [OPTION]... [-T] SOURCE DEST

    多源复制:cp [OPTION]... SOURCE... DIRECTORY

                     cp [OPTION]... -t DIRECTORY SOURCE    


    单源复制:cp [OPTION]... [-T] SOURCE DEST

        如果DEST不存在:

            没有给出DEST参数,则会报错(cp: missing destination file operand after ‘SOURCE’)。

            如果给出DEST是不存在的路径(错误的路径),则会报错(cp: cannot create regular file ‘DEST’: No such file or directory)。

        如果DEST存在:

            如果DEST是非目录文件,则覆盖DEST文件。

            如果DEST是目录文件,则在DEST目录下创建一个与SOURCE文件同名的文件,并复制其数据(元数据-如果有必要)至该文件。


    多源复制:cp [OPTION]... SOURCE... DIRECTORY

                     cp [OPTION]... -t DIRECTORY SOURCE

        多源复制,如果没有明确给出DIRECTORY,会把SOURCE参数最后的一个值作为DIRECTORY的值。

        如果DIRECTORY是一个非目录文件或者是一个不存在的路径(错误的路径),会报错(cp: target ‘DIRECTORY’ is not a directory)。

        若果DIRECTORY是一个目录文件,则会分别复制SOURCE参数中的每一个文件至DIRECTORY目录中,并保持原名称,如果DIRECTORY里面存在与SOURCE参数中的值同名的文件,则会被覆盖(-i选项会提示)。

    

    cp命令如果遇到符号链接,默认是复制的符号链接指向的文件的数据,而非符号链接本身。

    cp命令的常用选项:

        -i, --interactive

              prompt before overwrite (overrides a previous -n option)

              交互式复制,即覆盖前提请用户确认。(若果前面有-n参数,则忽略-n参数)


        -n, --no-clobber

              do not overwrite an existing file (overrides a previous -i option)

              不覆盖已经存在的目标文件(如果前面有-i参数,则忽略-i参数)


        -f, --force

              if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)

              强行覆盖目标文件(如果使用-n选项,则-f不起作用)


        -R, -r, --recursive

              copy directories recursively

              递归复制目录及目录的内容至目标目录。


        -P, --no-dereference

              never follow symbolic links in SOURCE

              不跟踪SOURCE中的符号链接


        --preserve[=ATTR_LIST]

              preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all

              保留指定的属性

              参数值             :       保留原来的某种属性

              mode              :       权限

              ownership      :       属主

              timestamps    :       时间戳

              context           :      安全标签

              links                :      链接属性

              xattr               :       扩展属性

              all                   :       以上所有

        -d    same as --no-dereference --preserve=links

               复制链接文件本身,而非其指向的源文件(拷贝时保留其链接属性)

        -a, --archive

              same as -dR --preserve=all


        -L, --dereference

              always follow symbolic links in SOURCE

              跟踪符号链接指向的文件(拷贝符号链接指向的文件内容)


        -p    same as --preserve=mode,ownership,timestamps

              保留源文件权限、属主、时间戳属性


        --backup[=CONTROL]

              make a backup of each existing destination file

              为每一个存在的目标文件先创建备份,再拷贝。

              The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.  The version control method may be selected via the --backup option or through  the  VER‐

              SION_CONTROL environment variable.  Here are the values:

              none, off

                     never make backups (even if --backup is given)

              numbered, t

                     make numbered backups

              existing, nil

                     numbered if numbered backups exist, simple otherwise

              simple, never

                     always make simple backups

        -S, --suffix=SUFFIX

              override the usual backup suffix

              覆盖通常的备份后缀


        -l, --link

              hard link files instead of copying

              用硬链接代替cp(对源文件建立硬链接,而非复制文件)


        -s, --symbolic-link

              make symbolic links instead of copying

              用符号链接替代cp(对源文件建立符号链接,而非复制文件)

              (只能在同一个目录创建相对符号链接)


        -u, --update

              copy only when the SOURCE file is newer than the destination file or when the destination file is missing

        -v, --verbose

              explain what is being done

示例:

~]# cd /tmp
tmp]# cp /var/log ./log
cp: omitting directory ‘/var/log’
tmp]# cp -r /var/log ./log


    mv命令

        mv命令原理:如果是一个文件,首先在目标位置建立一个文件,文件名就是DEST所指定的路径或文件名,然后再把源文件复制过去填充进去,填充完毕,删除源文件。

        如果源有多个,目标只能是目录

        NAME

               mv - move (rename) files

        SYNOPSIS

               mv [OPTION]... [-T] SOURCE DEST

               mv [OPTION]... SOURCE... DIRECTORY

               mv [OPTION]... -t DIRECTORY SOURCE...

        [OPTION]

            -i, --interactive

                   prompt before overwrite

            -f, --force

                   do not prompt before overwriting

示例:

~]# ll test.txt
-rw-r--r--. 1 root root 54 Jul 26 20:59 test.txt
~]# mv test.txt test2.txt
~]# ll test.txt
ls: cannot access test.txt: No such file or directory
~]# ll test2.txt
-rw-r--r--. 1 root root 54 Jul 26 20:59 test2.txt


    rm命令

        NAME

               rm - remove files or directories

        SYNOPSIS

               rm [OPTION]... FILE...

        [OPTION]

               -i     prompt before every removal

               -f, --force

                      ignore nonexistent files and arguments, never prompt

               -I     prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes

                      对于多个文件仅提醒一次


               -r, -R, --recursive

                      remove directories and their contents recursively

                      递归地删除目录及其内容


               -d, --dir

                      remove empty directories

               

               --preserve-root

                      do not remove '/' (default)

               --no-preserve-root

                      do not treat '/' specially


               -v, --verbose

                      explain what is being done

示例:

~]# rm -rI /tmp/log                  #-I仅提醒一次
rm: remove 1 argument recursively? y
~]# ll /tmp/log
ls: cannot access /tmp/log: No such file or directory

2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示。


命令执行的状态结果

            bash通过状态返回值来输出此结果

                    成功:0

                    失败:1-255

            命令执行完成之后,其状态返回值保存于bash的特殊变量$?中,其只保存最近一次命令的状态返回值结果。

            命令正常执行后,有的命令有返回值,其不同于状态返回值,根据命令及其功能不同,结果各不相同。

示例:

 ~]# pwd
/root
~]# echo $?
0
~]# pwdl
-bash: pwdl: command not found
~]# echo $?
127



        引用命令的执行结果

            $(COMMAND)

        或

            `COMMAND`

        例如:

            ~]# mkdir $(date +%H-%m-%S)

            ~]# mkdir `date +%H-%m-%S`

示例:

~]# mkdir $(date +%H-%m-%S)
~]#  ls
22-07-47
~]#  mkdir `date +%H-%m-%S`
~]# [root@learn xxx]# ls
22-07-02  22-07-47



命令行展开

        ~ :自动展开为用户的家目录,或指定用户的家目录。

               ~, ~root

示例:

xxx]# pwd
/root/testdir/xxx
[root@learn xxx]# cd ~
[root@learn ~]# pwd
/root
~]# cd -
/root/testdir/xxx
xxx]# cd ~root
~]# pwd
/root


        {}:大括号扩展(引号内的大括号将失去扩展功能),大括号包围的,用逗号隔开(只有一个元素也需要逗号,会匹配一个元素和一个空元素)的参数会扩展为独立的多个参数。

            花括号用来匹配一组用逗号分隔的字符串中的任一个。左花括号之前的所有字符称为前文(preamble),右花括号之后的所有字符称为后文(preamble)。前文和后文都是可选的。花括号中不能包含不加引号的空白符。


            可承载一个以逗号分隔的路径列表,并能够将其展开为多个路径

            /tmp/{a,b} 相当于 /tmp/a /tmp/b

示例:

~]# echo ab{1,2,3}c
ab1c ab2c ab3c
~]# echo "ab{1,2,3}c"
ab{1,2,3}c
~]# echo \"ab{1,2,3}c\"
"ab1c" "ab2c" "ab3c"
~]# echo {a,b}{1}
a{1} b{1}
~]# echo {a,b}{1,}
a1 a b1 b


3、请使用命令行展开功能来完成以下练习:


   (1)、创建/tmp目录下的:a_c, a_d, b_c, b_d

示例:

~]# cd /tmp
tmp]# touch {a,b}_{c,d}
tmp]# ls
a_c  a_d  b_c  b_d


   (2)、创建/tmp/mylinux目录下的:

mylinux/
    ├── bin
    ├── boot
    │   └── grub
    ├── dev
    ├── etc
    │   ├── rc.d
    │   │   └── init.d
    │   └── sysconfig
    │       └── network-scripts
    ├── lib
    │   └── modules
    ├── lib64
    ├── proc
    ├── sbin
    ├── sys
    ├── tmp
    ├── usr
    │   └── local
    │       ├── bin
    │       └── sbin
    └── var
        ├── lock
        ├── log
        └── run


~]# mkdir -p /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
~]# tree /tmp/mylinux/  
/tmp/mylinux/
├── bin
├── boot
│   └── grub
├── dev
├── etc
│   ├── rc.d
│   │   └── init.d
│   └── sysconfig
│       └── network-scripts
├── lib
│   └── modules
├── lib64
├── proc
├── sbin
├── sys
├── tmp
├── usr
│   └── local
│       ├── bin
│       └── sbin
└── var
    ├── lock
    ├── log
    └── run
24 directories, 0 files


第二周

标签:文件的   原来   安全标签   std   mis   opened   结果   lse   第二周   

原文地址:http://blog.51cto.com/dongqi/2150998

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!