[Linux] Permissions and commands


1. Basic instructions under Linux

Linux中的指令有很多,本文只着重介绍常用指令

1.ls

The function is very simple, it is to list all subdirectories and files under the directory.
In addition, there are many common options:

-a List all files in the directory, including hidden files starting with .
-d Display directories as files instead of the files under them. For example: ls -d specifies the directory
-i outputs the index information of the i node of the file. For example, ls -ai specifies the file
-k to express the size of the file in the form of k bytes. ls –alk specifies the file
-l lists the detailed information of the file.
-n Use numeric UID, GID instead of name. (Introduce UID, GID)
-F Append a character after each file name to describe the type of the file, "*" means an executable ordinary file; "/" means a directory; "@" means a symbolic link; "|" means FIFOs; "=" means sockets. (directory type identification)
-r Reverse sorting on directories.
-t Sort by time.
-s Output the size of the file after the l filename. (Sort by size, how to find the largest file in the directory)
-R List the files in all subdirectories. (recursively)
-1 Output only one file per line.

The most commonly used is ls -l, which can also be abbreviated as two ll.

insert image description here

2.pwd

Display the user's current directory

3.cd

There is no folder like windows under Linux, but there is a corresponding directory called directory. cd is to enter a specific directory.
Usage : cd directory name
example :
cd … : return to the parent directory
cd /home/litao/linux/
cd …/day02/ : relative path
cd ~: enter the user’s home directory
cd -: return to the most recently visited directory

4.touch

The touch command can change the date and time of a document or directory, including access time and change time, or create a new file that does not exist.
Usage : touch option file
common options :

-a or --time=atime or --time=access or --time=use only changes the access time.
-c or --no-create Do not create any documentation.
-d Use the specified datetime instead of the current time.
-f This parameter will be ignored and not processed, it is only responsible for solving the compatibility problem of the BSD version touch command.
-m or --time=mtime or --time=modify only change the change time.
-r Set the date and time of the specified document or directory to be the same as the date and time of the reference document or directory.
-t Use the specified datetime instead of the current time.

5.mkdir

Create a new directory under the current directory
Usage : mkdir directory name

6.rmdir&rm

rmdir is a bit tasteless, it can only delete empty directories.

rm can delete files or directories at the same time

Common options :

-f Even if the file attribute is read-only (that is, write-protected), delete it directly
-i ask for confirmation one by one before
deleting -r delete the directory and all files under it

7.man

There are many instructions under Linux, and it is impossible for us to remember them all, so we need to consult the help manual. You need to query the manual through the man command.
Common options :

-k Search the online help num according to the keyword to find only chapter num
-a Display all chapters, such as man printf, it defaults to search from the first chapter, stop when you know it, use the a option, when you press q to exit, it will continue to search until all chapters are searched.

The manual is divided into eight chapters

1 is an ordinary command
2 is a system call, such as open, write and the like (through this, at least you can easily find out what header files need to be added to call this function) 3 is a library function, such as printf, fread4 is a special file, that is, various device files under /dev 5 is the format of the file, such as passwd, which will explain the meaning of each field in this file 6 is reserved for the game, defined by each game 7 is an attachment and some variables, such as global variables such as environ Here it is explained that 8 is a command for system management, these commands can only be used by
root
,
such
as
ifconfig

8. cp command

Copy file or directory
command : cp option source file or directory target address
common selection :

-f or --force forcibly copy files or directories, regardless of whether the destination file or directory already exists
-i or --interactive ask the user before overwriting files
-r recursive processing, and process the files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to a directory or a symbolic link, it will be treated as an ordinary file.
-R or --recursive recursive processing, and the files and subdirectories in the specified directory will be processed together

9.mv

That is, move, move the file, and you can also rename the file
Command : mv source file or directory target file or directory
specific functions :

  1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory.
  2. When the second parameter type is a file, the mv command completes file renaming. At this time, there can only be one source file (it can also be the source directory name), and it will rename the given source file or directory to the given target file name.
  3. When the second parameter is an existing directory name, there can be more than one source file or directory parameter, and the mv command will move the source files specified by each parameter to the target directory.

Common options :

-f: force means to force, if the target file already exists, it will not ask but directly overwrite
-i: if the target file (destination) already exists, it will ask whether to overwrite!

10.cat


Common options for viewing the contents of a target file :

-b number non-empty output lines
-n number all output lines
-s do not output multiple empty lines

11.more


Common options similar to cat :
-n number all lines of output
q exit more

12.less

  • The less tool is also a tool for displaying files or other output in pages. It should be said that it is an orthodox tool for viewing file content in Linux, and its functions are extremely powerful.
  • The usage of less is more flexible than that of more. In more, we have no way to turn forward, we can only look back
  • But if you use less, you can use [pageup][pagedown] and other key functions to browse files back and forth, which is easier to use to view the content of a file!
  • In addition, you can have more search functions in less, you can not only search downward, but also search upward.

Common options :

-i ignore case when searching
-N show line number / string
: function to search "string" down
?


13.head

head and tail are as easy to understand as their names. They are used to display a certain number of text blocks at the beginning or end.
head is used to display the beginning of the file to the standard output, and the default head command prints the first 10 lines of its corresponding file.
Command : head
Common options for parameter files :

-n<number of lines> number of lines to display

14.tail

The tail command writes the file to the standard output from the specified point. Use the -f option of the tail command to easily check the log file being changed. tail -f filename will display the last content in the filename on the screen, and not only refresh, so that you can see the latest file content. It is used to display the end content of the specified file. When the file is not specified, it will be processed as input information
. Commonly used to view log files.
Common options :

-f read in a loop
-n<number of lines> display the number of lines

15.find

  • The find command under Linux searches for files in the directory structure and performs specified operations.
  • The find command under Linux provides quite a lot of search conditions, and the function is very powerful. Because find has powerful functions, it has many options, most of which are worth our time to understand.
  • Even if the system contains a network file system (NFS), the find command is also valid in the file system, as long as you have the corresponding permissions.
  • When running a very resource-consuming find command, many people tend to put it in the background, because traversing a large file system may take a long time (here refers to the file system above 30G bytes).

Common options :

-name Find files by filename.

16.zip/unzip

Compress a directory or file into a zip format, or extract a directory or file from a zip format
Common options :

-r Recursive processing, all files and subdirectories in the specified directory will be processed together

Example :

Compress the test2 directory: zip test2.zip test2/*
Unzip to the tmp directory: unzip test2.zip -d /tmp

17.tar

Pack/unpack, do not open it, directly see the content
Common options :

-c: Create a parameter command of a compressed file (create meaning);
-x: Unzip a parameter command of a compressed file!
-t: View the files inside the tarfile!
-z : Does it also have the gzip attribute? That is, does it need to be compressed with gzip?
-j: Does it also have the attributes of bzip2? That is, does it need to be compressed with bzip2?
-v : Show files during compression! This is commonly used, but not recommended for background execution!
-f: Use the file name, please note that the file name must be followed immediately after f! Do not add parameters!
-C : Unzip to the specified directory

18.unname

uname is used to obtain information about computers and operating systems.
uname can display basic information such as the version of the operating system used by the Linux host and the name of the hardware.
Common options:

-a or –all output all information in detail, followed by kernel name, host name, kernel version number, kernel version, hardware name, processor type, hardware platform type, operating system name

Two, authority

1. User

There are two types of users under Linux: super user (root) and ordinary user

  • Super user: can do anything under the linux system without restriction
  • Ordinary users: do limited things under linux.
  • The command prompt of a super user is "#", and the command prompt of a normal user is "$".

You can use the su command to switch users, switch from a normal user to a super user: su root, and you need to enter the root password, and switch from root to a normal user: su username, no password is required.

2. Classification of file visitors

  • Owner of files and file directories: u—User (Chinese civilian legal issues)
  • Users of the group where the owner of the file and file directory belongs: g—Group (not much to say)
  • Other users: o—Others (foreigners)

The meaning of the group you belong to is that you can be in the same group as you or you want one person to access the file and not allow others to access the file.

3. File Types and Access Rights

insert image description here
file type:

d: folder-
: ordinary file
l: soft link (similar to Windows shortcut)
b: block device file (such as hard disk, CD-ROM, etc.)
p: pipeline file
c: character device file (such as screen and other serial devices)
s: socket file

Basic permissions :

  • r(read): For a file, it has the permission to read the content of the file; for a directory, it has the permission to browse the directory information
  • w(write): For files, it has the permission to modify the content of the file; for directories, it has the permission to delete files in the moved directory
  • x(execute): For a file, it has the permission to execute the file; for a directory, it has the permission to enter the directory

So r– means that there is only read permission, and rwx means that there are three permissions that can be read, written and executed.
Permissions can also be represented in binary and octal:

permissions binary Octal
–x 001 1
-w- 010 2
r– 100 4
rw- 110 6
rwx 111 7
000 0

Other arrangements will not be listed one by one, note that rwx is a fixed position

3. File permission setting

a)chmod

Setting file access permissions
Note that only root and the file owner can set file permissions.
Command 1 : chmod user indicator +/-= permission character file name

+: Add the authority indicated by the authority code to the scope of authority
-: Cancel the authority indicated by the authority code to the scope of authority
=: Grant the authority indicated by the authority code to the scope of authority
User symbols :
u: owner
g: owner in the same group
o: other users
a: all users

Example:
chmod u+w /home/abc.txt
chmod ox /home/abc.txt

Command 2 : chmod permission octal file name
Example:
chmod 664 /home/abc.txt
chmod 640 /home/abc.txt

b)chown

Modify the owner of the file
Command: chown user name file name
Example:
chown user1 f1
chown -R user1 filegroup1

c)chgrp

Modify the group to which a file or directory belongs
Command: chgrp [parameter] user group name file name
Example: chgrp users /abc/f2

4. Directory permissions

  • Executable permission: If the directory does not have executable permission, you cannot cd into the directory.
  • Readable permission: If the directory does not have readable permission, you cannot use commands such as ls to view the contents of files in the directory.
  • Writable Permissions: If a directory does not have writable permissions, files cannot be created in the directory and files cannot be deleted in the directory.

But there is a problem here, that is, as long as the user has the write permission of the directory, the user can delete the file in the directory, regardless of whether the user has the write permission of the file.
So in order to solve this problem, the concept of sticky bit is proposed.

Command: chmod +t directory
When a directory is added sticky bit, the files in this directory can only be used by:

  • super admin delete
  • The owner of the directory removes
  • The owner of the file deletes

Guess you like

Origin blog.csdn.net/lyq2632750277/article/details/128421326