Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the command for linux to modify the file?

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the order for linux to modify the file". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the command for linux to modify the file"?

Linux modify the file command: 1, "vi" or "vim" command, can be used to modify the contents of the file; 2, "mv" command, can be used to modify the name of the file; 3, "chmod", "chgrp", "chown" command, can be used to modify the file permissions; 4, "touch" command, the time of the file can be modified.

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

Linux modifies the contents of the file

The command is: vi,vim

The vi editor, the equivalent of notepad, has editing capabilities, but is weak

Vim complex editor, equivalent to windows's editplus, notepad++, etc.

Steps:

1. Execute vi world.txt to enter the editor (default command mode)

2. Click an or I to enter edit mode and type in the content: hello linux world!

3. Then press the ESC key on the keyboard to exit edit mode (enter command mode)

4. Finally, hit the colon:

5. Click wq to save and exit.

Linux modifies the name of the file

The command is: mv

The mv (English spelling: move file) command is used to rename or move a file or directory to another location.

Syntax:

Mv [options] source destmv [options] source... Directory

The format is as follows:

Mv source_file (file) dest_file (file)

Change the source file name source_file to the destination file name dest_file.

Permissions for linux to modify files

Commands are: chmod, chgrp, chown

Chmod command

The chmod command is very important to change access to a file or directory. Users use it to control access to files or directories.

Take a folder called "cc" under the home folder as an example. The following step by step describes how to modify permissions:

1. Open the terminal. Enter "su" (without quotation marks)

two。 Next you will be asked to enter your password and enter your root password.

3. Suppose my folder is in the home directory with the address / var/home/dengchao/cc. Suppose I want to change the file permissions to 777, then the permission to enter the chmod 777 / var/home/userid/cc folder on the terminal becomes 777.

If you are modifying folder and subfolder permissions, you can use chmod-R 777 / var/home/userid/cc.

The specific permissions (such as the meaning of 777, etc.) are explained below:

There are 3 bits in 1.777, the highest bit 7 is to set file owner access, the second is to set group access, and the lowest bit is to set other people's access.

The permissions of each of them are represented by numbers. Specifically, there are these permissions:

R (Read, read, permission value is 4): for files, it has permission to read the contents of files; for directories, it has permission to browse directories.

W (Write, write, permission value is 2): for files, it has the permission to add and modify file contents; for directories, it has the permission to delete and move files in the directory.

X (eXecute, execute, permission value is 1): for files, have permission to execute files; for directories, the user has permission to enter directories.

two。 First of all, let's look at how to determine the permission value on a single bit. For example, the highest bit represents the file owner permission value. When the number is 7, 7 is represented by "rwx"-{4 (r) + 2 (w) + 1 (x) = 7}-and if the value is 6, then "rw-" is used to indicate-{4 (r) + 2 (w) + 0 (x) = 6} -, "-" indicates that you do not have the permission, here the table shows that you do not have the "execute" permission.

If we set the access rights of other users to "r -", the value is 4, 0,0,4.

At first, many beginners will be confused, in fact, it is very simple, we regard rwx as a binary number, if there is, it is expressed as 1, and if there is no, there is 0, then rwx can be expressed as: 111l

And the binary 111 is 7.

3. Let's take a look at how to determine permissions on three digits. If we want to set permissions for a file, the specific permissions are as follows:

If the file owner has "read", "write" and "execute" permissions, group users have "read" permissions, and other users have "read" permissions, the corresponding letter is "rwx r-r -", and the corresponding number is 744.

Generally, the highest bit represents the file owner permission value, the second bit represents group user rights, and the lowest bit represents other user rights.

Here are some examples to familiarize yourself with.

Permission value rwx rw- RMI 764rw-r-RMI 644rw-rw- RMI 664

Example 1:

$chmod axix sort

That is, set the attribute of the file sort to:

File owner (u) increases execution authority

The user (g) in the same group as the file owner increases the execution authority

Other users (o) increase execution rights

Example 2:

$chmod ug+w,o-x text

That is, set the attribute of the file text to:

File owner (u) increases write permission

Users in the same group as the file owner (g) increase write permissions

Other user (o) removes execution permission

Example 3:

$chmod Utility a.out

Suppose the a.out permission after executing chmod is (you can use the ls-l a.out command to see it):

-rws--x--x 1 inin users 7192 Nov 4 14:22 a.out

And this execution file uses a text file shiyan1.c, whose file access permission is "- rw---", that is, only the owner of the file has read and write permission.

When another user executes the a.out program, his identity temporarily changes to inin because of the program (because the s option is used in the chmod command), so he can read the shiyan1.c file (although the file is set to no one else has any permissions), which is the function of s.

Therefore, in the whole system, especially the root itself, it is best not to set too many files of this type (unless necessary) so as to ensure the security of the system and avoid the intrusion of the system because of the bug of some programs.

Example 4:

$chmod a murx mm.txt

$chmod-x mm.txt

$chmod ugo-x mm.txt

The above three commands delete the execution permission of the file mm.txt, which sets the object for all users.

Chgrp command

Function: change the group to which the file or directory belongs.

Syntax: chgrp [options] group filename quarter

Parameters:

The-c or-changes effect is similar to the "- v" parameter, but only returns part of the change.

-f or-quiet or-silent does not display an error message.

-h or-no-dereference only modifies symbolic link files and does not change any other related files.

-R or-recursive recursive processing, processing all files and subdirectories under the specified directory together.

-v or-verbose displays the instruction execution process.

-help online help.

-reference= sets all the groups of the specified file or directory to be the same as those of the reference file or directory.

-version displays version information.

This command changes the user group to which the specified file belongs. Where group can be the user group ID or the group name of the user group in the / etc/group file. The file name is a list of files to be changed to the group separated by spaces, and wildcards are supported. If the user is not the owner or superuser of the file, you cannot change the group of the file.

The options of this command mean:

-R recursively changes the grouping of the specified directory and all subdirectories and files under it.

Example 1:

$chgrp-R book / opt/local / book

Change the group of all files in / opt/local / book/ and its subdirectories to book.

Chown command

Function: change the owner and group of a file or directory. This command is also very common. For example, the root user copies one of his files to the user yusi. In order to enable the user yusi to access the file, the root user should set the owner of the file to yusi, otherwise, the user yusi cannot access the file.

Syntax: chown [options] user or group files

Description: chown changes the owner of the specified file to the specified user or group. The user can be a user name or user ID. A group can be a group name or a group ID. Files are a list of files to change permissions separated by spaces, and wildcards are supported.

Parameter description:

User: ID, the user of the new file owner

Group: the user community of the new file owner (group)

-c: the change action is displayed only if the owner of the file has indeed changed it.

-f: do not display an error message if the file owner cannot be changed

-h: only changes are made to the link, not to the file that the link actually points to

-v: displays the details of the owner's changes

-R: make the same owner changes for all files in the current directory as well as subdirectories (that is, change them one by one by recursion)

-help: displays auxiliary instructions

-version: display version

Example 1: change the owner of the file yusi123.com to yusi.

$chown yusi yusi123.com

Example 2: change the owner of the directory / demo and all the files and subdirectories under it to yusi and the group to users.

$chown-R yusi.users / demo

For example: chown qq / home/qq (change the owner of the qq directory under the home directory to the qq user)

For example: chown-R qq / home/qq (change the owner of all subfiles under the qq directory under the home directory to the qq user)

The time when linux modified the file

The command is: touch

The creation time of Linux system files can not be modified. The time of files can be modified through the touch command, so as to achieve the effect of being false and real.

Example: modify some files in the system to the time three months ago:

The current system file information is as follows:

[root@case test] # ls-ltotal 28drwxr-xr-x 2 root root 4096 Jan 9 15:47 1333-rwxrwxr-x 1 oracle oinstall 8143 Jan 9 14:41 3.shdrwxr-xr-x 2 root root 4096 Jan 9 15:49 444 Mustang Rakoto-1 root root 0 Jan 9 14:41 4.sh-rwxr-xr-x 1 root root 8143 Jan 9 14:44 77.sh-rwxrwxr-x 1 root root 54 Jan 9 16:03 find.sh

Modify all .sh files to the time of three months ago (October 13, 2015). Operation command:

[root@case test] # touch-d "10 take 13 Universe 2013" * .sh

The result is:

[root@case test] # ls-ltotal 28drwxr-xr-x 2 root root 4096 Jan 9 15:47 1333-rwxrwxr-x 1 oracle oinstall 8143 Oct 13 00:00 3.shdrwxr-xr-x 2 root root 4096 Jan 9 15:49 444 Mustang Rakoto-1 root root 0 Oct 13 00:00 4.sh-rwxr-xr-x 1 root root 8143 Oct 13 00:00 77.sh-rwxrwxr-x 1 root root 54 Oct 13 00:00 find.sh

In addition, you can change the time or month separately, as follows

To use am, pm or 24-hour format, dates can be in other formats such as 6 May 2000.

Touch-d "6:03pm" filetouch-d "05max 06ax 2000" filetouch-d "6:03pm 05max 06max 2000" file here, I believe you have a deeper understanding of "what is the order for linux to modify the file", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report