天天看点

6.Linux目录和文件管理类命令

bash特性之命令别名和命令引用:

命令别名:命令的另外一个名字

windows中清屏使用 cls

Linux下的清屏命令为clear

    alias:用来定义命令别名的

    alias 不跟选项和参数时,显示系统上所有的命令别名

    alias ALIAS=COMMAND

NAME

       alias - define or display aliases

SYNOPSIS

       alias [alias-name[=string] ...]    

[root@linux_basic tmp]# alias

alias cp='cp -i'

alias l.='ls -d .* --color=auto'

alias ll='ls -l --color=auto'

alias ls='ls --color=auto'

alias mv='mv -i'

alias rm='rm -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

定义Linux的清屏命令为cls

[root@linux_basic tmp]# alias 'cls=clear'

alias cls='clear'

此时在Linux下使用cls就相当于清屏了,此设置只能在当前终端中生效,切换或退出再登录时此别名是不会生效的,要生效则需要修改配置文件。

    别名与命令名同名时:

   执行命令本身可以使用:

         绝对路径

         \COMMAND

[root@linux_basic tmp]# \ls 和/bin/ls显示结果一样

hello  mylinux  test.txt  you

[root@linux_basic tmp]# /bin/ls

        生效范围:命令行定义的别名,其生效范围为当前会话;

在会话的设置会当前生效,在配置文件中的设置是永久有效的,但很难立即生效,需要重读配置文件

    unalias [ALIAS]

        -a: 撤消所有别名

       unalias - remove alias definitions  删除别名定义

       unalias alias-name...   删除制定别名的定义

       unalias -a

DESCRIPTION

       The unalias utility shall remove the definition for each alias name specified. See Alias Substitution . The aliases shall  be

       removed from the current shell execution environment; see Shell Execution Environment .

[root@linux_basic ~]# alias

[root@linux_basic ~]# unalias cls

[root@linux_basic ~]# cls

-bash: cls: command not found

    bash支持的引用:

        ''

        ""

        ``:引用一个命令的执行结果   反引号    

         $()  和 ``是一样的功能,建议使用$()

如何来新建一个file-hh-mm-ss.txt文件呢?

引用命令的执行结果

[root@linux_basic ~]# date +%T

17:47:03

[root@linux_basic ~]# date +%H-%M-%S

17-47-26

[root@linux_basic ~]# touch file-`date +%H-%M-%S`.txt

[root@linux_basic ~]# ls file-17-48-31.txt

file-17-48-31.txt

bash特性之文件名通配(globbing):

    *: 任意长度的任意字符

        p*d, pad, pbd, pd

        *ab*c

显示/etc/下所以以a开头的文件        

[root@linux_basic ~]# ls -d /etc/a*

/etc/abrt  /etc/adjtime  /etc/aliases.db  /etc/alternatives  /etc/asound.conf  /etc/audisp

/etc/acpi  /etc/aliases  /etc/alsa        /etc/anacrontab    /etc/at.deny      /etc/audit

    ?: 匹配任意单字符

[root@linux_basic mylinux]# ls

bin  boot  dev  etc  lib  lib64  proc  sbin  sys  tmp  usr  var

[root@linux_basic mylinux]# ls *s*

sbin:

sys:

usr:

bin  lib  lib64  local  sbin

[root@linux_basic mylinux]# ls *s* -d

sbin  sys  usr

[root@linux_basic mylinux]# ls s?

ls: cannot access s?: No such file or directory

[root@linux_basic mylinux]# ls s??

[root@linux_basic mylinux]# ls s???

[root@linux_basic mylinux]# ls s??? -d

sbin

[root@linux_basic mylinux]# ls s?? -d

sys    

        []: 匹配指定范围内的任意单字符

        [abc] 匹配含有一个a或b或c的字串

        [a-z] 匹配含有一个字母的字串 和[A-Z]是一样的

[root@linux_basic tmp]# ls

[root@linux_basic tmp]# touch yoU

[root@linux_basic tmp]# ls yo[a-z]

yoU

you:

are        

[root@linux_basic tmp]# ls -d yo[a-z]

yoA  yoH  you  yoU

[root@linux_basic tmp]# ls -d yo[A-Z]

        [0-9] 匹配含有单个数字

        [0-9a-z] 匹配含有数字和字符的

        [^]:匹配指定范围以外的任意单字符

        [^0-9a-z]

        字符集合:

            [:space:] : 所有空白字符  匹配单个空白字符[[:spsce:]]

            [:punct:] : 所有标点符号

            [:lower:] :所有小写字母

            [:upper:] :所有的大写字母

            [:digit:] :所有的数字

            [:alnum:] : 所有的字母和数字

            [:alpha:] :所有的字母

    练习:

        1、显示/var目录下所有以l开头,以一个小字母结尾,且中间出现一位数字的文件或目录;

            # ls /var/l*[[:digit:]]*[[:lower:]]

        2、显示/etc目录下,以任意一位数字开头,且以非数字结尾的文件或目录;

            # ls -d /etc/[[:digit:]]*[^[:digit:]]

        3、显示/etc目录下,以非字母开头,后面跟了一个字母及其它任意长度字符的文件或目录;

            # ls -d /etc/[^[:alpha:]][[:alpha:]]*

        1、在/tmp/mytest目录中创建以testdir打头,后跟当前日期和时间的空目录,形如testdir-2014-07-03-09-15-33;

            # mkdir -pv /tmp/mytest/testdir-$(date +%F-%H-%M-%S)

echo命令

       echo - write arguments to standard output

[root@linux_basic tmp]# echo "hello world"

hello world       

       echo [string ...]

       The echo utility writes its arguments to standard output, followed by a <newline>. If there are no arguments, only the  <new-

       line> is written.

    echo [-neE] [arg ...]

-n        do not append a newline

-e        enable interpretation of the following backslash escapes  使能转义字符

-E        explicitly suppress interpretation of backslash escapes

        \n

        \n        new line

        \t

        \t        horizontal tab

[root@linux_basic tmp]# echo "How are you.\nWorld."

How are you.\nWorld.

[root@linux_basic tmp]# echo -e "How are you.\nWorld."

How are you.

World.

[root@linux_basic tmp]# echo -n "How are you.\nWorld."

How are you.\nWorld.[root@linux_basic tmp]#

[root@linux_basic tmp]# echo  -e "How are you.\tWorld."

How are you.    World.

        \033[

            单个数字:控制字体

            3#:#是一个数字,3表示控制其前景色

            4#:#是一个数字,4表示控制其背景色

            组合使用,彼此间使用;分隔

        m:是固定格式

    \033[0m:控制符的功能至此结束,没有提供时,用ls命令会取消显示,

文件管理类命令:

    复制:cp

    移动:mv

    删除:rm

       cp - copy files and directories

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

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

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

       Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

    cp:

        cp SRC DEST

            SRC是文件:

                如果DEST不存在:复制SRC为DEST

                如果DEST存在:

                    如果DEST是文件:则提示是否覆盖

                    如果DEST是目录:将SRC复制进DEST中,并保持原名

[root@linux_basic tmp]# mkdir you/test

[root@linux_basic tmp]# touch test

A  hello  mylinux  test  test.txt  yoA  yoH  you  yoU

[root@linux_basic tmp]# cp test you/test

[root@linux_basic tmp]# ls you/test/

test

[root@linux_basic tmp]# cp test you/test/

cp: overwrite `you/test/test'? n

        cp SRC... DEST

            如果SRC不止一个,则DEST必须得是目录且存在;

[root@linux_basic tmp]# cp /etc/inittab /etc/fstab /tmp/test

cp: target `/tmp/test' is not a directory

            SRC是目录:

                复制目录,可使用-r选项:

                -R, -r, --recursive

              copy directories recursively  递归复制目录

        cp -r SRC... DEST

[root@linux_basic tmp]# cp -r /var/log mylog

A  hello  mylinux  mylog  test  test.txt  yoA  yoH  you  yoU

        练习:复制/etc目录下,所有以p开头,以非数字结尾的文件或目录至/tmp/mytest1目录;

            # mkdir /tmp/mytest1

            # cp -r /etc/p*[^[:digit:]]  /tmp/mytest1

        练习:复制/etc/目录下,所有以.d结尾的文件或目录至/tmp/mytest2目录;

            # mkdir /tmp/mytest2

            # cp -r /etc/*.d  /tmp/mytest2

        练习:复制/etc/目录下所有以l或m或n开头,以.conf结尾的文件至/tmp/mytest3目录;

            # mkdir /tmp/mytest3

            # cp -r /etc/[lmn]*.conf /tmp/mytest3

        -P: 复制符号链接文件本身,而非其指向的目标文件  符号链接文件的大小是源文件中字符的个数

         --preserve[=ATTR_LIST]

             mode,ownership,timestamps

                 mode: 权限

                 owership: 属主、属组

                 timestamps: 时间戳

      -R 目录复制使用递归

            -d     same as --no-dereference --preserve=links  保留连接

             -p: 相当于 --preserve(保留)=mode,ownership,timestamps

      preserve[=ATTR_LIST]   默认是保留权限、属主和属组、时间戳,也可以指定保留的属性

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

              links, xattr, all

       保留指定属性来复制的

        -a:相当于 -dR --preserve=all  保留文件的所有属性,常用来归档的

            归档:archive

        -i: interactive

        -i, --interactive  文件存在提示是否覆盖

              prompt before overwrite (overrides a previous -n option)

注意:在普通用户中cp是没有别名的,也即没有alias cp='cp -i'所以使用时要特别注意,不要随意把重要文件覆盖了。

    [root@linux_basic tmp]# alias

    alias cp='cp -i'          

        -f: force   此项需要注意使用,不要把重要文件给强制覆盖了

        -f, --force  文件存在时,强制复制不提示

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

以下命令中,大家需要注意复制过程中各文件的属性信息是否改变及各选项和参数的具体作用,此处不详解,有问题,可以一起来交流

[root@linux_basic tmp]# cd free/

[root@linux_basic free]# ls

[root@linux_basic free]# touch links

[root@linux_basic free]# ln -sv links link

`link' -> `links'

[root@linux_basic free]# ls -l

total 0

lrwxrwxrwx. 1 root root 5 Dec 20 20:31 link -> links

-rw-r--r--. 1 root root 0 Dec 20 20:31 links

[root@linux_basic free]# mkdir too

link  links  too

[root@linux_basic free]# cp link too/

[root@linux_basic free]# ls too/

link

[root@linux_basic free]# ls too/ -l

-rw-r--r--. 1 root root 0 Dec 20 20:33 link

[root@linux_basic free]# cp -p link too/

cp: overwrite `too/link'? y

-rw-r--r--. 1 root root 0 Dec 20 20:31 link

[root@linux_basic free]# cp -P link too/

lrwxrwxrwx. 1 root root 5 Dec 20 20:33 link -> links

[root@linux_basic free]# chmod +w /tmp/free/

[root@linux_basic free]# chmod +w /tmp/free

[root@linux_basic free]# ls -ld /tmp/free

drwxr-xr-x. 3 root root 4096 Dec 20 20:32 /tmp/free

[root@linux_basic free]# chmod a+w /tmp/free

drwxrwxrwx. 3 root root 4096 Dec 20 20:32 /tmp/free

[root@linux_basic free]# chmod -w /tmp/free

chmod: /tmp/free: new permissions are r-xrwxrwx, not r-xr-xr-x

dr-xrwxrwx. 3 root root 4096 Dec 20 20:32 /tmp/free

[root@linux_basic free]# cp other other_to

total 4

lrwxrwxrwx. 1 root      root         5 Dec 20 20:31 link -> links

-rw-r--r--. 1 root      root         0 Dec 20 20:31 links

-rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other

-rw-r--r--. 1 root      root         0 Dec 20 20:39 other_to

drwxr-xr-x. 2 root      root      4096 Dec 20 20:33 too

[root@linux_basic free]# cp -p other other_too

-rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other_too

[root@linux_basic free]# cp --preserve=mode,timestamps other othero

-rw-rw-r--. 1 root      root         0 Dec 20 20:38 othero

[root@linux_basic free]# rm lins

rm: cannot remove `lins': No such file or directory

[root@linux_basic free]# rm links

rm: remove regular empty file `links'? y

[cactiuser@linux_basic free]$ ls -l

lrwxrwxrwx. 1 root root    5 Dec 20 20:31 link -> links

-rw-r--r--. 1 root root    0 Dec 20 20:31 links

drwxr-xr-x. 2 root root 4096 Dec 20 20:33 too

[cactiuser@linux_basic free]$ mkdir other

mkdir: cannot create directory `other': Permission denied

[cactiuser@linux_basic free]$ ls -l /tmp/free/

[cactiuser@linux_basic free]$ ls -d /tmp/free/

/tmp/free/

[cactiuser@linux_basic free]$ ls -dl /tmp/free/

drwxr-xr-x. 3 root root 4096 Dec 20 20:32 /tmp/free/

drwxrwxrwx. 3 root root 4096 Dec 20 20:32 /tmp/free/

[cactiuser@linux_basic free]$ touch other

-rw-r--r--. 1 root      root         0 Dec 20 20:52 links

[root@linux_basic free]# cp -d link ok

lrwxrwxrwx. 1 root      root         5 Dec 20 20:53 ok -> links

继续阅读