Linux File and directory management

Source: Internet
Author: User
Tags create directory

We know that the directory structure of Linux is tree-like structure, the top-level directory is the root directory/.

Other catalogs can be mounted to add them to the tree, and they can be removed by lifting the mount.

Before starting this tutorial we need to know what absolute and relative paths are.

    • Absolute path:
      The path is written by the root directory/write, for example:/usr/share/doc this directory.
    • Relative path:
      The way the path is written, not by/writing, for example, by/usr/share/doc to the bottom of/usr/share/man, can be written as: CD. /man This is how the relative path is written!
Common commands for working with catalogs

Let's look at a few common commands for working with directories:

    • LS: List directory
    • CD: Switch directories
    • PWD: Displays the current directory
    • mkdir: Create a new directory
    • RmDir: Delete an empty directory
    • CP: Copying Files or directories
    • RM: Remove files or directories

You can use the man [command] to view the documentation for each command, such as: Man CP.

LS (list directory)

In Linux systems, the LS command is probably the most commonly run.

Grammar:

[[email protected] ~]# ls [-~]# ls [--color=~]# ls [--full-time] directory name

Options and Parameters:

    • -A: All files are listed together with the hidden file (files that begin with.) (common)
    • -D: Lists only the directory itself, not the file data in the directory (common)
    • -L: Long data string list, including file attributes and permissions and so on data; (common)

List all the files in the home directory (with attributes and hidden documents)

[Email protected] ~]# Ls-al ~
CD (Switch directory)

The CD is the abbreviation for Change directory, which is the command used to transform the working directory.

Grammar:

CD [Relative path or absolute path]
~~]# cd/root/w3cschool.cc/~]# cd./w3cschool.cc//~/~]# CD.

Next, we should be able to understand the CD command very well.

PWD (Displays the directory that is currently located)

The PWD is the abbreviation for print working directory, which is the command that displays the directory that is currently located.

[Email protected] ~]# pwd [-P] Options and Parameters:-P: Shows the exact path, not the link path. Example: simply display the current working directory: [[email protected]~]# pwd/root <==Show Directory ~ Example: shows the actual working directory, not the directory name of the link file itself [[email protected]~]# CD/var/mail <== Note,/var/Mail is a nexus file [[email protected] mail]# pwd/var/mail <==list Current working directory [[email protected] mail]# pwd-P/var/spool/mail <== What's going on? Is there a plus-P Difference many ~[[email protected] mail]# ls-LD/var/maillrwxrwxrwx1Root rootTenSep4  -: Wu/var/mail-spool/mail# See why you should know here? Because/var/mail is a link file, linked to/var/spool/Mail # So, plus pwdThe-P option will not display the data in the link file, but the correct full path is displayed!
mkdir (Create new directory)

If you want to create a new directory, then use mkdir (Make directory).

Grammar:

mkdir [-MP] Directory name

Options and Parameters:

    • -M: Configuration file Permissions Oh! Direct configuration, do not need to see the default permissions (umask) face ~
    • -P: help you to directly create the desired directory (including the top level directory) back up!

Example: Please try to create several new directories under/TMP to see:

[[Email protected] ~]# CD/tmp[[email protected] tmp]# mkdir test    <== Create a new directory for test [email protected ] tmp]# mkdir test1/test2/test3/test4mkdir:cannot Create directory ' test1/test2/test3/test4'  No Such file or directory       <==-P TEST1/TEST2/TEST3/TEST4

Add this-p option to help you create multi-level catalogs yourself!

Example: Creating a directory with permissions of Rwx--x--x

711-ldrwxr-xr-x  3 root  4096: testdrwxr-xr-x  3 root  4096: test1drwx--x--x  2 root  4096: Test2

The Permissions section above, if you do not add-m to force configuration properties, the system uses the default properties.

If we use-M, as in the example above we give M 711来 permission to give the new directory drwx--x--x.

RmDir (Delete empty directory)

Grammar:

rmdir [-p] Directory name

Options and Parameters:

    • - P: along with the previous level of "empty" directories are also deleted

Delete w3cschool.cc Directory

[Email protected] tmp]# rmdir w3cschool.cc/

Example: Delete the directory created in the MKDIR sample (/tmp)!

[Email protected] tmp]# ls-l <==See how many directories exist? DRWXR-xr-x3Root root4096Jul -  A: -TESTDRWXR-xr-x3Root root4096Jul -  A: -test1drwx--x--x2Root root4096Jul -  A: WuTest2[[email protected] tmp]# rmdir test<==can be deleted directly, no problem [[email protected] tmp]# rmdir test1<==because there is still content, so cannot delete! RmDir: ' Test1': Directory not empty[Email protected] tmp]# rmdir-p test1/test2/test3/test4[[email protected] tmp]# ls-L <==you see, the output in the bottom is missing from test and test1! DRWX--x--x2Root root4096Jul -  A: WuTest2

With the-P option, the TEST1/TEST2/TEST3/TEST4 can be deleted at once.

Note, however, that this rmdir can only delete empty directories, and you can use the RM command to delete non-empty directories.

CP (copy files or directories)

CP is a copy of files and directories.

Grammar:

[email protected] ~]# CP [-~]# CP [options] Source1 source2 source3 .... directory

Options and Parameters:

    • -A : as-pdr means, please refer to the following instructions for PDR. (common)

    • - D: If the source file is the attribute of the link file, the link file property is copied instead of the file itself;

    • - F: for Force, if the target file already exists and cannot be opened, then try again after removing it;

    • -I : if the target file (destination) already exists, the action will be asked (commonly used) when overwriting

    • - L: Create a link file for a hard link, rather than copying the file itself;

    • - P: Copy the past along with the properties of the file, rather than using the default properties (backup is often used);

    • - R: recursive continuous replication, used for the replication behavior of the directory; (Common)

    • - S: copy becomes the symbolic link file (symbolic link), i.e. "shortcut" files;

    • - u: upgrade destination If destination is older than source!

With root, copy the. BASHRC from the root directory to/TMP and name it BASHRC

[[email protected] ~]# cp ~/.bashrc/tmp/~]# cp-i ~/.bashrc/tmp/bashrccp:overwrite '/tmp/bashrc'  ? n  <==n not covered, Y is overlay
RM (remove files or directories)

Grammar:

RM [-fir] File or directory

Options and Parameters:

    • -F: Is the meaning of force, ignoring the nonexistent file, no warning message appears;
    • -I: Interactive mode that asks the user whether or not to move before deleting
    • -R: Pass it back! Most commonly used in directories are deleted! This is a very dangerous option!!!

Remove the BASHRC you just created in the example CP!

[[Email protected] tmp]# RM-i bashrcrm:remove regular file ' bashrc'? y

If you add the-I option, you will be asked to avoid deleting the wrong file name!

MV (move files and directories, or modify names)

Grammar:

[Email protected] ~]# MV [-~]# mv [options] Source1 source2 source3 .... directory

Options and Parameters:

    • -f:force mandatory Meaning, if the target file already exists, will not be asked and directly covered;
    • -I: If the target file (destination) already exists, you will be asked to overwrite it!
    • -U: If the target file already exists and source is newer, it will be upgraded (update)

Copy a file, create a directory, move the file to the directory

[[Email protected] ~]# CD/~/. BASHRC bashrc[[email protected] tmp]# mkdir mvtest[[email protected] tmp]# MV B ASHRC mvtest

Move a file to a directory, that's it!

Rename the name of the directory you just named Mvtest2

[Email protected] tmp]# MV Mvtest mvtest2
Linux file Content View

Use the following command to view the contents of a file in a Linux system:

    • Cat starts displaying file contents from the first line
    • The TAC begins with the last line, and you can see that the TAC is backwards written by cat!
    • NL display, the output line number!
    • More page-by-page display file contents
    • Less is similar to more, but better than more, he can page forward!
    • Head's just a few lines.
    • Tail only look at the tail a few lines

You can use the man [command] to view the documentation for each command, such as: Man CP.

Cat

Show file contents starting from first line

Grammar:

Cat [-abentv]

Options and Parameters:

    • -A: Equivalent to-vet's integration options, you can list some special characters rather than blanks;
    • -B: List line numbers, do line numbers for non-blank lines only, blank lines are not marked with line numbers!
    • -E: Displays the end of the break byte $;
    • -N: Print the travel number, along with the blank line will also have line number, and the option of-B is different;
    • -T: Displays the [tab] key in ^i;
    • -V: Lists some special characters that can't be seen

Check/etc/issue the contents of this file:

[Email protected] ~]# cat/etc/6.4  (Final) Kernel \ r on an \m
Tac

The TAC is just the opposite of the Cat command, and the file content starts from the last line, and you can see that the TAC is cat's backwards write! Such as:

[Email protected] ~]# tac/etc/6.4 (Final)
nl

Show line Numbers

Grammar:

NL [-BNW] File

Options and Parameters:

    • -B: Specifies the way the line number is specified, mainly in two ways:
      -B A: Indicates that the line number (similar to cat-n) is also listed, whether or not it is a blank line;
      -B T: If there is a blank line, the empty line does not list the row number (default);
    • -N: List the method of line number representation, there are three main kinds:
      -N LN: The line number is displayed at the left of the screen;
      -N RN: The line number is displayed on the very right of its own field and does not add 0;
      -N RZ: line number in its own field to the right of the display, and add 0;
    • -W: The number of digits occupied by the line number field.

Example one: Listing the contents of/etc/issue with NL

[[email protected] ~]# nl/etc/issue     1  6.4  (Final)     2  Kernel \ r on an \m
More

One page, one page, flip.

[Email protected] ~]# more/etc/ from man.conf. inch   from man-1.6d.... (omitted in the middle) .... --more--(%)  <== focus on this line! Your cursor will be here waiting for your order.

In the more of this program running, you have several keys that you can press:

    • Blank Key (space): Represents a page down;
    • Enter: Represents a downward turn "line";
    • /string: Represents the keyword in this display, down search for "string";
    • : F: Immediately display the file name and the number of lines currently displayed;
    • Q: The delegate immediately leaves more and no longer displays the contents of the file.
    • b or [Ctrl]-b: Represents a page back, but this action is only useful for files and useless for pipelines.
Less

Page by page, the following instance outputs the contents of the/etc/man.config file:

[Email protected] ~]# less/etc/ from man.conf. inch   from man-1.6d.... (omitted in middle   ) ....:<== Here can wait for you to enter the command!

The commands you can enter at run time are:

    • Blank key: Flip one Page down;
    • [PageDown]: flipping down one page;
    • [PageUp]: Flip up one page;
    • /string: Search down the function of "string";
    • Word string: Search for "string" function;
    • N: Repeat the previous search (with/or?) )
    • N: Reverse the repetition of the previous search (with/or?)! )
    • Q: Leave less this program;
Head

Remove the first few lines of the file

Grammar:

Options and Parameters:

    • -N: followed by a number that represents the meaning of a few lines
      [Email protected] ~]# Head/etc/man.config

      By default, the previous 10 lines are displayed! To display the first 20 rows, you need to do this:

       /etc/man.config
      Tail

      Remove the following lines of the file

      Grammar:

      Options and Parameters:

      • -N: followed by a number that represents the meaning of a few lines
      • -F: Indicates continuous detection of the following file name, wait until [ctrl]-c] to end the detection of tail
[Email protected] ~]# tail/etc/ /etc/man.config

Linux File and directory management

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.