与Linux上的文件合作更多的是创建,列表和更新它们。毕竟,文件可以是Linux命令(即二进制文件),脚本,图像,简单的文本文件,其他文件或文件夹的指针。您可能还记得UNIX和Linux系统的“一切都是文件”描述。
Even sockets and named pipes are files in their own way. Sometimes only the owner can see and use files, sometimes everyone can and sometimes select individuals will also have access. Here are some of the subtleties.
Listing files
在Linux上列出文件很容易。您使用ls命令。另一方面,命令像ls,ls -l,ls -a和LS -LTR工作方式有所不同:
- Thelscommand with no arguments simply lists file names
- Thels -l(long listing) command adds file permissions
- Thels -acommand includes file names that start with dots (often referred to as "hidden files”)
- TheLS -LTR命令以旧订单显示文件LS -LT以新的订单列出文件
Creating and updating files
If you want to create an empty file or update the timestamp on a file, use the触碰命令。使用触碰-awill only change the access time. Using触碰-d其次是日期(例如,触碰-d 20230101) will update the timestamp to the date specified:
$ ls -l message -rw-rw-r--. 1 shs shs 39 Jun 12 2019 message $ touch -d 20230101 message $ ls -l message -rw-rw-r--. 1 shs shs 39 Jan 1 00:00 message
The permissions string displayed for any file can be broken down as follows:
- The first character represents the file type
- Characters 2-4 show the owner’s permissions
- Characters 5-7 show the group’s permissions
- 角色8-10显示其他所有人的权限
- Character 11 will be a “.” or a “+” (more on this below)
The listing below breaks some listings by fields.
<- perms ->type own grp oth ex #links own grp date/time file name ==== === === === = = === === ================ ========= - rwx r-- r-- + 1 shs shs 39 Jan 1 00:00 message - rw- r-- r-- . 1 shs shs 425 Sep 19 11:42 5letters
D RWX RWX R-X。4 shs shs 4096 11月19日14:46 Bin L RWX RWX RWX。1 shs shs 13 3月23日2020 www->/var/www/html
The file type will most often be shown as a hypen (-)这意味着它是一个常规文件,但这并不能区分文本文件与图像等。“ d”表示它是目录,“ l”表示它是符号链接。实际上,如果您使用这样的命令,则可以计算当前目录中的每种文件类型中有多少个。
$ ls -l | cut -c-1 | sort | grep -v t | uniq -c 970 - 88 d 17 l
我插入了grep -v tcommand so as not to include the final “total” (the file count) that is added by the种类命令。
更改文件权限和所有权
You can use thechmod(change file owner) command to change file permissions. The command allows you to change all permissions in one command or to add or remove individual permissions as shown in the examples below.
$ chmod 644 myfile<==在一个命令中设置所有权限$ ls -l myfile -rw-r--r-+ 1 shs shs shs 39 1月1日1 00:00 myfile $ chmod u+ x myfile<==给所有者执行许可$ ls -l myfile -rwxr--r-+ 1 shs shs shs 39 Jan 1 00:00 myfile
Keep in mind when you use thechmod命令“ u”表示“用户”(所有者),“ g”表示“ group”,而“ o”表示“其他”(其他所有人)。
Thechow命令需要sudo即使您要更改的文件属于您,访问也是您的。
$ sudo chown nemo myfile
查看文件权限 - 谁可以做什么
文件的正常长列表将显示所有者和组以及分配给每个人的权限以及给其他任何人的权限。在上面使用“ myfile”文件的示例中,所有者和组都是“ shs”。这意味着只有这个单个用户(SHS是SHS组的唯一成员)具有写入访问权限,但是任何用户都可以读取文件。
If the file looks like the below, on the other hand, you need to dig a little further to figure out who else might be able to view the file’s content:
$ ls -l notes -rw-rw-r--+ 1 shs shs 3068 Dec 21 2018 notes
那+在权限末端的签名表示除了普通所有者,集团和其他所有人之外,还设置了其他权限。要查看细节,请使用getfaclcommand like this:
$ getfacl notes # file: zipfiles # owner: shs # group: shs user::rw- user:nemo:rwx<== additional permissions for user nemogroup :: rw- mask :: rwx其他:: r-
这个command shows us that nemo also has read, write and execute permissions. This type of additional access to files can be provided to individuals or groups using thesetfaclcommand as shown in the examples below. The first of these commands allows nemo to have read, write, and execute permissions without being a member of the group associated with the file. The second command gives all members of the wheel group read and write access.
$ setfacl -m u:nemo:rwx notes $ setfacl -m g:wheel:rw message
NOTE: The-mmeans “modify”, theustands for “user”, and thegmeans “group”.
Looking at file content
Commands to view the content of files depend on the type of file you want to view. The猫命令允许您显示文本文件的内容,当然,其中包括源代码,.bashrc文件等。图像文件可以在桌面上显示,但是在终端窗口中,您只能通过以八十八格或其他格式转储文件来检查内容。这个od -bccommand shows us that the file in question is a jpg file – even if it lacks a proper file extension because it pulls up identifying data from the file contents.
$ od -bc camper | head -4 0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 001 000 110 377 330 377 340 \0 020j f i f\0 001 001 001 \0 H 0000020 000 110 000 000 377 341 070 176 105 170 151 146 000 000 111 111 \0 H \0 \0 377 341 8 ~ E x i f \0 \0 I I
您也可以使用filecommand to extract descriptive information from a file as in the example below.
$ file camper camper: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, Exif Standard: [TIFF image data, little-endian, direntries=11, manufacturer=samsung, model=SM-G935V, orientation=upper-left, xresolution=164, yresolution=172, resolutionunit=2, software=GIMP 2.8.18, datetime=2018:04:30 07:56:54, GPS-Data], progressive, precision 8, 3465x2717, components 3
包起来
使用文件有很多有用的命令。它们允许您查看和控制谁可以访问文件以及如何使用文件。它们还允许您根据需要更改设置。