Linux basic commands-File Management

Source: Internet
Author: User
Tags bit set superuser permission

What is a file system?

A file system is a method and data structure for the operating system to save file information on a partition.

The Linux File System has a similar general structure, and its key components include:

Super Block

Index node inode

Data Block

Directory block directory Block

 

Three basic Permissions

Character Permission Meaning of a file Meaning of directory
R Read Permission Readable File Content List objects in a directory
W Write Permission You can modify or delete a file. You can create and delete files in the directory.
X Execution permission Attackers can execute this file. You can run the CD command to enter this directory.

The directory has only the execution permission, indicating that it can enter or traverse it to enter a deeper subdirectory.

The directory has only the execution permission. to access a file with the read permission under the directory, you must know the file name before accessing the file.

The directory has only the execution permission, and the directory list cannot be listed or deleted.

The combination of the execution permission and read permission on the directory indicates that you can enter the Directory and list the directories.

You can create, delete, and rename a file in a directory by combining the execution permission and read permission.

 

Special Permissions

Special Permissions

Description

SUID When an executable file with SUID bit set is executed, the file will be executed as the owner. that is to say, no matter who executes this file, he has the privilege of the file owner. Anyone who accesses this file can use all the system resources. if the owner is root, the executor has the superuser permission.
SGID When an executable file with a SGID bit is run, the file will have the group privilege to access any system resources that can be used by the entire group. If a directory has a SGID, then, all the files copied to this directory will be reset to the same group as this directory, unless-P (preserve) is added when the file is copied to keep the file attributes) to retain the original group settings.
Sticky-bit After sticky-bit is set for a file, any user has the write permission, but the owner must delete or move the file. After sticky-bit is set for a directory, objects stored in this directory can only be deleted or moved by the owner.

 

A typical example of setting SUID is the passwd program, which allows common users to change their own passwords by changing the password field of the/etc/shadow file. however, the system administrator must not allow common users to directly change the/etc/shadow file right. this is definitely not a good idea. the solution is to set the SUID of the passwd program. When the passwd program is executed, the common user will have the permissions of the Super User. After the passwd program is running, the general user will return to the general user's permissions.

To view the permissions of the/usr/bin/passwd file, set the permissions as follows:-RWSS in R-XR-X represents suid.

View the/tmp directory and you will find that its permissions are as follows: drwxrwxrwt where T is sticky-bit

 

Linux File Type

Common files: including text files, data files, and executable binary program files.

Directory file: in Linux, a directory is considered as a special file, which is used to form the tree structure of the file system.

Device Files: in Linux, each device is regarded as a file.Block DeviceAndCharacter Device.

Link file

 

File Type Representation

Common File -
Directory D
Symbolic Link L
Character Device File C
Block Device Files B
Socket S
Named Pipe P

System Files

*. Conf configuration file

*. Rpm rmp package

* Deb package

*. A archive file

*. Lock an object to lock

*.~ Backup File

. * Implicit File

 

Programs and scripts

*. C language source code file

*. Cpp C ++ source program file

*. H c or C ++ header files

*. O program object file

*. Pl Perl source program file

*. Php source code file

*. Python source code file

*. TCL script file

*. So, *. Lib Library File

 

File Creation command: TouchFile1 this command creates an empty file named file1.

Touch. file2 this command creates a hidden file named. file2. To view the hidden file, use the LS-a command.

Sudo touch/boot/file1: Use the superuser permission to create an empty file named file1 in the/boot directory.

Command for deleting files: RmTest this command deletes the test file

Rm Test. Test this command deletes multiple files.

In UNIX and Linux, if no response is returned, the command runs successfully.

 

Command for viewing files: CAT, less, more.The difference between less and cat is that less is a split screen display. The difference between more and less is that more cannot be checked back with the arrow keys. After turning it to the end, the system automatically exits. Cat is displayed all regardless of the size of the file and exits automatically.

 

Copy command: CP/Boot/GRUB/menu. lst./menu. Bak. lst this command copies menu. lst under boot to the current directory. The copied file name is menu. Bak. lst.

. Indicates the current directory.

... Indicates the upper-level directory of the current directory.

 

Command for renaming a file: MVMenu. Bak. lst menu. Backup this command renames menu. Bak. lst in the current directory to the menu. Backup file in the current directory. In fact, it isCutCommand. It can either move a file or rename a file.

 

Command for searching files: Find

For example, run sudo find/-name ls to find the file named ls from the root directory.

Sudo find/-name "ls *" searches for files whose names start with LS from the root directory.

Use Ctrl + C to terminate the search execution.

 

Command for managing file permissions: chmod

Chmod [-R] {[ugoa] [+-=] [rwxst]}

-R indicates recursively setting permissions for all subdirectories in the directory.

The content in {} is called a mode. You can specify multiple modes. Separate multiple modes with commas.

U indicates the owner (User)
G indicates the user in the group)
O indicates other users (other)
A Indicates all users (all)

+ Adds permissions.
-Indicates the permission to be deleted.
= Indicates assigning permissions and deleting the original Permissions
R indicates that read is allowed.
W indicates that write is allowed.
X indicates that execution is allowed.

Chmod go-r testfile1 omitted

Chmod U + x testfile1 omitted

The chmod U-X, go + R testfile1 owner deletes the execution permission and adds the read permission to the group and other users.

Chmod g = u testfile1 set the Group permission to be the same as the owner

Chmod o + u testfile1 has the permission to add the owner to others

Chmod 755 testfile2

Chmod 600 testfile2

Numerical setting method

Read Write Run Binary Octal Description
- - - 000 0 No permission
- - X 001 1 Execution allowed
- W - 010 2 Write allowed
- W X 011 3 Allow execution and write
R - - 100 4 Read allowed
R - X 101 5 Allow execution and reading
R W - 110 6 Allow write and read
R W X 111 7 Allow write and read Operations

Command for changing the user and user group of a file: chown

Command Format: chown [-R] <user [: Group]> <file or directory>

Similarly, if the operation object is a directory, recursively set all subdirectories under the directory.

To change the group separately, you can use the following format:

Chown [-R] <: group> <file or directory> note: the group must have ":"

For example, chown Jason/home/Jane/testfile3 is changed to Jason

Chown: users/home/Jane/testfile3: Change the group to users.

Chown group1: group1/home/Jane/testfile3 change owner and group to group1

Chown-r Apache: Apache/home/Osmond/testdir: Change the owner and group read of all files or directories in the testdir directory and Its subdirectories to Apache

Umask command

You can use the umask command to set the default file generation mask. the default generated mask tells the system which permissions should not be granted when creating a file or directory. if you place the umask command in the environment file (. bash_profile), you can control the access permissions of all newly created files or directories.

Command Format: umask [u1u2u3]

U1 indicates that permissions of the owner are not allowed; U2 indicates that permissions of people in the same group are not allowed; U3 indicates that permissions of others are not allowed.

You can use the umask command without any parameters or with the-S parameter to view the default generated mask.

$ Umask

0022

$ Umask-S

U = rwx, G = RX, O = RX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.