首页 Linux教程linux文件操作

linux系统目录结构

linux用户管理

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

文件操作命令

显示当前工作目录

每个shell和系统进程都有一个当前的工作目录 CWD:current work directory

显示当前shell CWD的绝对路径

pwd命令: printing working directory

  • -P 显示真实物理路径
  • -L 显示链接路径(默认)

范例:

[root@centos8 ~]#pwd
/root
[root@centos8 ~]#cd /data
[root@centos8 data]#pwd
/data
[14:38:41 root@centos8 sysconfig]#cd /bin
[14:38:52 root@centos8 bin]#pwd
/bin
[14:38:54 root@centos8 bin]#ll /
total 28
lrwxrwxrwx.   1 root root    7 May 11  2019 bin -> usr/bin
dr-xr-xr-x.   6 root root 4096 Dec 11 11:21 boot
drwxr-xr-x.   2 root root  142 Dec 13 14:26 data
drwxr-xr-x.  19 root root 3280 Dec 13 11:21 dev
drwxr-xr-x. 142 root root 8192 Dec 13 14:26 etc
drwxr-xr-x.   3 root root   18 Dec 11 11:19 home
lrwxrwxrwx.   1 root root    7 May 11  2019 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 May 11  2019 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 May 11  2019 media
drwxr-xr-x.   3 root root   18 Dec 11 11:15 mnt
drwxr-xr-x.   2 root root    6 May 11  2019 opt
dr-xr-xr-x. 259 root root    0 Dec 13 10:34 proc
dr-xr-x---.  15 root root 4096 Dec 13 14:26 root
drwxr-xr-x.  42 root root 1200 Dec 13 11:03 run
lrwxrwxrwx.   1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 May 11  2019 srv
dr-xr-xr-x.  13 root root    0 Dec 13 10:34 sys
drwxrwxrwt.  20 root root 4096 Dec 13 14:14 tmp
drwxr-xr-x.  12 root root  144 Dec 11 11:12 usr
drwxr-xr-x.  22 root root 4096 Dec 11 11:21 var
[14:38:57 root@centos8 bin]#pwd -P
/usr/bin

绝对和相对路径

  • 绝对路径
    以正斜杠/ 即根目录开始
    完整的文件的位置路径
    可用于任何想指定一个文件名的时候
  • 相对路径名
    不以斜线开始
    一般情况下,是指相对于当前工作目录的路径,特殊场景下,是相对于某目录的位置
    可以作为一个简短的形式指定一个文件名

基名:basename,只取文件名而不要路径
目录名:dirname,只取路径,不要文件名

范例:

[root@centos8 bin]#basename /etc/sysconfig/network
network
[root@centos8 bin]#dirname /etc/sysconfig/network
/etc/sysconfig

[root@centos8 ~]#dirname  /etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts
[root@centos8 ~]#basename /etc/sysconfig/network-scripts/ifcfg-eth0
ifcfg-eth0

更改目录

命令 cd : change directory 改变目录

选项:-P 切换至物理路径,而非软链接目录

可以使用绝对或相对路径

  • 切换至父目录: cd ..
  • 切换至当前用户主目录: cd
  • 切换至以前的工作目录: cd –

范例:

[root@centos8 ~]#cd /etc/sysconfig
[root@centos8 sysconfig]#pwd
/etc/sysconfig
[root@centos8 sysconfig]#cd ../../data
[root@centos8 data]#pwd
/data
[root@centos8 data]#cd /bin
[root@centos8 bin]#pwd
/bin
[root@centos8 bin]#cd -P /bin 
[root@centos8 bin]#pwd
/usr/bin

相关的环境变量:

  • PWD:当前目录路径
  • OLDPWD:上一次目录路径

范例:

[root@centos8 ~]#echo $OLDPWD
/etc/sysconfig
[root@centos8 ~]#cd -
/etc/sysconfig
[root@centos8 sysconfig]#pwd
/etc/sysconfig
[root@centos8 sysconfig]#

列出目录内容

ls 命令可以列出当前目录的内容或指定目录

用法:

ls [options] [files_or_dirs]

常见选项:

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

说明:

ls 查看不同后缀文件时的颜色由 /etc/DIR_COLORS 和@LS_COLORS变量定义

范例:

[root@centos8 ~]#vim /etc/DIR_COLORS
.jpg 01;31  #修改此行
[root@centos8 ~]#exit 
[root@centos8 ~]#echo $LS_COLORS
rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;31:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.m4a=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.oga=01;36:*.opus=01;36:*.spx=01;36:*.xspf=01;36:

linux文件操作插图

查看文件状态 stat

文件相关信息:metadata, data

每个文件有三个时间戳:

  • access time 访问时间,atime,读取文件内容
  • modify time 修改时间,mtime,改变文件内容(数据)
  • change time 改变时间,ctime,元数据发生改变
[root@centos8 ~]#stat /etc/passwd
  File: /etc/passwd
  Size: 1306        Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d  Inode: 134792556   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 20:37:12.830991350 +0800
Modify: 2019-12-09 20:37:12.826991351 +0800
Change: 2019-12-09 20:37:12.826991351 +0800
 Birth: -

确定文件内容

文件可以包含多种类型的数据,使用file命令检查文件的类型,然后确定适当的打开命令或应用程序使用

格式:

file [options] <filename>...

常用选项:

  • -b 列出文件辨识结果时,不显示文件名称
  • -f filelist 列出文件filelist中文件名的文件类型
  • -F 使用指定分隔符号替换输出文件名后默认的”:”分隔符
  • -L 查看对应软链接对应文件的文件类型
  • –help 显示命令在线帮助

范例:windows的文本格式和Linux的文本格式的区别

[root@centos8 data]#cat linux.txt
a
b
c
[root@centos8 data]#cat win.txt
a
b
c[root@centos8 data]#file win.txt linux.txt
win.txt:   ASCII text, with CRLF line terminators
linux.txt: ASCII text
[root@centos8 data]#hexdump -C linux.txt
00000000  61 0a 62 0a 63 0a                                 |a.b.c.|
00000006
[root@centos8 data]#hexdump  -C win.txt
00000000  61 0d 0a 62 0d 0a 63                              |a..b..c|
00000007

#安装转换工具
[root@centos8 data]#dnf -y install dos2unix

#将Windows的文本格式转换成的Linux文本格式
[root@centos8 data]#dos2unix  win.txt
dos2unix: converting file win.txt to Unix format...
[root@centos8 data]#file win.txt
win.txt: ASCII text

#将Linux的文本格式转换成Windows的文本格式
[root@centos8 data]#unix2dos  win.txt
unix2dos: converting file win.txt to DOS format...
[root@centos8 data]#file win.txt
win.txt: ASCII text, with CRLF line terminators

范例:转换文件字符集编码

#显示支持字符集编码列表
[root@centos8 ~]#iconv -l

#windows10上文本默认的编码ANSI(GB2312)
[root@centos8 data]#file windows.txt
windows.txt: ISO-8859 text, with no line terminators

[root@centos8 data]#echo $LANG
en_US.UTF-8

#默认在linux无法正常显示文本内容
[root@centos8 data]#cat windows11.txt
▒▒▒▒▒▒[root@centos8 data]#

#将windows10上文本默认的编码ANSI(GB2312)转换成UTF-8
[root@centos8 data]#iconv -f gb2312 windows.txt  -o windows1.txt
[root@centos8 data]#cat windows1.txt
马哥教育[root@centos8 data]#ll windows1.txt
-rw-r--r-- 1 root root 12 Mar 23 10:13 windows1.txt
[root@centos8 data]#file windows1.txt
windows1.txt: UTF-8 Unicode text, with no line terminators

#将UTF-8转换成windows10上文本默认的编码ANSI(GB2312)
[root@centos8 data]#iconv -f utf8 -t gb2312 windows1.txt -o  windows11.txt
[root@centos8 data]#file windows11.txt
windows11.txt: ISO-8859 text, with no line terminators

范例:

#将windows10上文本默认的编码ANSI(GB2312)转换成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

范例:

[root@centos8 ~]#cat list.txt 
/etc/
/bin
/etc/issue
[root@centos8 ~]#file -f list.txt
/etc/:      directory
/bin:       symbolic link to usr/bin
/etc/issue: ASCII text

文件通配符模式 wildcard pattern

文件通配符可以用来匹配符合条件的多个文件,方便批量管理文件

通配符采有特定的符号,表示特定的含义,此特符号称为元 meta 字符

常见的通配符如下:

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

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

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

范例:

[root@centos8 wang]#echo $OLDPWD
/data
[root@centos8 wang]#ll -/linux.txt
ls: invalid option -- '/'
Try 'ls --help' for more information.
[root@centos8 wang]#ll ~-/linux.txt
-rw-r--r-- 1 root root 6 Mar 23 09:48 /data/linux.txt

范例:

[root@centos8 data]#touch file{a..z}.txt file{A..Z}.txt file{0.9}.txt
[root@centos8 data]#ll file{0..9}.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file0.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file1.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file2.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file3.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file4.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file5.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file6.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file7.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file8.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file9.txt
[root@centos8 data]#ll file[0-9].txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file0.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file1.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file2.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file3.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file4.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file5.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file6.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file7.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file8.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file9.txt

[root@centos8 data]#ls file[a-c].txt
filea.txt  fileA.txt  fileb.txt  fileB.txt  filec.txt
[root@centos8 data]#ls file[C-E].txt
fileC.txt  filed.txt  fileD.txt  filee.txt  fileE.txt

[root@centos8 data]#ls file[wang].txt
filea.txt  fileg.txt  filen.txt  filew.txt
[root@centos8 data]#ls file[^wang].txt

范例:

[root@centos8 data]#ls file[:lower:].txt
filee.txt  filel.txt  fileo.txt  filer.txt  filew.txt
[root@centos8 data]#ls file[[:lower:]].txt
filea.txt  filed.txt  fileg.txt  filej.txt  filem.txt  filep.txt  files.txt  filev.txt  filey.txt
fileb.txt  filee.txt  fileh.txt  filek.txt  filen.txt  fileq.txt  filet.txt  filew.txt  filez.txt
filec.txt  filef.txt  filei.txt  filel.txt  fileo.txt  filer.txt  fileu.txt  filex.txt

范例:比较有无*的功能区别

[root@centos8 data]#ls -a *
[root@centos8 data]#ls -a

范例:

[root@centos8 ~]#ls -d /etc/*/
[root@centos8 ~]#l.

范例:

[root@centos8 data]#touch file*.log
[root@centos8 data]#touch file1.log
[root@centos8 data]#ls file*.log
 file1.log  'file*.log'
[root@centos8 data]#ls 'file*.log'
'file*.log'
[root@centos8 data]#ls 'file*.log'

范例:
1、显示/etc目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录列表
2、显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录列表
3、显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录列表
4、显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录列表
5、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录列表
6、只显示/root下的隐藏文件和目录列表
7、只显示/etc下的非隐藏目录列表

创建空文件和刷新时间

touch命令可以用来创建空文件或刷新文件的时间

格式:

touch [OPTION]... FILE...

选项说明:

  • -a 仅改变 atime和ctime
  • -m 仅改变 mtime和ctime
  • -t [[CC]YY]MMDDhhmm[.ss] 指定atime和mtime的时间戳
  • -c 如果文件不存在,则不予创建

范例:

[root@centos8 data]#touch f1.txt
[root@centos8 data]#ll
total 0
-rw-r--r-- 1 root root 0 Mar 23 11:30 f1.txt
[root@centos8 data]#ll  /etc/issue
-rw-r--r--. 1 root root 23 Jan  2 23:21 /etc/issue
[root@centos8 data]#touch /etc/issue
[root@centos8 data]#ll /etc/issue
-rw-r--r--. 1 root root 23 Mar 23 11:31 /etc/issue
[root@centos8 data]#stat /etc/issue
  File: /etc/issue
  Size: 23              Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d      Inode: 134345998   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-03-23 11:31:20.030291332 +0800
Modify: 2020-03-23 11:31:20.030291332 +0800
Change: 2020-03-23 11:31:20.030291332 +0800
 Birth: -

[root@centos8 data]#date
Fri Dec 13 16:11:55 CST 2019
[root@centos8 data]#touch date -d "-1 day" +%F_%T.log
[root@centos8 data]#ls
2019-12-12_16:11:48.log
[root@centos8 data]#touch $(date -d "1 year" +%F_%T).log
[root@centos8 data]#ls
2019-12-12_16:11:48.log  2020-12-13_16:13:11.log

linux文件操作插图1复制文件和目录

image-20191209211901026

利用 cp(copy)命令可以实现文件或目录的复制

格式:

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

常用选项

  • -i 如果目标已存在,覆盖前提示是否覆盖
  • -n 不覆盖,注意两者顺序
  • -r, -R 递归复制目录及内部的所有内容
  • -a 归档,相当于-dR –preserv=all,常用于备份功能
  • -d –no-dereference –preserv=links 不复制原文件,只复制链接名
  • –preserv[=ATTR_LIST]
    mode: 权限
    ownership: 属主属组
    timestamp:
    links
    xattr
    context
    all
  • -p 等同–preserv=mode,ownership,timestamp
  • -v –verbose
  • -f –force
  • -u –update 只复制源比目标更新文件或目标不存在的文件
  • -b 目标存在,覆盖前先备份,默认形式为 filename~,只保留最近的一个备份
  • –backup=numbered 目标存在,覆盖前先备份加数字后缀,形式为filename.~#~,可以保留多个版本
源\目标 不存在 存在且为文件 存在且为目录
一个文件 新建DEST,并将SRC中内容填充至DEST中 将SRC中的内容覆盖至DEST中 注意数据丢失风险! 建议用 –i 选项 在DEST下新建与原文件同名的文件,并将SRC中内容填充至新文件中
多个文件 提示错误 提示错误 在DEST下新建与原文件同名的文件,并将原文件内容复制进新文件中
目录须使用-r选项 创建指定DEST同名目录,复制SRC目录中所有文件至DEST下 提示错误 在DEST下新建与原目录同名的目录,并将SRC中内容复制至新目录中

范例:

[16:21:08 root@centos8 ~]#cp ~wang/issue  /data/issue_wang.bak
-rw-r--r--. 1 root root 86 Dec 13 16:21 /data/issue_wang.bak
-rw-r--r--. 1 wang wang 86 Dec 13 16:18 /home/wang/issue
[16:21:44 root@centos8 ~]#cp  -p ~wang/issue /data/issue_wang2.bak
[16:22:07 root@centos8 ~]#ll  ~wang/issue /data/issue_wang2.bak
-rw-r--r--. 1 wang wang 86 Dec 13 16:18 /data/issue_wang2.bak
-rw-r--r--. 1 wang wang 86 Dec 13 16:18 /home/wang/issue
[16:23:19 root@centos8 ~]#cp /etc/sysconfig/  /data/
cp: -r not specified; omitting directory '/etc/sysconfig/'
[16:23:40 root@centos8 ~]#cp -r /etc/sysconfig/  /data/
[16:23:45 root@centos8 ~]#ll /data
total 12
-rw-r--r--. 1 root root    0 Dec 13 16:11 2019-12-12_16:11:48.log
-rw-r--r--. 1 root root    0 Dec 13 16:13 2020-12-13_16:13:11.log
-rw-r--r--. 1 wang wang   86 Dec 13 16:18 issue_wang2.bak
-rw-r--r--. 1 root root   86 Dec 13 16:21 issue_wang.bak
drwxr-xr-x. 7 root root 4096 Dec 13 16:23 sysconfig
[16:24:03 root@centos8 ~]#cp -r /etc/sysconfig/  /data/sysconfig_bak
[16:24:34 root@centos8 ~]#ll /data
total 16
-rw-r--r--. 1 root root    0 Dec 13 16:11 2019-12-12_16:11:48.log
-rw-r--r--. 1 root root    0 Dec 13 16:13 2020-12-13_16:13:11.log
-rw-r--r--. 1 wang wang   86 Dec 13 16:18 issue_wang2.bak
-rw-r--r--. 1 root root   86 Dec 13 16:21 issue_wang.bak
drwxr-xr-x. 7 root root 4096 Dec 13 16:23 sysconfig
drwxr-xr-x. 7 root root 4096 Dec 13 16:24 sysconfig_bak

[16:32:43 root@centos8 ~]#cp -b  /etc/motd /data/issue
cp: overwrite '/data/issue'? y 
[16:33:09 root@centos8 ~]#ll /data
total 8
-rw-r--r--. 1 root root    1 Dec 13 16:33 issue
-rw-r--r--. 1 root root 2610 Dec 13 16:32 issue~
[16:33:11 root@centos8 ~]#cp -b  /etc/fstab  /data/issue
cp: overwrite '/data/issue'? y
[16:33:55 root@centos8 ~]#ll /data
total 8
-rw-r--r--. 1 root root 709 Dec 13 16:33 issue
-rw-r--r--. 1 root root   1 Dec 13 16:33 issue~
[16:33:56 root@centos8 ~]#cp --backup=numbered   /etc/fstab  /data/issue
cp: overwrite '/data/issue'? y
[16:34:32 root@centos8 ~]#cp --backup=numbered   /etc/shadow  /data/issue
cp: overwrite '/data/issue'? y
[16:34:41 root@centos8 ~]#cp --backup=numbered   /etc/os-release  /data/issue
cp: overwrite '/data/issue'? y
[16:34:48 root@centos8 ~]#ll /data
total 20
-rw-r--r--. 1 root root  420 Dec 13 16:34 issue
-rw-r--r--. 1 root root    1 Dec 13 16:33 issue~
-rw-r--r--. 1 root root  709 Dec 13 16:33 issue.~1~
-rw-r--r--. 1 root root  709 Dec 13 16:34 issue.~2~
----------. 1 root root 1349 Dec 13 16:34 issue.~3~

范例:

[root@centos8 ~]#cp /dev/zero /data/zero.bak

练习
1、每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为 backupYYYY-mm-dd,备份过程可见

[root@centos8 ~]#cp -av /etc/ /data/backupdate +%F

2、创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有权限

[root@centos8 ~]#cp -a /root /data/rootdir

移动和重命名文件

mv 命令可以实现文件或目录的移动和改名

同一分区移动数据,速度很快:数据位置没有变化

不同分区移动数据,速度相对慢:数据位置发生了变化

格式:

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

常用选项:

  • -i 交互式
  • -f 强制
  • -b 目标存在,覆盖前先备份

利用rename 可以批量修改文件名

格式:

rename [options] <expression> <replacement> <file>...

范例:

#为所有的conf文件加上.bak后缀: 
rename  'conf'    'conf.bak'   f* 
#去掉所有的bak后缀: 
rename  '.bak'  ''  *.bak

删除文件

linux文件操作插图3

使用 rm 命令可以删除文件

注意:此命令非常危险,慎重使用,建议使用mv 代替 rm

格式:

rm [OPTION]... FILE...

常用选项:

  • -i 交互式
  • -f 强制删除
  • -r 递归
  • –no-preserve-root 删除/

范例:

[root@centos8 ~]#rm -rf /
rm: it is dangerous to operate recursively on '/'
rm: use --no-preserve-root to override this failsafe

rm -rf    /*

范例:删除特殊文件

#将名为 “/data/-f” 的文件删除
[root@centos8 data]#rm -f -f  #此方式错误
[root@centos8 data]#rm -- -f
[root@centos8 data]#rm ./-f
[root@centos8 data]#rm /data/-f

[root@centos8 data]#touch '~'
[root@centos8 data]#ls
'~'
[root@centos8 data]#rm -f ~
rm: cannot remove '/root': Is a directory
[root@centos8 data]#rm -- ~
rm: cannot remove '/root': Is a directory
[root@centos8 data]#rm -f ./~

rm虽然删除了文件,但是被删除的文件仍然可能被恢复,在安全要求较高的场景下,可以使用shred安全删除文件

shred格式

shred [OPTION]... FILE...

常见选项:
-z 最后一次覆盖添加0,以隐藏覆盖操作
-v 能够显示操作进度
-u 覆盖后截断并删除文件
-n # 指定覆盖文件内容的次数(默认值是3次)

范例:

[root@centos8 ~]#shred  -zvun 5 passwords.txt
shred: passwords.txt: pass 1/6 (random)...
shred: passwords.txt: pass 2/6 (000000)...
shred: passwords.txt: pass 3/6 (random)...
shred: passwords.txt: pass 4/6 (ffffff)...
shred: passwords.txt: pass 5/6 (random)...
shred: passwords.txt: pass 6/6 (000000)...
shred: passwords.txt: removing
shred: passwords.txt: renamed to 0000000000000
shred: 0000000000000: renamed to 000000000000
shred: 000000000000: renamed to 00000000000
shred: 00000000000: renamed to 0000000000
shred: 0000000000: renamed to 000000000
shred: 000000000: renamed to 00000000
shred: 00000000: renamed to 0000000
shred: 0000000: renamed to 000000
shred: 000000: renamed to 00000
shred: 00000: renamed to 0000
shred: 0000: renamed to 000
shred: 000: renamed to 00
shred: 00: renamed to 0
shred: passwords.txt: removed
[root@centos8 ~]#ls passwords.txt
ls: cannot access 'passwords.txt': No such file or directory

目录操作

显示目录树 tree

常见选项:

  • -d: 只显示目录
  • -L level:指定显示的层级数目
  • -P pattern: 只显示由指定wild-card pattern匹配到的路径
创建目录 mkdir

常见选项:

  • -p: 存在于不报错,且可自动创建所需的各目录
  • -v: 显示详细信息
  • -m MODE: 创建目录时直接指定权限
删除空目录rmdir

常见选项:

  • -p 递归删除父空目录
  • -v 显示详细信息

注意:rmdir只能删除空目录,如果想删除非空目录,可以使用rm -r 命令,递归删除目录树

范例:

alias rm='DIR=/data/backupdate +%F%T;mkdir $DIR;mv -t $DIR'

本文链接:https://www.yunweipai.com/33862.html

linux系统目录结构

linux用户管理

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

暂无评论

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部