文件相关的命令

1. 文件目录相关

1.1 ls

命令ls列出目录的内容。

# ls -lsa
total 20
4 drwxr-xr-x  2 root root 4096 May  6 00:22 .
4 drwx------ 20 root root 4096 May 24 09:59 ..
4 -rw-r--r--  1 root root  318 Jun  7  2022 client.c
4 -rwxr--r--  1 root root   66 Jun  7  2022 compile.sh
4 -rw-r--r--  1 root root  333 Jun  7  2022 server.c

常用选项如下:

  • -l Force output to be one entry per line

  • -a Include directory entries whose names begin with a dot

(1)文件属性

第二列(如drwxr-xr-x)表明文件的类型和权限,

  • 第一位:文件类型,包括普通文件(-)、目录文件(d)、字符设备文件(c)、块设备文件(b)、连接文件(l);

  • 第2-10位: 文件访问权限, 三位一组,共三组,分别表示拥有者(u)权限、组用户(g)权限、其他用户(o)权限;

  • 每一组权限分别是: Read, Write和eXecute

(2)设置权限

用三位二进制表示一组权限: 有权限为1,没有权限为0,

  • 101 110 011 --> 463,表示拥有者读和执行、组用户具有读和写的权限、其他用户具有写和执行权限;

  • 如对文件权限设为463权限(chmod 463 filename),则其属性为-r- -rw- -wx

增减权限,如增加用户的执行权限chmod u+x filename

  • u表示拥有者,g表示组用户,o表示其他用户,a表示所有用户

  • -表示减少权限,+表示增加权限,=表示设置成给定权限

  • r(read)表示读权限,w(write)表示写权限,x(execute)表示执行权限

1.2 pwd

命令pwd输出当前工作目录的绝对路径,举例,

# pwd
/root/wechat-public

1.3 cd

命令cd用于改变目录,cd后面加上目录,输一部分目录名,按Tab键补全。

一点小技巧。对于常用的目录,可以在.bash_profile文件中定义个变量,举例,

export THESE=' /Users/sparkandshine/Dropbox/ReportPaper/su_these

运行命令source .bash_profile让配置生效,下次要进入这个目录,只需要运行cd $THESE

1.4 touch

命令touch改变文件访问和修改时间。

touch filename,如果文件不存在,则会以默认的权限创建一个文件。touch {1..100}.txt创建100个文件。

1.5 file

命令file查看文件的类型。举例,

# file tmp.jpg wechat-public.py account.txt
tmp.jpg:          JPEG image data, JFIF standard 1.02, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 60x18, components 3
wechat-public.py: Objective-C source, UTF-8 Unicode text
account.txt:      UTF-8 Unicode text

1.6 stat

命令stat显示文件状态信息,

# stat wechat-public.py
  文件:wechat-public.py
  大小:14758           块:32         IO 块:4096   普通文件
设备:fc01h/64513d      Inode:1184858     硬链接:1
权限:(0755/-rwxr-xr-x)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2023-05-24 22:09:47.136850252 +0800
最近更改:2023-05-21 10:01:46.506100222 +0800
最近改动:2023-05-21 10:01:46.510100368 +0800
创建时间:-

1.7 find

find命令用于搜索文件,使用方法如下,

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]

举例,找到当前目录下所有.c文件,find . -name "*.c" | xarg ...

2. 磁盘相关命令

2.1 磁盘使用情况du

命令du(disk usage)列出磁盘使用情况,举例,

# du -h -d 0
1.2G    .

# du -h -d 1
133M    ./ctags
20K     ./snap
12K     ./detect_code_similarity
104K    ./get_scores
8.0K    ./.ssh
40K     ./tmp_reader_writer
7.6M    ./.cache
104K    ./teaching_os
8.0K    ./.vim
60M     ./plagiarism_detection
16K     ./net_exp
1.1M    ./os_exp
28K     ./batch_create_users
131M    ./software_setup
683M    ./ubuntu
113M    ./root
8.0K    ./.config
46M     ./absense_analysis
1.2G    .

常用选项如下:

  • -h(Human-readable),文件大小单位用Byte, Kibibyte (KiB), Mebibyte (MiB), Gibibyte (GiB), Tebibyte (TiB),Pebibyte (PiB).

  • -d N,子目录的深度

2.2 磁盘分区表fdisk

命令fdisk用于创建、修改分区表。

fdisk -l,查看磁盘所有分区情况,

# fdisk -l
Disk /dev/loop0: 53.24 MiB, 55824384 bytes, 109032 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop1: 63.23 MiB, 66293760 bytes, 129480 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop2: 67.83 MiB, 71106560 bytes, 138880 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop4: 47.101 MiB, 50315264 bytes, 98272 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop5: 91.85 MiB, 96292864 bytes, 188072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop6: 63.46 MiB, 66535424 bytes, 129952 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop7: 63.34 MiB, 66412544 bytes, 129712 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/nvme0n1: 119.25 GiB, 128035676160 bytes, 250069680 sectors
Disk model: BC711 NVMe SK hynix 128GB
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: B9F3B491-01F6-4A0C-834D-A87AD2FC1720

Device           Start       End   Sectors   Size Type
/dev/nvme0n1p1    2048   2203647   2201600   1.1G EFI System
/dev/nvme0n1p2 2203648   5349375   3145728   1.5G Linux filesystem
/dev/nvme0n1p3 5349376 250066943 244717568 116.7G Linux filesystem


Disk /dev/sda: 931.53 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: ST1000LM035-1RK1
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: D7D35337-0DA7-414D-BE7C-E8D63BAF46E5

Device          Start        End   Sectors   Size Type
/dev/sda1        2048     264191    262144   128M Microsoft reserved
/dev/sda2      264192  622323711 622059520 296.6G Microsoft basic data
/dev/sda3   622323712 1236723711 614400000   293G Microsoft basic data
/dev/sda4  1236723712 1953521663 716797952 341.8G Microsoft basic data


Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 58.35 GiB, 62646124544 bytes, 122355712 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop8: 53.24 MiB, 55824384 bytes, 109032 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

2.3 df

命令df显示文件系统磁盘空间使用情况,

# df -ha
Filesystem                         Size  Used Avail Use% Mounted on
sysfs                                 0     0     0    - /sys
proc                                  0     0     0    - /proc
udev                               3.8G     0  3.8G   0% /dev
devpts                                0     0     0    - /dev/pts
tmpfs                              771M  3.1M  768M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   58G   23G   33G  41% /
securityfs                            0     0     0    - /sys/kernel/security
tmpfs                              3.8G     0  3.8G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              3.8G     0  3.8G   0% /sys/fs/cgroup
cgroup2                               0     0     0    - /sys/fs/cgroup/unified
cgroup                                0     0     0    - /sys/fs/cgroup/systemd
pstore                                0     0     0    - /sys/fs/pstore
efivarfs                              0     0     0    - /sys/firmware/efi/efivars
none                                  0     0     0    - /sys/fs/bpf
cgroup                                0     0     0    - /sys/fs/cgroup/cpu,cpuacct
cgroup                                0     0     0    - /sys/fs/cgroup/cpuset
cgroup                                0     0     0    - /sys/fs/cgroup/freezer
cgroup                                0     0     0    - /sys/fs/cgroup/pids
cgroup                                0     0     0    - /sys/fs/cgroup/net_cls,net_prio
cgroup                                0     0     0    - /sys/fs/cgroup/perf_event
cgroup                                0     0     0    - /sys/fs/cgroup/blkio
cgroup                                0     0     0    - /sys/fs/cgroup/rdma
cgroup                                0     0     0    - /sys/fs/cgroup/hugetlb
cgroup                                0     0     0    - /sys/fs/cgroup/memory
cgroup                                0     0     0    - /sys/fs/cgroup/devices
systemd-1                             -     -     -    - /proc/sys/fs/binfmt_misc
hugetlbfs                             0     0     0    - /dev/hugepages
mqueue                                0     0     0    - /dev/mqueue
debugfs                               0     0     0    - /sys/kernel/debug
tracefs                               0     0     0    - /sys/kernel/tracing
fusectl                               0     0     0    - /sys/fs/fuse/connections
configfs                              0     0     0    - /sys/kernel/config
/dev/nvme0n1p2                     1.5G  308M  1.1G  23% /boot
/dev/nvme0n1p1                     1.1G  5.3M  1.1G   1% /boot/efi
/dev/loop2                          68M   68M     0 100% /snap/lxd/22753
tmpfs                              771M  3.1M  768M   1% /run/snapd/ns
nsfs                                  0     0     0    - /run/snapd/ns/lxd.mnt
binfmt_misc                           0     0     0    - /proc/sys/fs/binfmt_misc
/dev/loop5                          92M   92M     0 100% /snap/lxd/24061
tmpfs                              771M     0  771M   0% /run/user/0
/dev/loop0                          54M   54M     0 100% /snap/snapd/18933
tmpfs                              771M     0  771M   0% /run/user/21092
tmpfs                              771M     0  771M   0% /run/user/21125
tmpfs                              771M     0  771M   0% /run/user/21121
/dev/loop7                          64M   64M     0 100% /snap/core20/1879
/dev/loop8                          54M   54M     0 100% /snap/snapd/19122
/dev/loop6                          64M   64M     0 100% /snap/core20/1891
tmpfs                              771M     0  771M   0% /run/user/21072
tmpfs                              771M     0  771M   0% /run/user/21045
tmpfs                              771M     0  771M   0% /run/user/21058
tmpfs                              771M     0  771M   0% /run/user/21048
tmpfs                              771M     0  771M   0% /run/user/21021
tmpfs                              771M     0  771M   0% /run/user/21022
tmpfs                              771M     0  771M   0% /run/user/21052
tmpfs                              771M     0  771M   0% /run/user/21082
tmpfs                              771M     0  771M   0% /run/user/21062
tmpfs                              771M     0  771M   0% /run/user/21029
tmpfs                              771M     0  771M   0% /run/user/21056
tmpfs                              771M     0  771M   0% /run/user/21009
tmpfs                              771M     0  771M   0% /run/user/21097
tmpfs                              771M     0  771M   0% /run/user/21070
tmpfs                              771M     0  771M   0% /run/user/21094
tmpfs                              771M     0  771M   0% /run/user/21093
tmpfs                              771M     0  771M   0% /run/user/21124
tmpfs                              771M     0  771M   0% /run/user/21130
tmpfs                              771M     0  771M   0% /run/user/21055
tmpfs                              771M     0  771M   0% /run/user/21071
tmpfs                              771M     0  771M   0% /run/user/21129
tmpfs                              771M     0  771M   0% /run/user/21085
tmpfs                              771M     0  771M   0% /run/user/21088
tmpfs                              771M     0  771M   0% /run/user/21115
tmpfs                              771M     0  771M   0% /run/user/21116
tmpfs                              771M     0  771M   0% /run/user/21128
tmpfs                              771M     0  771M   0% /run/user/21114
tmpfs                              771M     0  771M   0% /run/user/21087
tmpfs                              771M     0  771M   0% /run/user/21133
tmpfs                              771M     0  771M   0% /run/user/21081
tmpfs                              771M     0  771M   0% /run/user/21122
tmpfs                              771M     0  771M   0% /run/user/21123
tmpfs                              771M     0  771M   0% /run/user/21065
tmpfs                              771M     0  771M   0% /run/user/21090
tmpfs                              771M     0  771M   0% /run/user/21098
tmpfs                              771M     0  771M   0% /run/user/21099
tmpfs                              771M     0  771M   0% /run/user/21135
tmpfs                              771M     0  771M   0% /run/user/21131
tmpfs                              771M     0  771M   0% /run/user/21077
tmpfs                              771M     0  771M   0% /run/user/21096
tmpfs                              771M     0  771M   0% /run/user/21100
tmpfs                              771M     0  771M   0% /run/user/21102
tmpfs                              771M     0  771M   0% /run/user/21084
tmpfs                              771M     0  771M   0% /run/user/21091
tmpfs                              771M     0  771M   0% /run/user/2061
tmpfs                              771M     0  771M   0% /run/user/21076
tmpfs                              771M     0  771M   0% /run/user/21113
tmpfs                              771M     0  771M   0% /run/user/21074
tmpfs                              771M     0  771M   0% /run/user/21119
tmpfs                              771M     0  771M   0% /run/user/21126
tmpfs                              771M     0  771M   0% /run/user/21053
tmpfs                              771M     0  771M   0% /run/user/21067
tmpfs                              771M     0  771M   0% /run/user/21089
tmpfs                              771M     0  771M   0% /run/user/21108
tmpfs                              771M     0  771M   0% /run/user/21107
tmpfs                              771M     0  771M   0% /run/user/21127
tmpfs                              771M     0  771M   0% /run/user/21105
tmpfs                              771M     0  771M   0% /run/user/21080
tmpfs                              771M     0  771M   0% /run/user/21086
tmpfs                              771M     0  771M   0% /run/user/21079
本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2023-05-24 22:15

results matching ""

    No results matching ""