美文网首页
linux文件操作命令

linux文件操作命令

作者: 枕梦_a280 | 来源:发表于2018-11-24 14:34 被阅读0次

常用的文件操作命令

我们在使用linux的时候,对文件的操作是必不可少的,例如创建,修改,重命名,删除,查看,统计等。

  • 对目录操作命令
    mkdir创建一个或者多个目录
格式:mkdir [选项] 目录
1:在根目录下创建一个名为test的目录
[root@#localhost ~]# mkdir /test
[root@#localhost ~]# ll /test/ -d
drwxr-xr-x. 2 root root 6 Nov 24 14:15 /test/
2:一次性创建多个目录
[root@#localhost test]# mkdir test{1..10}
[root@#localhost test]# ls
test1  test10  test2  test3  test4  test5  test6  test7  test8  test9
3:创建多级目录
[root@#localhost ~]# mkdir -p /test/aaa/bbb/ccc/
[root@#localhost ~]# cd /test/aaa/
[root@#localhost aaa]# cd ./bbb/
[root@#localhost bbb]# cd ./ccc/

rmdir删除空目录

1:删除/test/aaa/bbb/目录下的ccc目录
[root@#localhost bbb]# rmdir ccc/
[root@#localhost bbb]# ls ccc
ls: cannot access ccc: No such file or directory

2:使用 -p 参数删除多级空目录删除(test下的aaa目录和aaa下的bbb被一并删除了,而test目录非空没有被删除)
[root@#localhost ~]# rmdir /test/aaa/bbb/ -p
[root@#localhost ~]# ls /test/aaa
ls: cannot access /test/aaa: No such file or directory
[root@#localhost ~]# ls /test/
test1  test10  test2  test3  test4  test5  test6  test7  test8  test9

3:使用rm命令删除(-r表示删除目录 -f表示强制删除不需要确认 rm -rf)
[root@#localhost ~]# rm -rf /test/aaa/bbb/ccc/
rm命令会将所有文件一并删除,慎用。
[root@#localhost test]# rm -rf /test/
[root@#localhost test]# ls /test
ls: cannot access /test: No such file or directory
  • 普通文件的操作命令
    touch命令创建一个或多个文件,rm命令删除文件
    touch命令修改文件的属性信息(如:读取时间和修改时间)
格式:touch [选项] [文件]
1:在/test目录下创建1.txt文件
[root@#localhost ~]# touch /test/1.txt
[root@#localhost test]# ls /test/1.txt 
/test/1.txt

2:在/test目录下创建多个文件
[root@#localhost ~]# touch /test/{2..9}.txt
[root@#localhost ~]# cd /test/
[root@#localhost test]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt 

3:批量删除以'.txt'结尾的文件
[root@#localhost test]# rm -f /test/*.txt

4:touch命令可以更改文件的属性信息
   -a参数:只修改读取时间
   -m参数:只修改修改时间
   -d参数:同时修改上面的两个时间
首先,创建一个文件/test/abs.txt
[root@#localhost test]# touch /test/abs.txt
[root@#localhost test]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 24 15:23 abs.txt
然后写入一些内容(echo命令后面会有讲解)
[root@#localhost test]# echo "123" > ./abs.txt
[root@#localhost test]# ll
total 4
-rw-r--r--. 1 root root 4 Nov 24 15:25 abs.txt
这时候可以看到文件的最后修改时间变成了15:25,我们可以通过touch命令将这个时间修改回原来的15:23
[root@#localhost test]# touch -d "2018-11-24 15:23:01" ./abs.txt 
[root@#localhost test]# ll
total 4
-rw-r--r--. 1 root root 4 Nov 24 15:23 abs.txt
  • mv命令
    重命名,移动文件、目录
格式:mv [选项] 源文件 [目标路径|目标文件名]
1:重命名一个文件,将/test/aaa.txt重命名为/test/bbb.txt
[root@#localhost test]# ls
aaa.txt
[root@#localhost test]# mv /test/aaa.txt /test/bbb.txt
[root@#localhost test]# ls
bbb.txt
2: 将/test/bbb.txt移动到/usr/下
[root@#localhost test]# mv ./bbb.txt /usr
[root@#localhost test]# ls
[root@#localhost test]# ls /usr/
bbb.txt
3:将/usr下的/bbb.txt移动到/test下且重命名为ccc.txt
[root@#localhost test]# mv /usr/bbb.txt /test/ccc.txt
[root@#localhost test]# ls /usr/bbb.txt
ls: cannot access /usr/bbb.txt: No such file or directory
[root@#localhost test]# ls /test/ccc.txt 
/test/ccc.txt
4:mv对目录的操作同上
  • cp命令
    拷贝文件
格式:cp [选项] 源文件 目标文件
1:cp命令不同于mv命令,执行之后,原文件依旧存在。
2:常用选项:-p 拷贝是保留原文件属性;-a 是其他三个参数的集合(-p;-d;-r. -d表示若对象为“链接文件”,则保留该“链接文件”的属性,-r表示递归)
-i 加入这个参数,在拷贝是目标路径存在同名文件将询问是否覆盖
3:示例
[root@server01 test]# mkdir test1
[root@server01 test]# touch test1/a.txt
[root@server01 test]# touch ./a.txt
[root@server01 test]# cp -i a.txt test1/a.txt 
cp:是否覆盖"test1/a.txt"? y
  • file命令
    命令用于查看文件的类型
格式:file [文件名]
1:linux系统中的文件类型
我们通过ll命令来查看文件的详细信息,列表中开头第一个字母就表明了该文件的文件类型
[root@server01 ~]# ll /dev/*
crw-rw----  1 root video    10, 175 11月 24 18:00 /dev/agpgart
lrwxrwxrwx  1 root root           3 11月 24 18:00 /dev/cdrom -> sr0
brw-rw----  1 root disk      8,   2 11月 24 18:00 /dev/sda2
drwxr-xr-x 4 root root 80 11月 24 18:00 usb
  常见的文件类型有:
  d 目录文件
  - 普通文件
  c 字符设备
  b 块设备
  s 套接口文件
  p 管道
  l 链接文件
2:file命令查看文件类型展示
[root@server01 test]# ll
总用量 0
-rw-r--r-- 1 root root  0 11月 24 20:16 a.txt
lrwxrwxrwx 1 root root 11 11月 24 20:31 b -> test1/a.txt
drwxr-xr-x 2 root root 19 11月 24 20:16 test1

[root@server01 test]# file test1/
test1/: directory
[root@server01 test]# file b 
b: symbolic link to `test1/a.txt'
  • cat命令
    查看文本文件内容(内容较少时)
格式:cat [选项] [文件]
[root@server01 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

加 -n 参数可以显示行号
[root@server01 ~]# 
[root@server01 ~]# cat /etc/passwd -n
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  • more命令
    查看文本文件内容(内容较多时)
[root@server01 ~]# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
gluster:x:996:993:GlusterFS daemons:/var/run/gluster:/sbin/nologin
saslauth:x:995:76:Saslauthd user:/run/saslauthd:/sbin/nologin
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
abrt:x:173:173::/etc/abrt:/sbin/nologin
setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
unbound:x:993:988:Unbound DNS resolver:/etc/unbound:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin
--More--(80%)

内容较多,可以使用回车翻页
  • less命令
    以较少的输出查看文本,可以翻页查看,用法类似more
    退出浏览状态使用q键
  • head命令
    指定查看文件前几行内容
格式:head [选项] [文件]
1:查看/etc/passwd 的前5行 (-n 可以省略不写,写成 head -5 就可以)
[root@server01 ~]# head -n 5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

2:默认查看的时前10行
[root@server01 ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
  • tail命令
    查看文件后几行内容或者查看持续刷新的文件内容(如不断写入中的日志)
格式:tail [选项] [文件]
1:查看/etc/passwd 后5行内容(同样 -n 可以不写 直接写 -5 就行)
[root@server01 test]# tail -n 5 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
lzp:x:1000:1000:lzp:/home/lzp:/bin/bash
2:查看持续刷新的文件内容
写一个脚本,不停地往/test/aaa.txt 文件中写入hello world
[root@server01 ~]# vi /test/test.sh 
#!/bin/bash
while true
do
        echo 'hello world' >> /test/aaa.txt
        sleep 5
done
然后执行这个脚本,用tail命令去查看aaa.txt文件
[root@server01 test]# sh /test/test.sh
[root@server01 ~]# tail -f /test/aaa.txt     # tail -f 也可以写作 tailf
hello world
hello world
hello world
hello world
...
按键盘上的ctrl+c即可退出持续刷新状态
  • wc命令
    用于统计文本的行数,字数,字节等
格式:wc [参数] 文本
常见参数: -l 统计行数;-c 统计字节;-w 统计单词数
[root@server01 ~]# wc -l /etc/passwd     #这里统计了行数,同时也获得了当前系统的用户数量
44 /etc/passwd
[root@server01 ~]# wc -w /etc/passwd
88 /etc/passwd
[root@server01 ~]# wc -c /etc/passwd
2292 /etc/passwd
  • stat命令
    上面我们说过touch命令可以修改文件的部分属性信息(时间),那么现在我们用stat命令来查看文件的属性信息
格式:stat 文件名
[root@VM_0_7_centos /]# stat /bin
  File: ‘/bin’ -> ‘usr/bin’
  Size: 7           Blocks: 0          IO Block: 4096   symbolic link
Device: fd01h/64769d    Inode: 17          Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-03-15 22:13:51.377000000 +0800
Modify: 2016-04-21 15:00:53.184000000 +0800
Change: 2016-04-21 15:00:53.184000000 +0800
 Birth: -
可以看出该命令详细展示了文件的类型、大小、IO、inode号、权限信息、访问时间、修改时间、创建时间等信息。
  • dd命令
    用于按照指定大小和个数的数据块来复制文件或转换文件
格式: dd [参数]
1:常用参数有:if 指定输入文件的来源;of指定目标文件;bs指定块的大小;count 指定块写入的次数
2:其他:在linux系统中有一个名为/dev/zero的文件,他不占用存储空间,却能提供无穷的数据,这里我们可以将其作为dd命令的输入文件来源
3:用dd命令在/test目录中生成一个大小为10M的文件
[root@server01 test]# pwd
/test
[root@server01 test]# ls
[root@server01 test]# dd if=/dev/zero of=/test/storige count=10 bs=1M
记录了10+0 的读入
记录了10+0 的写出
10485760字节(10 MB)已复制,0.0268275 秒,391 MB/秒
[root@server01 test]# ll -h
总用量 10M
-rw-r--r-- 1 root root 10M 11月 24 21:52 storige
4:我们还可以使用dd命令来生成光盘镜像,且生成的镜像可以直接挂载使用(请看下一个命令)。
[root@server01 test]# dd if=/dev/sr0 of=CentOS7_Linux.iso
记录了8730624+0 的读入
记录了8730624+0 的写出
4470079488字节(4.5 GB)已复制,199.974 秒,22.4 MB/秒
[root@server01 test]# ll -h
总用量 4.2G
-rw-r--r-- 1 root root 4.2G 11月 24 22:00 CentOS7_Linux.iso
-rw-r--r-- 1 root root  10M 11月 24 21:52 storige
  • mount挂载与umount解挂
mount挂载命令
格式:mount 文件系统 [参数] 源文件 挂载目录
参数:-t 指定挂载的文件类型;-a 挂载所有在/etc/fstab 中定义的文件系统;-o 指定挂载类型(如只读[ro],读写[rw],同步[sync],异步等方式[async])。
1:将上面使用dd命令生成的.iso文件挂载到根目录下名为dvd1的目录下
[root@server01 test]# mkdir /dvd1
[root@server01 test]# mount /test/CentOS7_Linux.iso /dvd1/
mount: /dev/loop0 写保护,将以只读方式挂载
[root@server01 test]# cd /dvd1/
[root@server01 dvd1]# ls
CentOS_BuildTag  EFI  EULA  GPL  images  isolinux  LiveOS  Packages  repodata  RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Testing-7  TRANS.TBL
2: 使用mount直接回车可以查看当前系统所有已经挂载的设备
[root@server01 /]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=1515292k,nr_inodes=378823,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
...
3:将/dev/sr0(系统光盘镜像,文件系统类型为iso9660)挂载到/mnt目录下
[root@server01 /]# mount -t iso9660 -o ro /dev/sr0 /mnt/
[root@server01 /]# cd /mnt/
[root@server01 mnt]# ll
总用量 678
-rw-rw-r-- 1 root root     14 5月   2 2018 CentOS_BuildTag
drwxr-xr-x 3 root root   2048 5月   4 2018 EFI
-rw-rw-r-- 1 root root    227 8月  30 2017 EULA
-rw-rw-r-- 1 root root  18009 12月 10 2015 GPL
drwxr-xr-x 3 root root   2048 5月   4 2018 images
drwxr-xr-x 2 root root   2048 5月   4 2018 isolinux
...

umount解挂命令
格式:umount [挂载点/设备文件]
umount 后面既可以是挂载点也可以是设备路径
[root@server01 ~]# umount /mnt
[root@server01 ~]# umount /dev/sr0 
  • du命令
du命令可以查看文件占用的空间大小:
[root@server04 ~]# du -sh /mnt/
4.2G    /mnt/

相关文章

网友评论

      本文标题:linux文件操作命令

      本文链接:https://www.haomeiwen.com/subject/iuzeqqtx.html