zoukankan      html  css  js  c++  java
  • Linux文件系统和管理-2文件操作命令(上)

    文件操作命令

    文件

    文件也包括目录
    目录是一种特殊的文件

    目录

    一个目录名分成两部分

    • 所在目录 dirname 父目录的路径
    • 文件名 basename
      本身就是两个命令
    [root@C8-1 misc]# type dirname
    dirname is /usr/bin/dirname
    [root@C8-1 misc]# type basename
    basename is /usr/bin/basename
    

    相对路径和绝对路径

    • 绝对路径 从根开始,完整的路径 必须以正斜杠/即根目录开始
    • 相对路径 相对于当前工作目录 不以/开始

    显示当前路径

    pwd printing working directory 打印工作目录

    换目录或路径

    cd change directory 改变目录

    • 切换至父目录: cd ..
    • 切换至当前用户主目录: cd . 点可以不写
    • 切换至以前的工作目录: cd - $OLDPWD记着呢,系统自带的变量
    [root@C8-1 boot]# echo $OLDPWD
    /bin
    [root@C8-1 boot]# cd /
    [root@C8-1 /]# echo $OLDPWD
    /boot
    

    根的根就是根

    一个点 . 是自己 当前文件夹
    两个点 .. 是父目录
    波浪符 ~ 回到对应id的家目录

    [root@C8-1 /]# ls -a
    .   bin   dev  home  lib64  misc  net  proc  run   srv  tmp  var
    ..  boot  etc  lib   media  mnt   opt  root  sbin  sys  usr
    

    列出目录内容

    ls

    ls 命令可以列出当前目录的内容或指定目录
    ll 是ls -l 的别命

    [root@C8-1 boot]# type ll
    ll is aliased to `ls -l --color=auto'
    

    常见选项:

    -a 包含隐藏文件
    -d 显示目录本身的内容
    -l 显示额外的信息
    -R 目录递归 显示子目录
    -ld 目录和符号链接信息 显示目录本身的内容
    -1 文件分行显示
    -S 按从大到小排序
    -t 按mtime排序 每个文件都有三个时间
    -u 配合-t选项,显示并按atime从新到旧排序
    -U 按目录存放顺序显示
    -X 按文件后缀排序

    查看文件状态 stat

    ll --time 太麻烦了,用stat直接看三个时间

    文件相关信息:

    • metadata
    • data

    每个文件有三个时间戳:

    • access time 访问时间,atime,读取文件内容
    • modify time 修改时间,mtime,改变文件内容(数据) 默认
    • change time 改变时间,ctime,元数据发生改变

    Inode 每个为你结案的唯一标识

    [root@C8-1 ~]# stat .bash_profile 
      File: .bash_profile
      Size: 207       	Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 34011036    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: system_u:object_r:admin_home_t:s0
    Access: 2020-06-17 05:04:45.933534066 -0400
    Modify: 2020-06-15 23:06:02.116855753 -0400
    Change: 2020-06-15 23:06:02.117855758 -0400
     Birth: -
    

    确定文件内容

    file

    file 每个文件的格式类型不一样,文件的头不确定了文件的类型,file帮我们确定
    Linux中文件的后缀要求并不严格。
    file可以判断七种文件类型。

    [root@C8-1 ~]# file .bash_profile 
    .bash_profile: ASCII text
    [root@C8-1 ~]# type file
    file is hashed (/usr/bin/file)
    [root@C8-1 ~]# file /usr/bin/file
    /usr/bin/file: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=1aaef7e4e7e253a9191d2e7e43d5e7dd8c8ab1e8, stripped
    [root@C8-1 ~]# file /dev/zero
    /dev/zero: character special (1/5)
    [root@C8-1 ~]# file /dev/sda
    /dev/sda: block special (8/0)
    

    Linux和Windows文件在底层是有一些差别的

    [root@C8-1 ~]# ll
    total 12
    -rw-------. 1 root root 1184 Mar 16 01:56 anaconda-ks.cfg
    -rw-r--r--. 1 root root    6 Jun 17 06:22 linux.txt
    -rw-r--r--. 1 root root    7 Jun 17 06:21 win.txt
    [root@C8-1 ~]# hexdump -C linux.txt
    00000000  61 0a 62 0a 63 0a                                 |a.b.c.|
    00000006
    [root@C8-1 ~]# hexdump -C win.txt
    00000000  61 0d 0a 62 0d 0a 63                              |a..b..c|
    00000007
    [root@C8-1 ~]# file linux.txt 
    linux.txt: ASCII text
    [root@C8-1 ~]# file win.txt 
    win.txt: ASCII text, with CRLF line terminators
    

    显示文件编码iconv

    Linux一直使用UTF-8,早期的中文版windows使用ANSIC,到win10 才使用UTF-8
    用iconv可以显示所有的编码格式

    #显示编码列表
    [15:34:50 root@centos8 ~]#iconv -l
    #将windows10上文本默认的编码转换成UTF-8
    [15:34:50 root@centos8 ~]#iconv -f gb2312 win.txt -o win2.txt
    [15:34:50 root@centos8 ~]#file linux.txt
    linux.txt: ASCII text
    [15:34:31 root@centos8 ~]#file windows.txt
    windows.txt: ASCII text, with CRLF line terminators
    #将windows的文本格式转换成Linux的文本格式
    [15:35:26 root@centos8 ~]#dos2unix windows.txt
    dos2unix: converting file windows.txt to Unix format...
    [15:36:00 root@centos8 ~]#file windows.txt
    windows.txt: ASCII text
    #将Linux的文本格式转换成windows的文本格式
    [15:36:02 root@centos8 ~]#unix2dos windows.txt
    unix2dos: converting file windows.txt to DOS format...
    [15:36:10 root@centos8 ~]#file windows.txt
    windows.txt: ASCII text, with CRLF line terminators
    [15:33:05 root@centos8 ~]#cat list.txt
    /etc/
    /bin
    /etc/issue
    [15:34:28 root@centos8 ~]#file -f list.txt
    /etc/: directory
    /bin: symbolic link to usr/bin
    /etc/issue: ASCII text
    

    文件通配符模式 wildcard pattern

    通配符就是为了搜索和匹配文件的时候更方便。

    常见的通配符如下:

    * 匹配零个或多个字符 但是匹配不了.开头的隐藏文件,*不包含隐藏文件
    ? 匹配任何单个字符
    ~ 当前用户家目录
    ~pang 用户pang家目录
    ~+和. 当前工作目录
    ~- 前一个工作目录
    [0-9] 匹配数字范围
    [a-z] 字母
    [A-Z] 字母
    [pang] 匹配列表中的任何的一个字符
    [^pang] 匹配列表中的所有字符以外的字符
    

    别外还有在Linux系统中预定义的字符类:man 7 glob

    [:digit:]:任意数字,相当于0-9
    [:lower:]:任意小写字母,表示 a-z
    [:upper:]: 任意大写字母,表示 A-Z
    [:alpha:]: 任意大小写字母
    [:alnum:]:任意数字或字母
    [:blank:]:水平空白字符
    [:space:]:水平或垂直空白字符
    [:punct:]:标点符号
    [:print:]:可打印字符
    [:cntrl:]:控制(非打印)字符
    [:graph:]:图形字符
    [:xdigit:]:十六进制字符
    

    范例:

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

    #先创建一些符合条件的文件
    [root@C8-1 ~]# touch /etc/l{0..9}bcd.test
    #直接写l[0-9]表示l开头第二位是0-9的数字的文件或文件夹,所以不符合要求
    [root@C8-1 ~]# ls /etc/l[0-9]
    ls: cannot access '/etc/l[0-9]': No such file or directory
    #需要配合*显示多个无所谓的字符
    [root@C8-1 ~]# ls /etc/l[0-9]*
    /etc/l0bcd.test  /etc/l2bcd.test  /etc/l4bcd.test  /etc/l6bcd.test  /etc/l8bcd.test
    /etc/l1bcd.test  /etc/l3bcd.test  /etc/l5bcd.test  /etc/l7bcd.test  /etc/l9bcd.test
    #在结尾一个字符出加上取值范围[a-z]即可
    [root@C8-1 ~]# ls /etc/l[0-9]*[a-z]
    /etc/l0bcd.test  /etc/l2bcd.test  /etc/l4bcd.test  /etc/l6bcd.test  /etc/l8bcd.test
    /etc/l1bcd.test  /etc/l3bcd.test  /etc/l5bcd.test  /etc/l7bcd.test  /etc/l9bcd.test
    #
    

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

    #先创建一些符合要求的文件
    [root@C8-1 ~]# touch /etc/{0..9}{a..c}.618bu-
    [root@C8-1 ~]# ls /etc/[0-9]*[^0-9]
    /etc/0a.618bu-  /etc/1a.618buZ  /etc/2b.618buY  /etc/3c.618buy  /etc/5a.618bu-  /etc/6a.618buZ  /etc/7b.618buY  /etc/8c.618buy
    …… /etc/9c.618buZ
    

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

    [root@C8-1 ~]# ls /etc/[^[:alpha:]][[:alpha:]]*
    

    4、显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录

    [root@C8-1 ~]# ls -d /etc/rc[0-6]*
    

    5、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录

    [root@C8-1 ~]# ls /etc/[mnrp]*.conf
    /etc/man_db.conf  /etc/mke2fs.conf  /etc/nsswitch.conf  /etc/resolv.conf  /etc/rsyslog.conf
    

    6、只显示/root下的隐藏文件和目录

    [root@C8-1 ~]# ls -d .*
    .  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc  .viminfo
    

    7、只显示/etc下的非隐藏目录
    只列文件夹不列文件

    [root@C8-1 ~]# ls -d /etc/*/
    /etc/alternatives/       /etc/dbus-1/         /etc/kernel/          /etc/oddjob/          /etc/rc3.d/           /etc/sssd/
    /etc/ansible/            /etc/dconf/          /etc/krb5.conf.d/     /etc/oddjobd.conf.d/  /etc/rc4.d/           /etc/sudoers.d/
    ……
    
    * * * 胖并快乐着的死肥宅 * * *
  • 相关阅读:
    Conda 中安装 Keras
    Clean Docker :
    Conservation Vs Non-conservation Forms of conservation Equations
    C语言之预处理命令
    const指针用法总结
    C语言中的小启发(陆续更新。。。)
    左值与右值
    计算的次序
    C语言中的符号重载
    C语言之switch语句详解
  • 原文地址:https://www.cnblogs.com/bpzblog/p/13160716.html
Copyright © 2011-2022 走看看