Common command and usage of file management under Linux system

Source: Internet
Author: User
Tags readable

What are the file management class commands on Linux and how they are used?
File system operation commands are divided into: directory operation command, file Operation command, Permission action command
Directory Operations Command:
LS lists the directory's attribute information
PWD lists the directory path where the user is currently located
Du list directory takes up disk space size
CD changes the user's current directory
mkdir Create a new directory
RmDir Deleting a directory and the files in it
File Operations Command
DF is used to report the total capacity, usage, and Remaining amount of the file system.
Find files in the specified path
Whereis Find a command-related information put it there.
Grep finds the specified string in the specified file
Cat continuous display, view file contents
More Paging View file contents
Less paging controls viewing of file contents
CP Copy files or directories
MV Moving files or directories
RM Delete file or non-empty directory
Touch to create an empty file
Head displays the first few lines of the file
Tail Displays the end of the file in a few lines
Permission Operation Command: The permission operation is to change the properties of the file attributes to achieve, so we need to understand the file properties


Figure 1.png
File Properties Operations
Chmod is used to set the number of access permissions based on the letter accessed by the file and the corresponding value.
Chown settings file for all
CHGRP setting the owning group information for a file


Figure 2.png
Special permissions in permission settings: Sticky bit t
Sticky bit: When a directory with permissions of 777 is granted a sticky bit, the user can only delete files that are owners in this directory.

Linux file System management includes access, browse, build, delete, move, link, permission settings and other operations, frequently used commands include: LS, CD, RM, mkdir, rmdir, less, cat, MV, LN, chmod, chown, chgrp and so on.
Frequently used commands:
LS: Lists the files under development or the current directory
Usage: LS [options] [file] lists the file information (default is the current directory). If you do not specify the-cftuvsux or--sort option, sort by letter size.
Example: Ls-f viewing files in a directory
Ls-l displaying details of files and directories
Ls-a Show hidden files
LS [0-9] displays the file name and directory name containing the number

CD: Changing the path of the current user
Usage: cd [dirName]
Description: Transform working directory to dirName. Where dirName notation can be either an absolute path or a relative path. If the directory name is omitted, it is transformed to the user's home directory (that is, the directory where login was first).
Example:
Cd/home enter '/home ' directory '
CD: Return to the previous level directory
CD ... /.. Go back to Level two directory
CD into personal home directory
CD ~user1 go to your personal home directory
CD-go back to the last directory you were in

rm:linux delete a file or directory command
Usage: RM [options] ... Directory... Delete the specified < file > (that is, unlink)
-D--directory Delete the directory that may still have data (Superuser only)
-F--force Skip files that do not exist, and do not display any information
-I--interactive Before you make any deletions, you must first confirm that
-r/r--recursive Delete all directory tiers in the directory at the same time
-V--verbose detailed display of steps
--help Show this help and leave
--version Display version information and leave the
to delete the file with the first character '-' (for example '-foo '), use one of the following methods:
RM---Foo
rm./-foo
Note that if you use RM to delete a file, you can still restore the file to its original state. If you want to make sure that the contents of the
file cannot be restored, consider using shred.
Example:
rm-i *.rd: Delete all the RB language programs; Ask for confirmation
Rm-r finished: Delete all files in finished subdirectory and subdirectories
Rm-rf finished: finished All files in subdirectories and subdirectories are deleted and not used--confirm

mkdir: Creating a Directory
Usage: mkdir [-p][--help][--version][-m < directory properties >][directory name]
Supplemental Note: mkdir can create a directory and set permissions for the directory at the same time.
Parameters
-m< Directory properties > or--mode< Directory properties > set directory permissions at the same time when the directory is established.
-P or--parents if the upper-level directory to which you want to create the directory is not currently established, the upper-level directory will be created.
--help display Help.
The--verbose displays detailed information when it executes.
--version Displays version information.
RmDir

Usage rights: All users with appropriate permissions to the current directory
Mode of Use: rmdir [-P] DirName
Description: Delete the empty directory.
Parameter:-P is deleted when the subdirectory is deleted and the directory becomes empty.
Example:
Delete the subdirectory named AAA under the working directory:
RmDir AAA
In the BBB directory under the working directory, delete the subdirectory named Test. If the BBB directory becomes an empty directory after the Test is deleted, BBB is also deleted.
How to delete a non-empty directory under Linux
RM-RF the name of the directory to delete

Less
Function:
Less is similar to more, but you can browse files at will with less, and more can only move forward, but not backwards, and no will load the entire file before viewing it.
Usage: less [parameter] file
Command parameters:
-B buffer size set the size of the buffer
-e When the file display is finished, automatically leave
-F forcing special files to be opened, such as peripheral code, directories, and binaries
-G only flags the last keyword searched
-I ignores case when searching
-m shows a percentage similar to the more command
-N Displays the line number of each line
-o "file name" saves the contents of less output in the specified file.
-Q does not use warning tones
-S displays a row of continuous empty behavior
-S line too long will be out of the partial discard
-X number Displays the "tab" key as a specified number space
/string: Search down the function of "string"
String: The ability to search up "string"
N: Repeat previous search (related to/or?)
N: Reverse Duplicate previous search (with/or? related)
b Turn back one page
D Turn back half a page
H Display Help interface
Q Exit less command
U scroll forward Half page
Y Scrolls forward one line
SPACEBAR Scrolls one line
Enter key to scroll one page
[PageDown]: Flip one PAGE Down
[PageUp]: Flip one page Up
Cases:
Example 1: Viewing a file
Command: Less Log2013.log


Figure 1.jpg
Example 2:PS viewing process information and displaying it via less paging
Command: Ps-ef |less


Figure 2.jpg
Additional Notes
1. Full Screen navigation
CTRL + F-Move forward one screen
CTRL + B-Move backward one screen
CTRL + D-Move forward half screen
CTRL + U-Move backward half screen
2. Single-line navigation
J-Move one line forward
K-Move backward one line
3. Other navigation
G-Move to the last line
G-Move to the first line
Q/zz-Exit less command
4. Other useful commands
V-Edit the current file using the configured editor
H-Show help documentation for less
&pattern-Displays only the lines of the matching pattern, not the entire file
5. Tag Navigation
When you use less to view large files, you can mark them in any one place, and you can navigate to the text location labeled with a specific tag by command:
Ma-Use A to mark the current position of the text
' A-navigate to mark a
The above is a description of the usage of the less command in Linux, because the less command does not read the entire file content at the beginning, so it will be faster to open large files.

Cat: is a text file view and connect tool. To view the contents of a file, it is simpler to use cat, which is the name of the cat directly behind it.
Usage: cat [options] [File]...de>
Parameter options
-A,--show-all equivalent to-vet
-B,--number-nonblank number of non-null output lines
-e equivalent to-ve
-E,--show-ends displays at the end of each line
-N,--number all line numbers for the output
-S,--squeeze-blank does not output multiple lines of blank lines
-T vs.-VT equivalence
-T,--show-tabs to display the jump character as ^i
-U (ignored)
-V,--show-nonprinting uses ^ and M-references, except LFD and TAB
--HELP Display this help message and leave
Cases:
# Cat/etc/profile View the contents of the profile file under the/etc/directory
# cat-b/etc/fstab View the profile content in the/etc/directory, and number non-blank lines, line numbers starting from 1;
# cat-n/etc/profile A numbered output display of all rows (including blank lines) of the profile in the/etc directory
# CAT-E/etc/profile Note: View the profile content under/etc/and append the $ symbol at the end of each line;de>
Cat plus parameter-N and NL tools, file content output at the same time, each line will be preceded by a line number;
# cat-n/etc/profile
# nl/etc/profilede>
Cat can display the contents of multiple files at the same time, for example, we can display the contents of two files simultaneously on a cat command;
# cat/etc/fstab/etc/profilede>
For files with great content, cat can be routed through the pipeline | To more tools, and then to a page-by-page view;
# Cat/etc/fstab/etc/profile | Morede>

Rmv
Role:
MV command to rename a file or directory or to move a file from one directory to another. This command is equivalent to the combination of the Ren and move commands under the DOS system. Its use rights are for all users.
Format:
MV [options] source file or directory destination file or directory
[Options] Main parameters:
-I: Interactive mode operation. If the MV operation will result in overwriting an existing target file, then the system asks whether to overwrite it, asking the user to answer "y" or "n", which avoids overwriting the file by mistake.
-F: Disables interactive operation. MV operation to overwrite an existing target file without giving any indication, after specifying this parameter the I parameter will no longer work.
A second parameter:
When the second parameter type is a file, the MV command completes the file rename, which renames the given source file or directory to the specified destination file name.
When the second parameter is a directory name that already exists, the source file or directory parameter can have more than one, and the MV command moves the source files specified by each parameter to the destination directory. When you move a file across a file system, the MV copies the original file, and the link to the file is lost.
Instance:
(1) Move all files in the/usr/udt to the current directory (".") Representation) in:
$ mv/usr/udt/*.
(2) Rename the file Test.txt to Wbk.txt:
$ mv Test.txt Wbk.txt
(3) Moving files in one subdirectory of the current directory to another subdirectory
MV File name/* Another directory
(4) Move all files under the current folder to the top level directory
MV *.. /

In:
Usage rights: All users
Usage: in [options] source dist, where option is in the format:
[-BDFINSVFF] [-S backup-suffix] [-v {numbered,existing, Simple}] [--help] [--version] [--]
Description: The Linux file system, the so-called link, we can treat it as an alias of the file.
links can be broken down into two types: Hard link and soft link (sysmbolic link)
Hard links: A file can have multiple names,
Soft Connect: A special file is generated, and the contents of the file are pointed to the location of another file.
Hard links exist in the same file system, while soft connections can span different file systems. The
in Source Dist is a connection (dist) to the source, and the parameters are determined by the use of a hard link or a soft connection.
* Either a hard link or a soft connection does not copy the original file, only consumes very little disk space.
Parameter:
-F: Link first delete the file with the same name as Dist
-D: System administrator hard link their own directory
-i: Delete the file with dist file is the first to ask
-N: When the soft link, the dist as a generic file
- S: Soft Connect (symbolic Link)
-V: Displays its file name before linking
-B: Files that will be overwritten or deleted at link time
-s SUFFIX: Add the backed up file with SUFFIX end
-V METHOD: Specify how to Back up
--help: Show Help
Example:
generates a symbolic link:zz
Ln-s yy ZZ
Creates a hard link:zz
ln for the file yy yy xx

chmod: File folder permission setting command
Usage: chmod [-CFVR] [--help] [--version] Mode file ...
Description: Linux/unix file access rights are classified as three levels: file owners, groups, and others. The use of chmod can be used to control how files are accessed by others.
Put the meter:
Mode: Permission to set the string, in the following format: [Ugoa ...] [[+-=][RWXX] ...] [,...], where u represents the owner of the file, G indicates that the owner of the file belongs to the same group (group), and O denotes other persons, a means that all three are.

    • Represents an increase in permissions,-represents a cancellation permission, = Represents a unique set of permissions.
      R is readable, w means writable, x is executable, x means only if the file is a subdirectory, or the file has been set as executable.
      -C: If the file permissions have changed, the change action will be displayed
      -F: Do not display an error message if the file permissions cannot be changed
      -V: Show details of permission changes
      -r: The same permissions change for all files in the current directory and subdirectories (that is, they are changed in a recursive manner)
      --HELP: Show Auxiliary Instructions
      --version: Display version
      Example: Set the file file1.txt to be read by everyone:
      chmod ugo+r File1.txt
      Set the file file1.txt to be readable by everyone:
      chmod a+r File1.txt
      The file file1.txt and File2.txt are set as the owner of the file, and the same group as the person to which they belong can be written, but others other than the other are not writable:
      chmod ug+w,o-w file1.txt File2.txt
      Set ex1.py to only the owner of the file can perform:
      chmod u+x ex1.py
      Set all files and subdirectories in the current directory to be readable by anyone:
      Chmod-r A+r *

Report:
In addition chmod can also use numbers to represent permissions such as chmod 777 file
The syntax is: chmod ABC file
Each of the a,b,c is a number that represents the permissions of the user, Group, and other respectively.
R=4,w=2,x=1
To rwx the attribute then 4+2+1=7;
To rw-the attribute then 4+2=6;
To r-x the property, 4+1=7.
Example:
chmod a=rwx File
And
chmod 777 File
Same effect
chmod ug=rwx,o=x File
And
chmod 771 File
Same effect
Use chmod 4755 filename to give the program root privileges
Directive Name: Chown
Usage rights: Root
Usage: chmod [-CFHVR] [--help] [--version] user[:group] File ...
Description: Linux/unix is a multi-person and multi-work system, with all files owned by the owner. The chown can be used to change the owner of the file. Generally speaking, this instruction is only used by the system administrator (root), the general user does not have the authority to change the other person's file owner, and does not have the authority to change the owner of their own files to other persons. Only the system administrator (root) has this permission.
Put the meter:
User: New file owner's user idgroup: The user group of the new file owner (group)-C: If the file owner has changed, the change action is not displayed-F: If the file owner cannot be changed, do not display the error message-H: Only the link is changed More, not the one that link really points to.-V: Shows the details of the owner change-r: the same owner change (that is, one-by-one change) for all files in the current directory and subdirectories--help: Display Auxiliary description--version: Display version
Example:
Set the owner of the archive file1.txt to the Users group user jessie:
Chown Jessie:users File1.txt
The owner of all files and subdirectories in the current directory is set to user Lamport of the Users group:
Chmod-r Lamport:users *
-RW-------(600)--only the master has read and write permission.
-rw-r--r--(644)--only the master has read and write permission, and the group user and other users have Read permission only.
-RWX------(700)--only the owner has read, write, and execute permissions.
-rwxr-xr-x (755)-The master has read, write, execute permission, but the group user and other users only read, execute permissions.
-rwx--x--x (711)-The master has read, write, execute permissions, but the group user and other users only execute permissions.
-rw-rw-rw-(666)--All users have file read and write permissions. This approach is undesirable.
-RWXRWXRWX (777)-All users have read, write, and execute permissions. More undesirable approach.
Here are two general settings for the directory:
DRWX------(700)-only the owner can read and write in the directory.
Drwxr-xr-x (755)-All users can read the directory, but only the owner can change the contents of the directory
SUID's representative number is 4, for example 4755 results are-rwsr-xr-x
Sgid's representative number is 2, for example 6755 results are-rwsr-sr-x
The sticky bit represents a number of 1, for example 7755 results are-rwsr-sr-t

Chown: Changes the owner of the specified file to the specified user or group, which can be either a user name or a user ID; a group can be a group name or a group ID; a file is a space-separated list of files to change permissions, and wildcard characters are supported. System administrators often use the Chown command to give users permission to use a file after copying it to another user's directory.
Usage: chown [options] ... Owner [: [Group]] file ...
2. Command function:
Change the file owner and group by Chown. You can use the user name and user identification number settings when you change the owner or group of the file. Ordinary users cannot change their files to other owners. Its operation permissions are generally administrators.
3. Command parameters:
Necessary parameters:
-C Displays information about the changed parts
-F Ignore Error messages
-H Fix Symbolic link
-R handles all files in the specified directory and its subdirectories
-V displays detailed processing information
-deference acts on the point of a symbolic link, not the linked file itself
Select parameters:
--reference=< directory or File > Specify the directory/file as the reference, the operation of the file/directory to the reference file/directory of the same owner and group
--from=< Current User: Current group > changes only if the current user and group are the same as the specified user and group
--HELP Display Help information
--version displaying version information
4. Usage examples:
Example 1: Changing Owners and groups
Command: Chown mail:mail log2012.log
Output:
[email protected] test6]# LL
---xr--r--1 root users 302108 11-30 08:39 linklog.log
---xr--r--1 root users 302108 11-30 08:39 log2012.log
-rw-r--r--1 root users 11-30 08:39 Log2013.log
-rw-r--r--1 root users 0 11-30 08:39 Log2014.log
-rw-r--r--1 root users 0 11-30 08:39 Log2015.log
-rw-r--r--1 root users 0 11-30 08:39 Log2016.log
-rw-r--r--1 root users 0 11-30 08:39 Log2017.log
[Email protected] test6]# chown mail:mail log2012.log
[email protected] test6]# LL
---xr--r--1 root users 302108 11-30 08:39 linklog.log
---xr--r--1 mail mail 302108 11-30 08:39 log2012.log
-rw-r--r--1 root users 11-30 08:39 Log2013.log
-rw-r--r--1 root users 0 11-30 08:39 Log2014.log
-rw-r--r--1 root users 0 11-30 08:39 Log2015.log
-rw-r--r--1 root users 0 11-30 08:39 Log2016.log
-rw-r--r--1 root users 0 11-30 08:39 Log2017.log
[Email protected] test6]#

    • R recursively alters the owner of the specified directory and all subdirectories and files under it.

    • V shows the work done by the Chown command.

Common command and usage of file management under Linux system

Related Article

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.