Linux操作系统基础命令使⽤-文件与目录管理(学习笔记)

  • 1.文件创建

  • 1.touch
    
     [tom@centos7 ~]$ pwd
     /home/tom
     [tom@centos7 ~]$ cd Desktop/
     [tom@centos7 Desktop]$ pwd
     /home/tom/Desktop
     [tom@centos7 Desktop]$ ls
     456.txt
     [tom@centos7 Desktop]$ touch  aa
     [tom@centos7 Desktop]$ ls
     456.txt  aa
     [tom@centos7 Desktop]$ touch  bb cc
     [tom@centos7 Desktop]$ ls
     456.txt  aa  bb  cc
    
    linux - 通配符基础
    * 匹配任意
    {} 匹配指定内容
    {1..6} 1 2 3 4 5 6
    
     [tom@centos7 Desktop]$ touch  aa{1..4}
     [tom@centos7 Desktop]$ ls
     456.txt  aa  aa1  aa2  aa3  aa4  bb  cc
     [tom@centos7 Desktop]$ touch  fa*
     [tom@centos7 Desktop]$ 
    [tom@centos7 Desktop]$ ls
     456.txt  aa  aa1  aa2  aa3  aa4  bb  cc  fa*
    

    2.文件删除

  • remove
  • rm
  •  -i 删除钱询问
[tom@centos7 Desktop]$  rm bb
 [tom@centos7 Desktop]$ ls
 456.txt  aa  aa1  aa2  aa3  aa4  cc  fa*
 [tom@centos7 Desktop]$ rm -i  cc
 rm: remove regular empty file ‘cc’? n
 [tom@centos7 Desktop]$ ls
 456.txt  aa  aa1  aa2  aa3  aa4  cc  fa*
 [tom@centos7 Desktop]$ rm -i cc
 rm: remove regular empty file ‘cc’? y
 [tom@centos7 Desktop]$ ls
 456.txt  aa  aa1  aa2  aa3  aa4  fa*

 -r 删除目录/文件

[tom@centos7 ~]$ ls
 123.txt  core.2066  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  
Videos
 [tom@centos7 ~]$ rm Templates
 rm: cannot remove ‘Templates’: Is a directory
 [tom@centos7 ~]$ rm -r Templates
 [tom@centos7 ~]$ ls
 123.txt  core.2066  Desktop  Documents  Downloads  Music  Pictures  Public  Videos
 [tom@centos7 Desktop]$ pwd
 /home/tom/Desktop
 [tom@centos7 Desktop]$ ls
 456.txt  aa  aa1  aa2  aa3  aa4  fa*
 [tom@centos7 Desktop]$ touch  bb cc dd ee
 [tom@centos7 Desktop]$ rm -r  bb
 [tom@centos7 Desktop]$ l
 bash: l: command not found...
 [tom@centos7 Desktop]$ ls
 456.txt  aa  aa1  aa2  aa3  aa4  cc  dd  ee  fa*

 -f 强制删除 (告警提示-直接忽略)

[tom@centos7 ~]$ ls
 123.txt  core.2066  Desktop  Documents  Downloads  Music  Pictures  Public  Videos
 [tom@centos7 ~]$ rm -f Videos
 rm: cannot remove ‘Videos’: Is a directory
 [tom@centos7 ~]$ rm -rf Videos
 [tom@centos7 ~]$ ls
123.txt  core.2066  Desktop  Documents  Downloads  Music  Pictures  Public
 rm -rf 
[tom@centos7 Desktop]$ rm -rf  cc dd ee
 [tom@centos7 Desktop]$ ls
 456.txt  aa  aa1  aa2  aa3  aa4  fa*
 [tom@centos7 Desktop]$ rm -rf  aa{1..4}
 [tom@centos7 Desktop]$ ls
 456.txt  aa  fa*
 [tom@centos7 Desktop]$ touch  aa{1..5}
 [tom@centos7 Desktop]$ ls
 456.txt  aa  aa1  aa2  aa3  aa4  aa5  fa*
 [tom@centos7 Desktop]$ rm -rf aa*
 [tom@centos7 Desktop]$ ls
 456.txt  fa*
 [tom@centos7 Desktop]$ rm -rf *
 [tom@centos7 Desktop]$ ls
 [tom@centos7 Desktop]$ touch aa01
 [tom@centos7 Desktop]$ ls
 aa01
 [tom@centos7 Desktop]$ touch ../aa02
 [tom@centos7 Desktop]$ ls
 aa01
 [tom@centos7 Desktop]$ ls ..
 123.txt  aa02  core.2066  Desktop  Documents  Downloads  Music  Pictures  Public
 [tom@centos7 Desktop]$ rm -rf  ../aa02 
[tom@centos7 Desktop]$ ls ..
 123.txt  core.2066  Desktop  Documents  Downloads  Music  Pictures  Public
 [tom@centos7 Desktop]$ 
[tom@centos7 Desktop]$ touch /aa03
 touch: cannot touch ‘/aa03’: Permission denied
 [tom@centos7 Desktop]$ su 
Password: 
su: Authentication failure
 [tom@centos7 Desktop]$ su 
Password: 
[root@centos7 Desktop]# touch /aa03
 [root@centos7 Desktop]# ls /
 aa03  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  
srv  sys  tmp  usr  var
 [root@centos7 Desktop]# rm -rf /aa03
 [root@centos7 Desktop]# rm -rf /*  #慎⽤

3.文件复制

copy

cp

[tom@centos7 Desktop]$ cp /etc/hosts  ./
 [tom@centos7 Desktop]$ ls
 aa01  hosts
 [tom@centos7 Desktop]$ ls -lh
 total 4.0K
-rw-rw-r--. 1 tom tom   0 Jan 14 21:03 aa01
-rw-r--r--. 1 tom tom 158 Jan 14 21:16 hosts

-i 询问是否覆盖

[tom@centos7 Desktop]$ cp /etc/hosts  ./
 [tom@centos7 Desktop]$ ls
 aa01  hosts
 [tom@centos7 Desktop]$ cp -i /etc/hosts ./
 cp: overwrite ‘./hosts’? y

-a 复制目录,保留原始文件属性信息

[tom@centos7 Desktop]$ ls -lh
 total 4.0K
-rw-rw-r--. 1 tom tom 0 Jan 14 21:03 aa01
-rw-r--r--. 1 tom tom 158 Jan 14 21:18 hosts
 [tom@centos7 Desktop]$ cp -ia  /etc/hosts ./
 cp: overwrite ‘./hosts’? y
 [tom@centos7 Desktop]$ ls -lh
 total 4.0K
-rw-rw-r--. 1 tom tom   0 Jan 14 21:03 aa01
-rw-r--r--. 1 tom tom 158 Jun  7  2013 hosts

4. 文件 剪切/移动/重命名

remove - rm

move - mv

 [tom@centos7 Desktop]$ mv  hosts  ../
 [tom@centos7 Desktop]$ ls
aa01
 [tom@centos7 Desktop]$ ls ..
 123.txt  core.2066  Desktop  Documents  Downloads  hosts  Music  Pictures  Public
 [tom@centos7 Desktop]$ mv  ../hosts  ./
 [tom@centos7 Desktop]$ ls
 aa01  hosts
 [tom@centos7 Desktop]$ 
[tom@centos7 Desktop]$ mv hosts host123
 [tom@centos7 Desktop]$ ls
 aa01  host123
 [tom@centos7 Desktop]$ mv host123  ../hosts
 [tom@centos7 Desktop]$ ls ..
 123.txt  core.2066  Desktop  Documents  Downloads  hosts  Music  Pictures  Public

5. 文件查看

一次性显示完整行 - 适合查看小文件 - 较短文件

cat

tac

 [tom@centos7 ~]$ cat hosts
 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 [tom@centos7 ~]$ 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
 [tom@centos7 ~]$  tac /etc/passwd
 tom:x:1000:1000:tom:/home/tom:/bin/bash
 tcpdump:x:72:72::/:/sbin/nologin
 ntp:x:38:38::/etc/ntp:/sbin/nologin
 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
显示部分⾏ - 默认看10⾏ - 指定⾏数
head
 tail 
[tom@centos7 ~]$ 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
 [tom@centos7 ~]$ 
[tom@centos7 ~]$ tail /etc/passwd
 setroubleshoot:x:992:989::/var/lib/setroubleshoot:/sbin/nologin
 pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
 gdm:x:42:42::/var/lib/gdm:/sbin/nologin
 gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin
 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
 ntp:x:38:38::/etc/ntp:/sbin/nologin
 tcpdump:x:72:72::/:/sbin/nologin
 tom:x:1000:1000:tom:/home/tom:/bin/bash
 [tom@centos7 ~]$ 
[tom@centos7 ~]$ 
[tom@centos7 ~]$ head /etc/passwd -n 5
 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
 [tom@centos7 ~]$ 
[tom@centos7 ~]$ tail /etc/passwd -n  5
 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
 ntp:x:38:38::/etc/ntp:/sbin/nologin
 tcpdump:x:72:72::/:/sbin/nologin
 tom:x:1000:1000:tom:/home/tom:/bin/bash

翻阅查看 - 适合大文件-较长文件

more

less

 [tom@centos7 ~]$ more /etc/passwd
 [tom@centos7 ~]$ less /etc/passwd  上下翻阅
[tom@centos7 ~]$ more -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
查看统计
wc-c 统计字节数-l 统计⾏数-w 统计单词数-L 统计最⻓⾏-字节数⻓度
[tom@centos7 ~]$ cat hosts
 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
 [tom@centos7 ~]$ wc -l  hosts
 2 hosts
 [tom@centos7 ~]$ wc -c  hosts 
158 hosts
 [tom@centos7 ~]$ wc -w hosts
 10 hosts
 [tom@centos7 ~]$ wc -L  hosts
 78 hosts

6.目录管理

mkdir

rmdir

 #普通创建/删除
[tom@centos7 Desktop]$ mkdir  test01 
[tom@centos7 Desktop]$ ls
 aa01  test01
 [tom@centos7 Desktop]$ rmdir test01
 [tom@centos7 Desktop]$ ls
 aa01
 #递归创建
[tom@centos7 Desktop]$ mkdir a01/a02
 mkdir: cannot create directory ‘a01/a02’: No such file or directory
 [tom@centos7 Desktop]$ mkdir -p a01/a02 
[tom@centos7 Desktop]$ ls a01/
 a02
 #递归删除
[tom@centos7 Desktop]$ ls a01
 a02
 [tom@centos7 Desktop]$ 
[tom@centos7 Desktop]$ rmdir a01/a02
 [tom@centos7 Desktop]$ ls
 a01  aa01
 [tom@centos7 Desktop]$ ls a01/
[tom@centos7 Desktop]$ rmdir -p  a01/a02
 [tom@centos7 Desktop]$ ls
 aa01
 #⽬录详细信息查看
[tom@centos7 Desktop]$ mkdir a01 
[tom@centos7 Desktop]$ cd a01
 [tom@centos7 a01]$ touch  aa{1..9}
 [tom@centos7 a01]$ ls
 aa1  aa2  aa3  aa4  aa5  aa6  aa7  aa8  aa9
 [tom@centos7 a01]$ 
[tom@centos7 a01]$ cd ..
 [tom@centos7 Desktop]$ pwd
 /home/tom/Desktop
 [tom@centos7 Desktop]$ ls
 a01  aa01
 [tom@centos7 Desktop]$ ls -lh  a01
 total 0-rw-rw-r--. 1 tom tom 0 Jan 14 22:23 aa1

7.获取帮助强化

获取帮助不⽌于此
ls (1p)              
whereis 定位命令⼆进制源码位置 / 该命令man ⼿册位置
[tom@centos7 Desktop]$ whereis ls
 ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
 [tom@centos7 Desktop]$ 
whatis 查找man⼿册相关简要说明 以及命令man 类型
[tom@centos7 Desktop]$ whatis ls
 ls (1)               - list directory contents- list directory contents
 1 普通⽤户可以执⾏命令/⽂件
3 函数库 c
 7 标准协议. ip tcp
 8 管理员可以使⽤的命令
9 内核相关⽂件
which 查看命令⼆进制源代码位置 , 可以查看当前命令alias别名
[tom@centos7 Desktop]$ 
[tom@centos7 Desktop]$ which ls
 alias ls='ls --color=auto'
        /usr/bin/ls

8. find文件查找

find [⽂件路径] [⽂件查找参数]
 [tom@centos7 ~]$ touch 123.txt
 [tom@centos7 ~]$ touch 456.txt
 [tom@centos7 ~]$ touch 123.mp3
 [tom@centos7 ~]$ su
 Password: 
[root@centos7 tom]# touch  1234.txt
 [root@centos7 tom]# exit
 #******************************************************************************-name 
[tom@centos7 ~]$ find  .  -name 123.txt #在当前路径下 ,查找123.txt⽂件
./123.txt
 [tom@centos7 ~]$ find  . -name  "*.txt" #在当前路径下 ,查找txt相关⽂件
[tom@centos7 ~]$ find . -name "123.*" #在当前路径下 ,查找123相关⽂件
./123.txt
 ./123.mp3
 [root@centos7 tom]# find  /  -name "*.mp3"
 find: ‘/run/user/1000/gvfs’: Permission denied
 /home/tom/123.mp3
 [root@centos7 tom]# umount /run/user/1000/gvfs 
[root@centos7 tom]# rm -rf /run/user/1000/gvfs 
[root@centos7 tom]# find  /  -name "*.mp3"     
/home/tom/123.mp3
[root@centos7 tom]# find /home/   -name "*.txt"
 #******************************************************************************-user
 [root@centos7 tom]# find  . -user tom # 在当前路径下查找⽤户为tom⽂件
[root@centos7 tom]# find .  -user  tom   -name "*.txt"
 [root@centos7 tom]# find  .  -user  root
 ./345.text
 ./1234.txt-group
 [root@centos7 tom]# find . -group root -name  "*.txt"
 ./1234.txt-nouser/-nogroup
 [root@centos7 tom]# find .  -nouser
 [root@centos7 tom]# find .  -nogroup
 #******************************************************************************-time
 [root@centos7 tom]# stat 123.txt
  File: ‘123.txt’
  Size: 0
               Blocks: -mtime -5 / +5 -ctime -5 -atime -5 
0
          IO Block: 4096
   regular empty file
 Device: 803h/2051d      Inode: 100852257   Links: 1
 Access: (0664/-rw-rw-r--)  Uid: ( 1000/     tom)   Gid: ( 1000/     tom)
 Context: unconfined_u:object_r:user_home_t:s0
 Access: 2026-05-31 08:49:57.933782202 +0800 最后⼀次更改后的⾸次访问时间
Modify: 2026-05-31 08:49:57.933782202 +0800 最后⼀次对⽂件内容修改时间
Change: 2026-05-31 08:49:57.933782202 +0800 最后⼀次对⽂件属性进⾏修改时间
[root@centos7 tom]# find . -atime -1 #查找⼀天内访问过的⽂件
[root@centos7 tom]# find . -ctime -1 #查找⼀天内进⾏过属性修改的⽂件
[root@centos7 tom]# find . -mtime -1 #查找在⼀天之内进⾏内容修改过的⽂件 
3天之前修改过内容,2天之内修改过属性,1天之内被访问的  txt 相关的⽂件
[root@centos7 tom]# find .  -mtime +3   -ctime -2  -atime -1  -name "*.txt" 
#******************************************************************************-type ⽂件类型
[root@centos7 tom]# find . -type f  (普通⽂件)
[root@centos7 tom]# find /dev/ -type b(块设备)
 /dev/sda3
 /dev/sda2
 /dev/sda1
 /dev/sr0
 /dev/sda
 [root@centos7 tom]# find /bin/ -type l (链接)
 #******************************************************************************-size ⽂件⼤⼩
+50k 50K以上的⽂件-50k  50K以内的⽂件-50M  50K以内的⽂件
50   = 50K的⽂件
 [root@centos7 tom]# find /  -size  +50k
 [root@centos7 tom]# find /  -size  +50M

9. locate文件查找

locate与find不同:find是去硬盘找文件,locate只在本地locate数据库中查找文件索引.
本地locate数据库需要手动更新 - updatedb
[root@centos7 tom]# locate  123.txt
 /home/tom/123.txt
 [root@centos7 tom]# locate -c  123.txt # 查找123.txt⽂件数量
1 
[root@centos7 tom]# locate  "*.txt" # 可以使⽤通配符⽅式模糊查找
[root@centos7 tom]# locate  "*.txt" -c
 818
 [root@centos7 tom]# locate -n 10 "*.txt" #通配符⽅式模糊查找 | 显示前10个⽂件
/etc/brltty/brl-ba-all.txt
 /etc/brltty/brl-bd-all.txt
 /etc/brltty/brl-bl-18.txt
 /etc/brltty/brl-bl-40_m20_m40.txt
 /etc/brltty/brl-ec-all.txt
 /etc/brltty/brl-ec-spanish.txt
/etc/brltty/brl-eu-all.txt
 /etc/brltty/brl-lb-all.txt
 /etc/brltty/brl-lt-all.txt
 /etc/brltty/brl-mb-all.txt
 [root@centos7 tom]# locate -i 123.TXT #查找时忽略掉⼤⼩写
/home/tom/123.txt
 [root@centos7 tom]# touch  312.txt
 [root@centos7 tom]# locate 312.txt
 # locate 并不是真正的去硬盘中查找所有⽂件的⽂件名 , ⽽是去locate本地数据库中进⾏查找⽂件名信息
[root@centos7 tom]# locate -S #查看Locate 数据库
Database /var/lib/mlocate/mlocate.db:
 11,020 directories
 142,904 files # 数据库中记录的⽂件数量
7,060,384 bytes in file names
 3,138,968 bytes used to store database
 # 在locate查询之前,建议使⽤⼿动更新数据库⽅式进⾏更新
[root@centos7 tom]# updatedb #⼿动更新数据库
[root@centos7 tom]# locate -S
 Database /var/lib/mlocate/mlocate.db:
 11,020 directories
 142,905 files # 数据库中记录的⽂件数量
7,060,401 bytes in file names
 3,138,977 bytes used to store database
 [root@centos7 tom]# locate  312.txt
 /home/tom/312.txt
 # locate 指定路径进⾏查找 | 建议使⽤绝对路径
[root@centos7 tom]# locate "./*.txt"
 [root@centos7 tom]# 
[root@centos7 tom]# 
[root@centos7 tom]# pwd
 /home/tom
 [root@centos7 tom]# 
[root@centos7 tom]# locate "/home/tom/*.txt"
 /home/tom/.456.txt
 /home/tom/123.txt
 /home/tom/1234.txt
 /home/tom/312.txt
 /home/tom/456.txt
 /home/tom/.cache/tracker/db-locale.txt
 /home/tom/.cache/tracker/db-version.txt
/home/tom/.cache/tracker/first-index.txt
 /home/tom/.cache/tracker/last-crawl.txt
 /home/tom/.cache/tracker/locale-for-miner-apps.txt
 /home/tom/.cache/tracker/locale-for-miner-user-guides.txt
 /home/tom/.cache/tracker/parser-sha1.txt
 [root@centos7 tom]# 
[root@centos7 tom]# locate "/home/tom/123.txt"
 /home/tom/123.txt

10.grep文件内容查找/过滤

/etc/passwd #本地⽤户信息⽂件
[root@centos7 tom]# more  /etc/passwd
 [root@centos7 tom]# grep  "tom"  /etc/passwd  # 在/etc/passwd⽂件中只查找和tom相关的信息
tom:x:1000:1000:tom:/home/tom:/bin/bash
 [root@centos7 tom]# grep -i "Tom" /etc/passwd # 在/etc/passwd⽂件中只查找和tom相关的信息/忽
略⼤⼩写
tom:x:1000:1000:tom:/home/tom:/bin/bash
 [root@centos7 tom]# grep "Tom" /etc/passwd   
[root@centos7 tom]# 
[root@centos7 tom]# grep  "tom"   -n   /etc/passwd # 显示⾏数
41:tom:x:1000:1000:tom:/home/tom:/bin/bash
 [root@centos7 tom]# grep  -v "tom"  /etc/passwd   # 反选
[root@centos7 tom]# grep -v  -i "tom" -n /etc/passwd
 #
 [root@centos7 tom]# grep  -c "tom" /etc/passwd #tom关键字出现⾏数
1-c, --count
               print only a count of matching lines per FILE
注: 在⼀⾏匹配多个关键字时 -c 参数表达为显示的⾏数
#在⼀⾏匹配⼀个关键字时 -c 参数表达为显示的次数

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值