3.1Linux目录及文件管理

Wesley13
• 阅读 595

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'   '这个是单引号
[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就相当于清屏了,此设置只能在当前终端中生效,切换或退出再登录时此别名是不会生
效的,要生效则需要修改配置文件。

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

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

    unalias [ALIAS]
        -a: 撤消所有别名
NAME
       unalias - remove alias definitions  删除别名定义

SYNOPSIS
       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
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.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
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:] :所有的字母

    练习:
        1、显示/var目录下所有以l开头,以一个小字母结尾,且中间出现一位数字的文件或目录;
            # ls -d /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命令
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 . If there are no arguments, only the  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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
sql注入
反引号是个比较特别的字符,下面记录下怎么利用0x00SQL注入反引号可利用在分隔符及注释作用,不过使用范围只于表名、数据库名、字段名、起别名这些场景,下面具体说下1)表名payload:select\from\users\whereuser\_id1limit0,1;!(https://o
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这