3.1Linux目录及文件管理

bash特性之命令别名和命令引用:
命令别名:命令的另一个名字
windows中清屏使用 cls
Linux下的清屏命令为clear
    alias:用来定义命令别名的
    alias 不跟选项和参数时,显示系统上全部的命令别名
    alias ALIAS=COMMAND
NAME
       alias - define or display aliaseslinux

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'   '这个是单引号
[root@linux_basic tmp]# alias
alias cls='clear'
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就至关于清屏了,此设置只能在当前终端中生效,切换或退出再登陆时此别名是不会生
效的,要生效则须要修改配置文件。git


    别名与命令名同名时:
执行命令自己可使用:        绝对路径
                                                    \COMMAND
[root@linux_basic tmp]# \ls
hello  mylinux  test.txt  you
[root@linux_basic tmp]# /bin/ls
hello  mylinux  test.txt  youshell

        生效范围:命令行定义的别名,其生效范围为当前会话;
在会话的设置会当前生效;在配置文件中的设置是永久有效的,但很难当即生效,须要重读配置文件windows

    unalias [ALIAS]
        -a: 撤消全部别名
NAME
       unalias - remove alias definitions  删除别名定义bash

SYNOPSIS
       unalias alias-name...app

       unalias -aide

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
alias cls='clear'
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'
[root@linux_basic ~]# unalias cls
[root@linux_basic ~]# cls
-bash: cls: command not found
[root@linux_basic ~]# 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'字体

    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.txtspa

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
hello  mylinux  test.txt  you
[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]
yoA  yoH  you  yoU
        [0-9] 匹配含有单个数字
        [0-9a-z] 匹配含有数字和字符的
    [^]:匹配指定范围之外的任意单字符
        [^0-9a-z]

        字符集合:
            [:space:] : 全部空白字符  匹配单个空白字符[[:spsce:]]
            [:punct:] : 全部标点符号
            [:lower:] :全部小写字母
            [:upper:] :全部的大写字母
            [:digit:] :全部的数字
            [:alnum:] : 全部的字母和数字
            [:alpha:] :全部的字母

    练习:
        一、显示/var目录下全部以l开头,以一个小字母结尾,且中间出现一位数字的文件或目录;
            # ls -d /var/l*[[:digit:]]*[[:lower:]]
        二、显示/etc目录下,以任意一位数字开头,且以非数字结尾的文件或目录;
            # ls -d /etc/[[:digit:]]*[^[:digit:]]
        三、显示/etc目录下,以非字母开头,后面跟了一个字母及其它任意长度字符的文件或目录;
            # ls -d /etc/[^[:alpha:]][[:alpha:]]*

    练习:
        一、在/tmp/mytest目录中建立以testdir打头,后跟当前日期和时间的空目录,形如testdir-2014-07-03-09-15-33;
            # mkdir -pv /tmp/mytest/testdir-$(date +%F-%H-%M-%S)


echo命令
NAME
       echo - write arguments to standard output
[root@linux_basic tmp]# echo "hello world"
hello world      
SYNOPSIS
       echo [string ...]

DESCRIPTION
       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

NAME
       cp - copy files and directories

SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
       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
[root@linux_basic tmp]# ls
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
        cp SRC DEST
            SRC是目录:
                复制目录,可以使用-r选项:
                -R, -r, --recursive
              copy directories recursively  递归复制目录

        cp -r SRC... DEST
[root@linux_basic tmp]# ls
A  hello  mylinux  test  test.txt  yoA  yoH  you  yoU
[root@linux_basic tmp]# cp -r /var/log mylog
[root@linux_basic tmp]# ls
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)
    [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
[root@linux_basic free]# ls
link  links  too
[root@linux_basic free]# cp link too/
[root@linux_basic free]# ls too/
link
[root@linux_basic free]# ls too/ -l
total 0
-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
[root@linux_basic free]# ls too/ -l
total 0
-rw-r--r--. 1 root root 0 Dec 20 20:31 link
[root@linux_basic free]# cp -P link too/
cp: overwrite `too/link'? y
[root@linux_basic free]# ls too/ -l
total 0
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
[root@linux_basic free]# ls -ld /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
[root@linux_basic free]# ls -ld /tmp/free
dr-xrwxrwx. 3 root root 4096 Dec 20 20:32 /tmp/free
[root@linux_basic free]# chmod +w /tmp/free
[root@linux_basic free]# ls -ld /tmp/free
drwxrwxrwx. 3 root root 4096 Dec 20 20:32 /tmp/free
[root@linux_basic free]# cp other other_to
[root@linux_basic free]# ls -l
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
[root@linux_basic free]# ls -l
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
-rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other_too
drwxr-xr-x. 2 root      root      4096 Dec 20 20:33 too
[root@linux_basic free]# cp --preserve=mode,timestamps other othero
[root@linux_basic free]# ls -l
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-rw-r--. 1 root      root         0 Dec 20 20:38 othero
-rw-r--r--. 1 root      root         0 Dec 20 20:39 other_to
-rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other_too
drwxr-xr-x. 2 root      root      4096 Dec 20 20:33 too
[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
[root@linux_basic free]# ls -l
total 4
lrwxrwxrwx. 1 root      root         5 Dec 20 20:31 link -> links
-rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other
-rw-rw-r--. 1 root      root         0 Dec 20 20:38 othero
-rw-r--r--. 1 root      root         0 Dec 20 20:39 other_to
-rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other_too
drwxr-xr-x. 2 root      root      4096 Dec 20 20:33 too

[cactiuser@linux_basic free]$ ls -l
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
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/
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
drwxr-xr-x. 2 root root 4096 Dec 20 20:33 too
[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/
[cactiuser@linux_basic free]$ ls -dl /tmp/free/
drwxrwxrwx. 3 root root 4096 Dec 20 20:32 /tmp/free/
[cactiuser@linux_basic free]$ touch other
[cactiuser@linux_basic free]$ ls -l
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
drwxr-xr-x. 2 root      root      4096 Dec 20 20:33 too

[root@linux_basic free]# touch links [root@linux_basic free]# ls -l total 4 lrwxrwxrwx. 1 root      root         5 Dec 20 20:31 link -> links -rw-r--r--. 1 root      root         0 Dec 20 20:52 links -rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other -rw-rw-r--. 1 root      root         0 Dec 20 20:38 othero -rw-r--r--. 1 root      root         0 Dec 20 20:39 other_to -rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other_too drwxr-xr-x. 2 root      root      4096 Dec 20 20:33 too [root@linux_basic free]# cp -d link ok [root@linux_basic free]# ls -l total 4 lrwxrwxrwx. 1 root      root         5 Dec 20 20:31 link -> links -rw-r--r--. 1 root      root         0 Dec 20 20:52 links lrwxrwxrwx. 1 root      root         5 Dec 20 20:53 ok -> links -rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other -rw-rw-r--. 1 root      root         0 Dec 20 20:38 othero -rw-r--r--. 1 root      root         0 Dec 20 20:39 other_to -rw-rw-r--. 1 cactiuser cactiuser    0 Dec 20 20:38 other_too drwxr-xr-x. 2 root      root      4096 Dec 20 20:33 too

相关文章
相关标签/搜索