文件相关命令

通配符;pwd;basename;dirname;cd;touch;stat;mv;

rm;cp;dd;tree;mkdir;file;rename;cat;hexdump…

此处只介绍一般的经常使用,详细命令使用参考命令使用帮助
若内容有误,请指正,感激不尽!

1、通配符(glob)

  • 通配符:通配符是一种特殊语句,用来模糊搜索文件。通配符是用来匹配文件名的。
  • *:匹配任意长度字符
  • ?:匹配任意单个字符
  • []:匹配[]中指定的字符
    [0-9]:
    [a-z]:在bash中表示aAbB…z;其中包含大写但不包含Z
    [A-Z]:在bash中表示AbB…zZ;其中包含小写但不包含a
  • [^]:匹配[]之外的任意字符
  • ~:当前用户家目录
预定义字符类man 7 glob
  • [:digit:]:任意数字,相当于0-9
  • [:lower:]:任意小写字母
  • [:upper:]: 任意大写字母
  • [:alpha:]: 任意大小写字母
  • [:alnum:]:任意数字或字母
  • [:blank:]:水平空白字符
  • [:space:]:水平或垂直空白字符
  • [:punct:]:标点符号
  • [:print:]:可打印字符
  • [:cntrl:]:控制(非打印)字符
  • [:graph:]:图形字符
  • [:xdigit:]:十六进制字符
1、默认.不包括隐藏文件
[root@centos7 ~]# ls .(显示部分文件)
anaconda-ks.cfg  Desktop  Documents  Downloads 
 initial-setup-ks.cfg  Music  Pictures  Public  
[root@centos7 ~]# 
[root@centos7 ~]# ls -a (显示部分文件)
.  .bash_history  .bashrc  .cshrc   Documents  
 .ICEauthority   .local    .pki    .tcshrc  
.. .bash_logout   .cache   .dbus    Downloads   

2、隐藏文件可用.*表示
[root@centos7 ~]# alias l.
alias l.='ls -d .* --color=auto'
系统定义别名l.时,.*代表隐藏文件
[root@centos7 ~]# l.(显示部分文件)
.   .bash_history  .bash_profile 
 .cache   .cshrc  .esd_auth      

3、仅表示某目录下的目录可以用/.表示;
eg:显示/etc下的非隐藏目录
[root@centos7 tmp]#ls -d /etc/*/.
/etc/abrt/.     /etc/dbus-1/.    /etc/glvnd/. 
...(此处部分显示)

4、从3中得到只有目录的表示后会带/;使用rm删除软链接目录时尤其小心末尾是否带“ / ”。
示例1:/etc/sysconfig/network-scripts/ifcfg-ens33 文件末尾不可能出现/
示例2:
[root@centos7 tmp]# ln -s rootbak/ rootbak.link
[root@centos7 tmp]# ll
total 4
dr-xr-x---. 16 root root 4096 Jul 23 15:33 rootbak
lrwxrwxrwx.  1 root root    8 Jul 23 15:38 rootbak.link -> rootbak/
[root@centos7 tmp]# 
[root@centos7 tmp]# 
[root@centos7 tmp]# rm rootbak.link -f
    删除软链接目录
[root@centos7 tmp]# ls rootbak/
anaconda-ks.cfg  Desktop  Documents  Downloads  
initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos
[root@centos7 tmp]# ln -s rootbak/ rootbak.link
[root@centos7 tmp]# rm rootbak.link/ -f
rm: cannot remove ‘rootbak.link/’: Is a directory
[root@centos7 tmp]# rm rootbak.link/ -fr
[root@centos7 tmp]# ls rootbak/ 
    删除软链接目录所指向目录下的文件!!!
[root@centos7 tmp]# ll
total 0
dr-xr-x---. 2 root root 6 Jul 23 15:39 rootbak
lrwxrwxrwx. 1 root root 8 Jul 23 15:39 rootbak.link -> rootbak/

目录:首先明确当前目录,操作的对象

2、pwd

  • 因为Linux系统一切皆文件,工作时一般都处于某个路径下;所在的目录为当前的工作目录
  • CWD:current work directory当前工作目录
  • [root@centos7 tmp]#此命令提示符中tmp为当前的工作目录;
  • 显示当前工作目录命令:pwd
  • pwd:printing working directory

3、路径表示法

  • 相对路径:①指定目录以当前所在目录为起点开始描述;
    ②软链接使用相对路径时相对路径是相对于创建的链接文件
    所在目录为起点开始描述的,与当前目录无关。
  • 绝对路径:以 / 为起点开始描述某文件的具体路径。
  • 基名:basename
  • 目录名:dirname
命令示例:
相对于/etc/sysconfig/network-scripts此目录而言
basename:network-scripts
dirname:/etc/sysconfig

[root@centos7 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@centos7 network-scripts]# basename `pwd`
network-scripts
[root@centos7 network-scripts]# dirname `pwd`
/etc/sysconfig

4、cd

  • cd:切换目录
  • cd可使用相对路径和绝对路径
  • 相关环境变量:
    PWD:当前工作目录
    OLDPWD:上一次目录路径
  • cd -:切换至上一次所在目录

5、mkdir

  • mkdir:make directories创建目录
SYNOPSIS
mkdir [OPTION]... DIRECTORY...
[OPTION]
-p, --parents:
    no error if existing, make parent directories as needed
    如果存在没有错误,根据需要创建父目录
-v, --verbose:
    print a message for each created directory
    显示创建目录的详细信息
-m, --mode=MODE:
    set file mode (as in chmod), not a=rwx - umask
    设置创建文件权限而不是默认的777-umask

注意:路径基名方为命令的作用对象;基名之前的路径必须得存在。

6、rmdir

  • rmdir:remove empty directories移除空目录
SYNOPSIS  
       rmdir [OPTION]... DIRECTORY...
[OPTION]
-p, --parents:
    no error if existing, make parent directories as needed
    如果存在没有错误,根据需要创建父目录
-v, --verbose:
    print a message for each created directory
    显示创建目录的详细信息

7、{}命令行展开

  • {} 命令行展开,bash的基础特性之一
  • {} 以逗号分隔的,并能够将其展开
示例:
[root@centos7 ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@centos7 ~]# 
[root@centos7 ~]# echo {a..f}
a b c d e f
[root@centos7 ~]# echo {A..F}
A B C D E F
[root@centos7 ~]#  
[root@centos7 tmp]# touch f{1..4}.txt
[root@centos7 tmp]# ls
1.txt  f1.txt  f2.txt  f3.txt  f4.txt
[root@centos7 tmp]# mkdir dir{1..4};ls dir*
dir1:
dir2:
dir3:
dir4:
[root@centos7 ~]# mkdir /test/dir5/{x/{1,2},y}/{a,b} -p
[root@centos7 ~]# tree /test/
/test
└── dir5
    ├── x
    │   ├── 1
    │   │   ├── a
    │   │   └── b
    │   └── 2
    │       ├── a
    │       └── b
    └── y
        ├── a
        └── b
11 directories, 0 files

8、tree

  • tree - list contents of directories in a tree-like format.
  • tree - 像树状格式列出目录内容
tree  [directory ...]
-d          List directories only.
            仅列出目录
-L level    Max display depth of the directory tree.
            目录树显示的最大深度

9、stat

  • stat - display file or file system status显示文件或文件系统的状态
[root@centos7 cd]# type stat
stat is /usr/bin/stat
[root@centos7 cd]# whatis stat
stat (2)             - get file status
stat (1)             - display file or file system status
stat (3p)            - get file status

STAT(1)
SYNOPSIS
       stat [OPTION]... FILE...
[root@centos7 tmp]# stat f1.txt 
 File: ‘f1.txt’
   Size: 595       	Blocks: 8          IO Block: 4096   regular file
Device: 805h/2053d	Inode: 77          Links: 1
Access: (0644/-rw-r--r--)  Uid: (0/ root) Gid: (0/ root)
Context: system_u:object_r:etc_t:s0
Access: 2020-07-20 08:31:28.245997725 +0800
Modify: 2019-07-17 16:52:24.705604787 +0800
Change: 2019-07-23 13:56:26.209384721 +0800
  • 文件:包含两类数据:
      1、数据:data
      2、元数据:meta data
      3、stat可查看文件的元数据
  • 文件的三个时间
    Access time(Atime):访问时间
    Modify time(Mtime):内容被修改时间
    Change time(Ctime):状态改变时间;元数据改变

10、touch

  • touch - change file timestamps 更改文件时间戳
    若文件存在刷新时间戳;若文件不存在,创建新文件
  • bash下创建单个文件file:touch;>;>>
[root@centos7 tmp]# > f{1..10}.conf
-bash: f{1..10}.conf: ambiguous redirect
[root@centos7 tmp]# touch f{1..10}.conf
[root@centos7 tmp]# ls
f10.conf  f1.conf  f2.conf  f3.conf  f4.conf
 f5.conf  f6.conf  f7.conf  f8.conf  f9.conf
[root@centos7 tmp]# 
SYNOPSIS
       touch [OPTION]... FILE...
DESCRIPTION
-a     change only the access time
        只修改访问时间
-m     change only the modification time
        只修改修改时间
-c, --no-create do not create any files
        不创建空文件;
-t STAMP
        use [[CC]YY]MMDDhhmm[.ss] instead of current time
        使用([[CC]YY]MMDDhhmm[.ss])表示的时间更改当前时间戳
扩展

  若一个较大的文件占据着磁盘空间,但这个文件正在被应用程序占用,
使用rm删除时此文件时,磁盘空间并未立即释放,此时可用" > "对文件进行覆盖,以释放磁盘空间。

11、file

  • file — determine file type
  • file — 确定文件类型
  • file命令对文件的检查分为文件系统、魔法幻数检查和语言检查3个过程。
  • 有些文件内容不能有cat等文本查看工具直接查看;因此使用file查看文件类型;再使用对应的工具查看文件内容
 file file ...
-b:列出辨识结果,不显示文件名称
-f namefile:从namefile(每行一个)读取要检查的文件的名称。
-L:直接列出符号链接所指向的文件类别

12、rename

  • rename - rename files
  • rename - 修改文件名(可批量修改)
  • rename [options] expression replacement file…
[root@centos7 tmp]# ls
f10.conf  f1.conf  f2.conf  f3.conf  f4.conf  
f5.conf   f6.conf  f7.conf  f8.conf  f9.conf
[root@centos7 tmp]# rename conf conf.bak *.conf
[root@centos7 tmp]# ls
f10.conf.bak  f1.conf.bak 
f2.conf.bak  f3.conf.bak  
f4.conf.bak  f5.conf.bak 
f6.conf.bak  f7.conf.bak 
f8.conf.bak  f9.conf.bak
[root@centos7 tmp]# 

13、dd

  • dd - convert and copy a file
  • dd - 转换或复制一个文件
DESCRIPTION
Copy a file, converting and formatting according to the operands.
if=FILE     read from FILE instead of stdin
if=FILE     从FILE中读取代替从标准输入中读取
of=FILE     write to FILE instead of stdout
of=FILE     写入FILE代替到标准输出
bs=BYTES    read and write up to BYTES bytes at a time
bs=BYTES    一次读写BYTES个字节
count=N     copy only N input blocks
count=N     仅复制N块

创建文件
[root@centos7 tmp]# dd if=/dev/zero of=/data/tmp/bigfiel bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 5.6642 s, 190 MB/s
[root@centos7 tmp]# ll -h bigfiel 
-rw-r--r--. 1 root root 1.0G Jul 23 15:13 bigfiel

备份数据
[root@centos7 tmp]# ll -h /etc/passwd
-rw-r--r--. 1 root root 2.4K Jul 21 18:21 /etc/passwd
[root@centos7 tmp]# dd if=/etc/passwd of=/data/tmp/passwd.bak
4+1 records in
4+1 records out
2360 bytes (2.4 kB) copied, 0.000189807 s, 12.4 MB/s
[root@centos7 tmp]# ll -h
total 4.0K
-rw-r--r--. 1 root root 2.4K Jul 23 15:16 passwd.bak
默认bs为512Byte

14、ls

  • ls - list directory contents
  • ls - 列出目录内容
SYNOPSIS
    ls [OPTION]... [FILE]...
DESCRIPTION
-a, --all       不忽略以.开头的文件,显示全部文件
    do not ignore entries starting with .
-l,            长格式显示     
    use a long listing format
-R, --recursive 递归显示
    list subdirectories recursively
-d, --directory 只显示目录本身,不显示里面内容,常与-l连用
    list directories themselves, not their contents
-r, --reverse  排序时的逆序显示 
    reverse order while sorting
-h, --human-readable 以K,M,g为单位显示
    with -l, print sizes in human readable format (e.g., 1K 234M 2G)

l.显示隐藏文件;还带有-d选项
[root@centos7 ~]# alias l.
alias l.='ls -d .* --color=auto'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值