再次学习:文件处理命令
1.目录显示命令ls
命令名称:ls
命令英文原意:list
命令所在路径:/bin/ls
执行权限:所有用户
功能描述:显示目录文件
语法:
ls [-a -l -d] [文件或目录]- 选项a:显示所有文件(包括隐藏文件)
- 选项l:long,长信息显示,即详细信息显示
- 选项d:查看目录属性
xiaoyu@xiaoyu-VMware-Virtual-Platform:~$ ls Desktop Documents Downloads Music Pictures Public snap Templates Videosxiaoyu@xiaoyu-VMware-Virtual-Platform:~$ ls -a . .bash_history .bashrc .config Documents .local .pam_environment .profile .python_history .ssh Templates .Xauthority .. .bash_logout .cache Desktop Downloads Music Pictures Public snap .sudo_as_admin_successful Videos注:以点号开头的文件是隐藏文件
隐藏文件的作用:
- 保护重要配置文件:防止用户误删或误改系统关键配置,避免日常文件操作中不小心破坏系统设置
- 保持目录整洁:将配置、缓存、日志等辅助文件隐藏起来,让用户更专注于自己的工作文件
常见隐藏文件类型:
| 类型 | 示例 | 作用 |
| ----- | ---------------------- | --------- |
| 用户配置 | .bashrc, .zshrc | Shell环境配置 |
| 应用配置 | .gitconfig, .vimrc | 应用程序设置 |
| 缓存数据 | .cache/ | 程序缓存文件 |
| 历史记录 | .bash_history | 命令历史记录 |
| SSH密钥 | .ssh/ | 安全密钥存储 |
如何查看隐藏文件:
# 列出所有文件(包括隐藏文件) ls -a # 详细列出隐藏文件 ls -la # 只显示隐藏文件 ls -d .*如何创建隐藏文件:
# 创建隐藏文件 touch .myconfig # 创建隐藏目录 mkdir .mydataxiaoyu@xiaoyu-VMware-Virtual-Platform:~$ ls -l total 36 drwxr-xr-x 2 xiaoyu xiaoyu 4096 Mar 21 19:57 Desktop drwxr-xr-x 2 xiaoyu xiaoyu 4096 Dec 31 16:32 Documents drwxr-xr-x 2 xiaoyu xiaoyu 4096 Dec 31 16:32 Downloads drwxr-xr-x 2 xiaoyu xiaoyu 4096 Dec 31 16:32 Music drwxr-xr-x 2 xiaoyu xiaoyu 4096 Dec 31 16:32 Pictures drwxr-xr-x 2 xiaoyu xiaoyu 4096 Dec 31 16:32 Public drwx------ 5 xiaoyu xiaoyu 4096 Dec 31 18:00 snap drwxr-xr-x 2 xiaoyu xiaoyu 4096 Dec 31 16:32 Templates drwxr-xr-x 2 xiaoyu xiaoyu 4096 Dec 31 16:32 Videos2.目录创建命令mkdir
命令英文原意:make directories(directory:目录、名录)
命令所在路径:/bin/mkdir
功能:创建新目录,如果目录名前加上选项-p,可以递归创建目录
语法:
mkdir [-p] 目录名xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ mkdir /tmp/studyhard xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ cd /tmp xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ ls snap-private-tmp systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-switcheroo-control.service-twtqkf studyhard systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-logind.service-OhpCbt systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-colord.service-OoxGbE systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-oomd.service-Egkx4e systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-fwupd.service-WMrE8P systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-resolved.service-n0g8XI systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-ModemManager.service-UR8NZb systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-timesyncd.service-9sZbFt systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-polkit.service-rs66Mk systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-upower.service-QMnLoJ systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-power-profiles-daemon.service-r0PtHm vmware-root_643-3979708515 xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ ls -l total 56 drwx------ 5 root root 4096 Mar 18 17:18 snap-private-tmp drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 10:44 studyhard drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-colord.service-OoxGbE drwx------ 3 root root 4096 Mar 15 21:15 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-fwupd.service-WMrE8P drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-ModemManager.service-UR8NZb drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-polkit.service-rs66Mk drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-power-profiles-daemon.service-r0PtHm drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-switcheroo-control.service-twtqkf drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-logind.service-OhpCbt drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-oomd.service-Egkx4e drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-resolved.service-n0g8XI drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-timesyncd.service-9sZbFt drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-upower.service-QMnLoJ drwx------ 2 root root 4096 Mar 15 21:02 vmware-root_643-3979708515注:/tmp是Linux中的一个特殊且重要的临时目录
作用:
- 存放临时文件:应用程序运行过程中产生的临时数据、程序崩溃或正常退出后应被清理的文件、编译过程中的中间文件(如.o对象文件)、下载\解压的临时缓存
- 多用户共享:所有用户都有读写权限,是进程间交换数据的便利位置
- 系统重启清理:传统上,系统重启时/tmp中的东西会清空,现代系统通常使用
tmpfs(内存文件系统),断电即失
xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ mkdir /tmp/study/hard mkdir: cannot create directory ‘/tmp/study/hard’: No such file or directory这里在一个没有不存在的目录下创建子目录,自然会报错
可以使用选项-p递归创建目录
xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ mkdir -p /tmp/study/hardxiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ ls -l /tmp total 60 drwx------ 5 root root 4096 Mar 18 17:18 snap-private-tmp drwxrwxr-x 3 xiaoyu xiaoyu 4096 Mar 22 10:54 study drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 10:44 studyhard drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-colord.service-OoxGbE drwx------ 3 root root 4096 Mar 15 21:15 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-fwupd.service-WMrE8P drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-ModemManager.service-UR8NZb drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-polkit.service-rs66Mk drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-power-profiles-daemon.service-r0PtHm drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-switcheroo-control.service-twtqkf drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-logind.service-OhpCbt drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-oomd.service-Egkx4e drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-resolved.service-n0g8XI drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-timesyncd.service-9sZbFt drwx------ 3 root root 4096 Mar 15 21:02 systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-upower.service-QMnLoJ drwx------ 2 root root 4096 Mar 15 21:02 vmware-root_643-3979708515可以看到study目录的权限字符第1位是d,说明study确实是一个目录
xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ ls -l /tmp/study total 4 drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 10:54 hard查看study目录下确实有一个hard目录,4096表示hard目录占用的磁盘空间为4096字节
ls -l显示的 4096 是目录文件本身的大小,不是目录内所有文件的总大小!要查看目录总占用,需用du -sh
xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ mkdir -p /tmp/study/happy /tmp/study/persist xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ ls -l /tmp/study total 12 drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 11:03 happy drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 10:54 hard drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 11:03 persist如上所以,可以一行mkdir命令创建多个目录
3.切换目录命令cd
change directory
语法:
cd [目录名]4.显示当前所在目录命令pwd
print working directory
语法:直接输入pwd
xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ cd studyhard xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/studyhard$ pwd /tmp/studyhard xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/studyhard$ cd . xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/studyhard$ cd .. xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ cd /study -bash: cd: /study: No such file or directory xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ cd study xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/study$ pwd /tmp/study xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/study$ ls happy hard persist xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/study$ cd happy xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/study/happy$ pwd /tmp/study/happy xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/study/happy$ ls xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/study/happy$ cd .. xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp/study$ cd .. xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ cd.. cd..: command not found xiaoyu@xiaoyu-VMware-Virtual-Platform:/tmp$ cd .. xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ pwd /其中.表示当前目录,..表示上一级目录
5.删除空目录命令rmdir
remove empty directory
语法:
rmdir [目录名]xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ rmdir /tmp/studyhardxiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls /tmp snap-private-tmp systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-switcheroo-control.service-twtqkf study systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-logind.service-OhpCbt systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-colord.service-OoxGbE systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-oomd.service-Egkx4e systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-fwupd.service-WMrE8P systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-resolved.service-n0g8XI systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-ModemManager.service-UR8NZb systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-systemd-timesyncd.service-9sZbFt systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-polkit.service-rs66Mk systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-upower.service-QMnLoJ systemd-private-767a44d5b70b41e4bfc9c555d2f0ccf0-power-profiles-daemon.service-r0PtHm vmware-root_643-3979708515可以看到,/tmp目录下确实没有studyhard这个目录了
xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ rmdir /tmp rmdir: failed to remove '/tmp': Permission denied如上,非空目录就删不掉了
6.文件复制命令cp
copy
语法:
cp [选项] 源文件 目标文件 cp [选项] 源文件... 目标目录选项:
- -r:递归复制目录(必须)
- -p:保留文件属性(权限、时间等)
- -v:显示复制过程
其他选项有需要再自行学习
先使用touch命令创建一个文件:
xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ touch /tmp/study/file.txt xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls -l /tmp/study total 12 -rw-rw-r-- 1 xiaoyu xiaoyu 0 Mar 22 11:32 file.txt drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 11:03 happy drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 10:54 hard drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 11:03 persistxiaoyu@xiaoyu-VMware-Virtual-Platform:/$ pwd / xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ cp /tmp/study/file.txt /tmp/study/happy xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls /tmp/study/happy file.txt可以看到,在/tmp/study/happy目录下多出来一个file.txt文件,它就是我们复制过来的文件
xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls -l /tmp/study/happy total 0 -rw-rw-r-- 1 xiaoyu xiaoyu 0 Mar 22 11:34 file.txtxiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls -l /tmp/study total 12 -rw-rw-r-- 1 xiaoyu xiaoyu 0 Mar 22 11:32 file.txt drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 11:34 happy drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 10:54 hard drwxrwxr-x 2 xiaoyu xiaoyu 4096 Mar 22 11:03 persist但是我们看到两个文件的时间属性不一样,原文件被创建/修改的时间是11:32,而被复制过来的文件的时间是11:34
可以在执行命令前加上-p选项,则会保留和原文件一样的信息
如下:
xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ cp -p /tmp/study/file.txt /tmp/study/happy xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls -l /tmp/study/happy total 0 -rw-rw-r-- 1 xiaoyu xiaoyu 0 Mar 22 11:32 file.txtxiaoyu@xiaoyu-VMware-Virtual-Platform:/$ cp /tmp/study/happy/file.txt /tmp/study/hard xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls -l /tmp/study/hard total 0 -rw-rw-r-- 1 xiaoyu xiaoyu 0 Mar 22 11:47 file.txt xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ rmdir /tmp/study/hard rmdir: failed to remove '/tmp/study/hard': Directory not empty xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ rm /tmp/study/hard/file.txt xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls /tmp/study/hard xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ cp /tmp/study /tmp/study/hard cp: -r not specified; omitting directory '/tmp/study' xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ cp -r /tmp/study /tmp/study/copyfile cp: cannot copy a directory, '/tmp/study', into itself, '/tmp/study/copyfile' xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ mkdir /tmp/study1 xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ cp /tmp/study /tmp/study1 cp: -r not specified; omitting directory '/tmp/study' xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ cp -r /tmp/study /tmp/study1 xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls -l /tmp/study1 total 4 drwxrwxr-x 6 xiaoyu xiaoyu 4096 Mar 22 11:51 study可以看出,如果要复制目录,必须加上选项-r
不带上-r只能复制文件,遇到目录就会报错;加上-r会递归复制目录中的所有子目录和文件
xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ cp -r /tmp/study /tmp/study1/copied xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls /tmp/study1 copied study xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls /tmp/study1/copied copyfile file.txt happy hard persist xiaoyu@xiaoyu-VMware-Virtual-Platform:/$ ls /tmp/study1/copied/copyfile file.txt happy hard persist可以在复制文件/目录的时候将目标文件/目标目录改名,这里将目标目录改成copied名称
