Linux File Management commands

Source: Internet
Author: User
Tags printable characters
 

1. Copy files using the CP command

This command is used to copy the given file or directory to another file or directory. It is very powerful like the Copy command in msdos.

Syntax: CP [Option] source file or directory target file or directory

Note: This Command copies the specified source file to the target file or multiple source files to the target directory.

The options of this command are as follows:

-A this option is usually used when copying directories. It retains links and file attributes, and Recursively copies directories. Its role is equal to the combination of DPR options.

-D: the link is retained during copy.

-F delete an existing target file without prompting.

-I and F options are opposite. A prompt is displayed asking the user to confirm before overwriting the target file. When the answer is Y, the target file will be overwritten, which is an interactive copy.

-P in addition to copying the source file content, CP also copies the modification time and access permissions to the new file.

-If the source file provided by R is a directory file, CP will recursively copy all subdirectories and files in the directory. The target file must be a directory name.

-L do not copy, but only link files.

It should be noted that, in order to prevent the user from using the CP Command inadvertently to destroy another file, such as the target file name specified by the user already exists, use the CP command to copy the file, this file will be overwritten by the new source file. Therefore, we recommend that you use the I option when using the CP command to copy the file.

 

2. Move command to move the file

You can use the MV command to rename a file or directory or move the file from one directory to another. This command is like a combination of Ren and move in msdos.

Syntax: MV [Option] source file or directory target file or directory

Note: depending on the type of the second parameter in the MV command (whether it is the target file or the target directory), the MV command renames the file or moves it to a new directory. When the second parameter type is file, the MV command renames the file. At this time, only one source file (or the source directory name) can be used ), it renames the given source file or directory to the given target file name. When the second parameter is an existing directory name, there may be multiple source files or directory parameters. The MV command moves the source files specified by each parameter to the target directory. When a file is moved across file systems, the MV copies the file first, and then deletes the original file. The link to the file will also be lost.

The meaning of each option in the command is:

-I interactive operation. If the MV operation will overwrite the existing target file, the system will ask whether to rewrite the file and ask the user to Answer Y or N. This will avoid overwrite the file by mistake.

-F prohibit interactive operations. When an MV operation overwrites an existing target file, no instructions are given. If this option is specified, the I option will no longer work.

If the target file (not a directory) already exists, the content of the file will be overwritten by the new file. To prevent the user from using the MV command to destroy another file, it is best to use the I option when using the MV command to move the file.

 

3.Rm command to delete files or directories

You can use the RM command to delete unnecessary files. This command is used to delete one or more files or directories in a directory. It can also delete a directory and all its files and subdirectories. For linked files, the link is closed, and the original file remains unchanged.

The common format of the RM command is:

Rm [Option] file...

If the-r option is not used, RM will not delete the directory.

The options of this command are as follows:

-F ignores non-existing files and never gives a prompt.

-R indicates that Rm recursively deletes all directories and subdirectories listed in the parameter.

-I.

Be careful when using the RM command. Because once a file is deleted, it cannot be recovered. To prevent this situation, you can use the I option to confirm the files to be deleted one by one. If you enter y, the file will be deleted. If you enter anything else, the file will not be deleted.

 

4.MkdirCommand to create a directory

Function: create a directory (similar to the MD command in msdos ).

Syntax: mkdir [Option] Dir-name

Note: This command creates a directory named by Dir-name. The user who creates a directory must have write permission in the current directory (the Dir-name parent directory), and The dirname cannot be an existing directory or file name in the current directory.

The meaning of each option in the command is:

-M sets the access permission for the new directory. You can also set it using the CHMOD command.

-P can be a path name. If some directories in the path do not exist, after this option is added, the system automatically creates those directories that do not exist, that is, multiple directories can be created at a time.

 

5. rmdirCommand to delete an empty directory

Function: delete an empty directory.

Syntax: rmdir [Option] Dir-name

Description: Dir-name indicates the directory name. This command deletes one or more sub-directory items from a directory. Note that a directory must be empty before it is deleted. The RM-r dir command can replace rmdir, But it is dangerous. When deleting a directory, you must also have the write permission on the parent directory.

The meaning of each option in the command is:

-P recursively deletes the directory dirname. When the subdirectory is deleted and its parent directory is empty, it is also deleted. If the entire path is deleted or some paths are retained for some reason, the system displays the corresponding information on the standard output.

 

6. CDCommand to change the working directory

Syntax: CD [Directory]

Note: This command changes the current directory to the directory specified by directory. If no directory is specified, return to the user's home directory. To change to a specified directory, you must have the execution and read permissions on the specified directory.

This command can use wildcards

 

7.The PWD command displays the absolute path of the current directory

In the Linux hierarchical directory structure, you can use the mkdir command in any authorized directory to create a new directory, or use the CD command to convert one directory to another. However, there is no prompt to tell the user which directory is currently in. To know the current directory, run the PWD command to display the entire path name.

Syntax: pwd

Note: This command displays the absolute path of the current working directory.

 

8. The LS command is used to list the contents of a directory.

Ls is short for the English word list. Its function is to list the contents of a directory. This is one of the most commonly used commands, because you need to view the contents of a directory from time to time. This command is similar to the Dir command in DOS.

Syntax: ls [Option] [directory or file]

For each directory, this command lists all subdirectories and files in the directory. For each file, ls will output its file name and other required information. By default, output entries are sorted alphabetically. When the directory name or file name is not given, the current directory information is displayed.

The meanings of the options in the command are as follows:

-A: displays all subdirectories and files in the specified directory, including hidden files.

-A: displays all subdirectories and files in the specified directory, including hidden files. "." And "..." are not listed.

-B indicates that non-printable characters in the file name are displayed with octal escape characters.

-C is sorted by the file modification time.

-C is divided into multiple columns to display items.

-D if the parameter is a directory, only its name is displayed, and all files under it are not displayed. It is often used together with the L option to obtain detailed information about the directory.

-F is not sorted. This option will invalidate the LTS option and make the AU option valid.

-F indicates "/" after the directory name, "*" after the executable file, "@" after the symbolic link, and "|" after the pipeline (or FIFO ", mark "=" after the socket file ".

-I displays the I node number of the file in the first output column.

-L displays detailed information about a file in a long format. This option is the most commonly used.

The information listed in each row is: file type and number of permission links. The name of the time when the file is created or recently modified for the file owner File Group

For a symbolic link file, the displayed file name is "->" and the Referenced File Path Name.

For device files, the "file size" field shows the master and secondary device numbers, rather than the file size.

The total number of blocks in the directory is displayed at the beginning of the long format list, which contains indirect blocks.

-L if the specified name is a symbolic link file, the file to which the link is directed is displayed.

-M output is in bytes stream format. Files are displayed on different pages separated by commas.

-N: the output format is the same as that of the L option. In the output, the file owner and group are represented by the corresponding UID and GID, rather than the actual name.

-O and l options are the same, but do not display the owner information.

-P adds a slash (/) to the end of the directory.

-Q: Use "?" to indicate non-printable characters in the file name. .

-R displays the output results in alphabetical order or the earliest priority.

-R recursively displays the files in each subdirectory of the specified directory.

-S indicates the number of blocks used by each directory item, including indirect blocks.

-T: The display time is based on the modification time (most recent priority) rather than by name. If the file is modified at the same time, the file is in alphabetical order. The modification time depends on whether C or U is used. The default time mark is the last modification time.

-U indicates the last time the file was accessed (the most recent priority) rather than by name. Change the-t time to the last access time.

-X displays information about each sort item by row.

In the information displayed by the LS-l command, the start is a string consisting of 10 characters. The first character indicates the file type, which can be one of the following types:

-Common files

D directory

L Symbolic Link

Block B Device Files

C character Device File

The following nine characters indicate the object access permissions. They are divided into three groups, each with three characters.

The first group indicates the permissions of the file owner, the second group indicates the permissions of users in the same group, and the third group indicates the permissions of other users. The three characters in each group indicate the read, write, and execute permissions for the object.

The permissions are as follows:

R read

W write

X. For a directory, it indicates the access permission.

S when the file is executed, assign the UID or GID of the file to the UID (User ID) or GID (group ID) of the execution process ).

T sets the flag bit (left in memory and not swapped out ). If the file is a directory, the files in the directory can only be deleted by the Super User, directory owner, or file owner. If it is an executable file, after the file is executed, the pointer pointing to its body segment remains in the memory. In this way, the system will be able to mount the file more quickly when you execute it again.

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.