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

相關文章
相關標籤/搜索