文件管理

1、创建文件

touch 文件名1 文件名2

[root@fishman-160 ~]# touch 1.txt
[root@fishman-160 ~]# ls
1.txt  公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg
[root@fishman-160 ~]# touch {2..10}.txt
[root@fishman-160 ~]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt  公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg
[root@fishman-160 ~]# touch file{1..3}.txt #创建文件名file{序号},多个文件
[root@fishman-160 ~]# ls
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt  公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  file1.txt  file2.txt  file3.txt  initial-setup-ks.cfg
​

echo 创建

[root@fishman-160 ~]#   echo '' > 1.txt # >重定向输出,会覆盖原文件
[root@fishman-160 ~]# ls
1.txt  公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg

vim 创建

[root@fishman-160 ~]# vim 2.txt
[root@fishman-160 ~]# ls
1.txt  2.txt  公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg

 

2、文件属性

2.1、stat 类似windows的右键属性

[root@fishman-160 ~]# stat /etc/passwd
  文件:/etc/passwd
  大小:2580       块:8          IO 块:4096   普通文件
设备:803h/2051d   Inode:36713302    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:system_u:object_r:passwd_file_t:s0
最近访问:2023-07-10 09:11:14.062378566 +0800 #atime
最近更改:2023-07-06 16:28:39.856921996 +0800 #mtime
最近改动:2023-07-06 16:28:39.857921996 +0800 #ctime chmod + x xx.sh,修改了ctime
创建时间:2023-07-06 16:28:39.856921996 +0800

2.2、查看一天内生成的文件

[root@fishman-160 ~]# find -mtime -1
.
./.cache/mesa_shader_cache
./.cache/mesa_shader_cache/index
./.cache/mesa_shader_cache/6b
./.cache/mesa_shader_cache/6b/0825442bd89c7f5d49a2d1a9c18d7be40e0c3c
./.cache/mesa_shader_cache/5d
./.cache/mesa_shader_cache/5d/a9b9fedf7d5576fea0bd05f2c3ff393a8d9b91
./.cache/mesa_shader_cache/c0
./.cache/mesa_shader_cache/c0/f6fbd4f349949edea51ef8b4fea20ff0d89b35
./.cache/mesa_shader_cache/f8
./.cache/mesa_shader_cache/f8/6f7b870befd4048e6068504182d89f730f9574
./.cache/tracker
./.cache/tracker/meta.db
./.cache/tracker/meta.db-wal
./.cache/tracker/meta.db-shm
./.cache/tracker/locale-for-miner-apps.txt
./.cache/event-sound-cache.tdb.dc083866081e4b7bb22ee69c527f6da4.x86_64-redhat-linux-gnu
./.cache/gnome-software/shell-extensions
./.cache/gnome-software/shell-extensions/gnome.json
./.cache/gnome-software/shell-extensions/extensions-web.xmlb
./.cache/gnome-software/odrs
./.cache/gnome-software/odrs/ratings.json
./.config/pulse/dc083866081e4b7bb22ee69c527f6da4-default-sink
./.config/pulse/dc083866081e4b7bb22ee69c527f6da4-default-source
./.config/ibus/bus
./.config/ibus/bus/dc083866081e4b7bb22ee69c527f6da4-unix-0
./.config/dconf
./.config/dconf/user
./.config/gtk-3.0
./.config/gtk-3.0/bookmarks
./.ICEauthority
./.local/share/evolution/addressbook/system
./.local/share/evolution/addressbook/system/contacts.db
./.local/share/tracker/data
./.local/share/tracker/data/tracker-store.journal
./.local/share/tracker/data/.meta.isrunning
./.local/share/keyrings
./.Xauthority

2.3、查看文件类型

[root@fishman-160 ~]# file /etc/passwd
/etc/passwd: ASCII text #通过file来查看

2.4、按照一定排序查看文件

按照时间排序ls -ltr

[root@fishman-160 ~]# ls -lhtr ~ #r 是reverse,反转,代表从小到大
总用量 39M
drwxr-xr-x  9 1001 1001  186 6月  13 23:08 nginx-1.25.1
-rw-------. 1 root root 1.3K 7月   6 13:39 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.8K 7月   6 16:28 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 7月   6 16:37 桌面
drwxr-xr-x. 2 root root    6 7月   6 16:37 音乐
drwxr-xr-x. 2 root root    6 7月   6 16:37 下载
drwxr-xr-x. 2 root root    6 7月   6 16:37 文档
drwxr-xr-x. 2 root root    6 7月   6 16:37 图片
drwxr-xr-x. 2 root root    6 7月   6 16:37 视频
drwxr-xr-x. 2 root root    6 7月   6 16:37 模板
drwxr-xr-x. 2 root root    6 7月   6 16:37 公共
drwxr-xr-x. 2 root root   19 7月  10 10:57 nework
drwxr-xr-x. 2 root root    6 7月  10 14:05 test
-rw-r--r--. 1 root root 2.6K 7月  11 14:02 passwd
-rw-r--r--. 1 root root    6 7月  11 14:45 password
-rw-r--r--. 1 root root   22 7月  11 15:52 a是windows打开正常linux打开乱码.txt
-rw-r--r--. 1 root root   32 7月  11 15:55 aa.txt
-rw-r--r--. 1 root root   73 7月  11 16:00 b在linux编辑的文档熬windows下没有换行.sh
-rw-------. 1 root root  949 7月  13 11:24 nohup.out
-rw-r--r--  1 root root 1.2M 7月  19 16:57 nginx-1.25.1.tar.gz
-rw-r--r--  1 root root 442K 7月  21 15:39 libncurses5-32bit-6.4.20230701-16.2.x86_64.rpm
-rw-r--r--  1 root root  10K 7月  25 16:45 -cvf
-rw-r--r--  1 root root 4.7M 7月  27 12:11 nginx.tar.bz2
-rw-r--r--  1 root root 2.9M 7月  27 12:16 nginx.tar.xz
-rw-r--r--  1 root root 6.5M 7月  27 12:22 nginx.tar.gz
-rw-r--r--  1 root root  24M 7月  27 12:25 nginx.tar
root@fishman-160 ~]# ls -lht ~ #日期由大到小
总用量 39M
-rw-r--r--  1 root root  24M 7月  27 12:25 nginx.tar
-rw-r--r--  1 root root 6.5M 7月  27 12:22 nginx.tar.gz
-rw-r--r--  1 root root 2.9M 7月  27 12:16 nginx.tar.xz
-rw-r--r--  1 root root 4.7M 7月  27 12:11 nginx.tar.bz2
-rw-r--r--  1 root root  10K 7月  25 16:45 -cvf
-rw-r--r--  1 root root 442K 7月  21 15:39 libncurses5-32bit-6.4.20230701-16.2.x86_64.rpm
-rw-r--r--  1 root root 1.2M 7月  19 16:57 nginx-1.25.1.tar.gz
-rw-------. 1 root root  949 7月  13 11:24 nohup.out
-rw-r--r--. 1 root root   73 7月  11 16:00 b在linux编辑的文档熬windows下没有换行.sh
-rw-r--r--. 1 root root   32 7月  11 15:55 aa.txt
-rw-r--r--. 1 root root   22 7月  11 15:52 a是windows打开正常linux打开乱码.txt
-rw-r--r--. 1 root root    6 7月  11 14:45 password
-rw-r--r--. 1 root root 2.6K 7月  11 14:02 passwd
drwxr-xr-x. 2 root root    6 7月  10 14:05 test
drwxr-xr-x. 2 root root   19 7月  10 10:57 nework
drwxr-xr-x. 2 root root    6 7月   6 16:37 公共
drwxr-xr-x. 2 root root    6 7月   6 16:37 模板
drwxr-xr-x. 2 root root    6 7月   6 16:37 视频
drwxr-xr-x. 2 root root    6 7月   6 16:37 图片
drwxr-xr-x. 2 root root    6 7月   6 16:37 文档
drwxr-xr-x. 2 root root    6 7月   6 16:37 下载
drwxr-xr-x. 2 root root    6 7月   6 16:37 音乐
drwxr-xr-x. 2 root root    6 7月   6 16:37 桌面
-rw-r--r--. 1 root root 1.8K 7月   6 16:28 initial-setup-ks.cfg
-rw-------. 1 root root 1.3K 7月   6 13:39 anaconda-ks.cfg
drwxr-xr-x  9 1001 1001  186 6月  13 23:08 nginx-1.25.1

按照文件体积排序

[root@fishman-160 ~]# ls -lhS ~ #文件体积,由大到小
总用量 39M
-rw-r--r--  1 root root  24M 7月  27 12:25 nginx.tar
-rw-r--r--  1 root root 6.5M 7月  27 12:22 nginx.tar.gz
-rw-r--r--  1 root root 4.7M 7月  27 12:11 nginx.tar.bz2
-rw-r--r--  1 root root 2.9M 7月  27 12:16 nginx.tar.xz
-rw-r--r--  1 root root 1.2M 7月  19 16:57 nginx-1.25.1.tar.gz
-rw-r--r--  1 root root 442K 7月  21 15:39 libncurses5-32bit-6.4.20230701-16.2.x86_64.rpm
-rw-r--r--  1 root root  10K 7月  25 16:45 -cvf
-rw-r--r--. 1 root root 2.6K 7月  11 14:02 passwd
-rw-r--r--. 1 root root 1.8K 7月   6 16:28 initial-setup-ks.cfg
-rw-------. 1 root root 1.3K 7月   6 13:39 anaconda-ks.cfg
-rw-------. 1 root root  949 7月  13 11:24 nohup.out
drwxr-xr-x  9 1001 1001  186 6月  13 23:08 nginx-1.25.1
-rw-r--r--. 1 root root   73 7月  11 16:00 b在linux编辑的文档熬windows下没有换行.sh
-rw-r--r--. 1 root root   32 7月  11 15:55 aa.txt
-rw-r--r--. 1 root root   22 7月  11 15:52 a是windows打开正常linux打开乱码.txt
drwxr-xr-x. 2 root root   19 7月  10 10:57 nework
drwxr-xr-x. 2 root root    6 7月   6 16:37 公共
drwxr-xr-x. 2 root root    6 7月   6 16:37 模板
drwxr-xr-x. 2 root root    6 7月   6 16:37 视频
drwxr-xr-x. 2 root root    6 7月   6 16:37 图片
drwxr-xr-x. 2 root root    6 7月   6 16:37 文档
drwxr-xr-x. 2 root root    6 7月   6 16:37 下载
drwxr-xr-x. 2 root root    6 7月   6 16:37 音乐
drwxr-xr-x. 2 root root    6 7月   6 16:37 桌面
-rw-r--r--. 1 root root    6 7月  11 14:45 password
drwxr-xr-x. 2 root root    6 7月  10 14:05 test
​
[root@fishman-160 ~]# ls -lhrS ~ #r代表由小到大
总用量 39M
drwxr-xr-x. 2 root root    6 7月  10 14:05 test
-rw-r--r--. 1 root root    6 7月  11 14:45 password
drwxr-xr-x. 2 root root    6 7月   6 16:37 桌面
drwxr-xr-x. 2 root root    6 7月   6 16:37 音乐
drwxr-xr-x. 2 root root    6 7月   6 16:37 下载
drwxr-xr-x. 2 root root    6 7月   6 16:37 文档
drwxr-xr-x. 2 root root    6 7月   6 16:37 图片
drwxr-xr-x. 2 root root    6 7月   6 16:37 视频
drwxr-xr-x. 2 root root    6 7月   6 16:37 模板
drwxr-xr-x. 2 root root    6 7月   6 16:37 公共
drwxr-xr-x. 2 root root   19 7月  10 10:57 nework
-rw-r--r--. 1 root root   22 7月  11 15:52 a是windows打开正常linux打开乱码.txt
-rw-r--r--. 1 root root   32 7月  11 15:55 aa.txt
-rw-r--r--. 1 root root   73 7月  11 16:00 b在linux编辑的文档熬windows下没有换行.sh
drwxr-xr-x  9 1001 1001  186 6月  13 23:08 nginx-1.25.1
-rw-------. 1 root root  949 7月  13 11:24 nohup.out
-rw-------. 1 root root 1.3K 7月   6 13:39 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.8K 7月   6 16:28 initial-setup-ks.cfg
-rw-r--r--. 1 root root 2.6K 7月  11 14:02 passwd
-rw-r--r--  1 root root  10K 7月  25 16:45 -cvf
-rw-r--r--  1 root root 442K 7月  21 15:39 libncurses5-32bit-6.4.20230701-16.2.x86_64.rpm
-rw-r--r--  1 root root 1.2M 7月  19 16:57 nginx-1.25.1.tar.gz
-rw-r--r--  1 root root 2.9M 7月  27 12:16 nginx.tar.xz
-rw-r--r--  1 root root 4.7M 7月  27 12:11 nginx.tar.bz2
-rw-r--r--  1 root root 6.5M 7月  27 12:22 nginx.tar.gz
-rw-r--r--  1 root root  24M 7月  27 12:25 nginx.tar

3、创建目录

mkdir创建目录

[root@fishman-160 ~]# mkdir test
[root@fishman-160 ~]# ls
1.txt  2.txt  公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg  test

mkdir 创建多级目录

[root@fishman-160 ~]# mkdir test/a/b/c
mkdir: 无法创建目录 “test/a/b/c”: 没有那个文件或目录
[root@fishman-160 ~]# mkdir -p test/a/b/c #-p 创建多级目录
[root@fishman-160 ~]# tree test
test
└── a
    └── b
        └── c

4、删除文件和目录

rm 删除文件和目录

[root@fishman-160 ~]# ls
1.txt  2.txt  公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg  test
[root@fishman-160 ~]# rm -rf *.txt test # -r 递归,-f 强制
[root@fishman-160 ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg

rmdir 删除目录

root@fishman-160 ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg  test
[root@fishman-160 ~]# rmdir test
[root@fishman-160 ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg

1700449176664

5、复制文件和目录

cp 复制文件和目录

-R/r

递归

[root@fishman-160 ~]# cp /etc/passwd ~
[root@fishman-160 ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg  passwd
​
[root@fishman-160 ~]# cp -r /etc/sysconfig/network-scripts ~ #递归复制,可以用于复制目录
[root@fishman-160 ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg  network-scripts  passwd
[root@fishman-160 ~]# ls network-scripts/
ifcfg-ens33

-a

cp -a 是 Linux 和类 Unix 操作系统中用于复制文件和目录的命令,其中 -a 选项表示进行递归复制并保持源文件和目录的属性。具体来说,-a 选项相当于同时使用了 -R(递归复制)和 -p(保持文件属性)选项。

使用 cp -a 命令可以保留文件和目录的所有属性,包括权限、所有者、组、时间戳等。这在需要完整复制文件系统层次结构时很有用,尤其是在备份或迁移文件时。

示例:

cp -a /path/to/source /path/to/destination

这将递归地复制 /path/to/source 目录及其所有内容到 /path/to/destination,同时保持所有文件和目录的属性。

1700447614488

6、移动文件和目录

[root@fishman-160 ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg  network-scripts
[root@fishman-160 ~]# mv network-scripts nework #通过mv命令完成目录重命名
[root@fishman-160 ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg  nework
[root@fishman-160 ~]# ls nework/
1.txt

7、查看文件

cat 文件

[root@fishman-160 ~]# cat /root/.bash_profile  #cat命令查看文件
# .bash_profile 

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

[root@fishman-160 ~]# cat /proc/cpuinfo | more #cat的结果通过管道符输出more
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Xeon(R) CPU E3-1230 v3 @ 3.30GHz
stepping    : 3
microcode   : 0x27
cpu MHz     : 3300.004
cache size  : 8192 KB
physical id : 0
siblings    : 4
core id     : 0
cpu cores   : 4
apicid      : 0
initial apicid  : 0
fpu     : yes
fpu_exception   : yes

more 文件

[root@fishman-160 ~]# more /root/.bash_profile  #more命令查看文件
# .bash_profile# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi# User specific environment and startup programs
​
PATH=$PATH:$HOME/bin
​
#不支持后退,但几乎不需要加参数,空格键是向下翻页,Enter键是向下翻一行,在不需要后退的情况下比较方便

less 文件

[root@fishman-160 ~]# less /root/.bash_profile 
#支持前后翻滚,既可以向上翻页(pageup按键),也可以向下翻页(pagedown按键)。,空格键是向下翻页,Enter键是向下翻一行

head 文件

[root@fishman-160 ~]# head /etc/passwd #默认查看文件前10行
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 文件

[root@fishman-160 ~]# tail /etc/passwd #默认查看文件末10行
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
setroubleshoot:x:980:977::/var/lib/setroubleshoot:/sbin/nologin
flatpak:x:979:976:User for flatpak system helper:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:978:975::/run/gnome-initial-setup/:/sbin/nologin
pesign:x:977:974:Group for the pesign signing daemon:/run/pesign:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
kc:x:1000:1000:kc:/home/kc:/bin/bash

tail -f 文件

[root@fishman-160 ~]# tail -f /var/log/messages  #监控尾部信息,动态显示数据(不关闭)
Jul 10 10:44:06 fishman-160 systemd[1]: Starting dnf makecache...
Jul 10 10:44:06 fishman-160 dnf[4643]: 元数据缓存近期已刷新。
Jul 10 10:44:06 fishman-160 systemd[1]: dnf-makecache.service: Succeeded.
Jul 10 10:44:06 fishman-160 systemd[1]: Started dnf makecache.
Jul 10 10:53:24 fishman-160 dbus-daemon[1836]: [session uid=0 pid=1836] Activating via systemd: service name='org.freedesktop.Tracker1.Miner.Extract' unit='tracker-extract.service' requested by ':1.64' (uid=0 pid=2310 comm="/usr/libexec/tracker-miner-fs " label="unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023")
Jul 10 10:53:24 fishman-160 systemd[1795]: Starting Tracker metadata extractor...
Jul 10 10:53:24 fishman-160 dbus-daemon[1836]: [session uid=0 pid=1836] Successfully activated service 'org.freedesktop.Tracker1.Miner.Extract'
Jul 10 10:53:24 fishman-160 systemd[1795]: Started Tracker metadata extractor.
Jul 10 11:08:39 fishman-160 cupsd[974]: REQUEST localhost - - "POST / HTTP/1.1" 200 183 Renew-Subscription successful-ok
Jul 10 11:13:27 fishman-160 systemd[1]: fwupd.service: Succeeded.

image-20230710120618325

1700455356648

8、查看目录体积

查看单个目录

[root@fishman-160 ~]# du -sh /etc
33M /etc

查看所有目录

[root@fishman-160 ~]# du -h ~
4.0K    /root/.cache/dconf
4.0K    /root/.cache/mesa_shader_cache/b3
8.0K    /root/.cache/mesa_shader_cache/35
8.0K    /root/.cache/mesa_shader_cache/c7
4.0K    /root/.cache/mesa_shader_cache/62
4.0K    /root/.cache/mesa_shader_cache/01
4.0K    /root/.cache/mesa_shader_cache/8c
...

通过倒序,查看目录内1层的文件和目录,按照体积大小从大到小排序

[root@fishman-160 ~]# du -h --max-depth=1 /etc | sort -rh 
33M /etc
11M /etc/udev
9.8M    /etc/selinux
3.6M    /etc/brltty
2.2M    /etc/pki
608K    /etc/ssh
436K    /etc/containers
344K    /etc/sane.d
284K    /etc/asciidoc
264K    /etc/vmware-tools
244K    /etc/xdg
216K    /etc/libvirt
160K    /etc/sysconfig
152K    /etc/lvm
152K    /etc/dbus-1
124K    /etc/profile.d
124K    /etc/pam.d
100K    /etc/grub.d
​
posted @   墨香清梦  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示