Commands that Linux must remember

Source: Internet
Author: User
Tags log log parent directory touch command

Write in front:

1, "You must know about the Linux file directory operation of the 12 common commands" is the landlord collected on the Linux file directory operations most commonly used commands, including the file or directory of new, copy, move, delete, view, etc., is a developer to operate the Linux system common commands, so you must know.

2, "You must know about the Linux file directory operation of the 12 common commands" for beginners, for the Linux great God, please bypass, but welcome to discuss learning together!

3, this collection, thank you for sharing from http://www.cnblogs.com/peida/archive/2012/10/23/2734829.html, if you need more detailed information please pay attention to.

Let's start studying together:

1, mkdir command

MkDir is used to create a directory of the specified name, requiring the user who created the directory to have write permissions in the current directory, and the specified directory name cannot be a directory that is already in the current directory.

(1) command format: mkdir [options] Directory

(2) Command function: Create a folder or directory named after the specified file name in the specified location, the user who is creating the folder or directory must have write permission to the parent folder of the folder being created, and the same directory cannot have duplicate names.

(3) Command parameters:

-M,--mode to set permissions < mode > (similar to chmod) instead of rwxrwxrwx

-P,--Parents can be the name of a path, if some directory in the path does not exist, and after this option, the system will automatically set up those directories that do not already exist, that is, multiple directories can be built at one time

-V,--verbose displays information when creating the current new directory or file, such as "directory Test6 created"

(4) Command example:

① Creating an empty directory

mkdir lijl_bj1

② Creating multiple directories recursively

Mkdir–p./lijl_bj1/test (./indicates the current directory,.. /indicates the parent directory,/represents the root directory)

③ creating a directory with permissions of 777

Mkdir–m 777 test777

④ Creating a new directory display information

Mkdir–v Test

⑤ Creating a directory structure for a project

Mkdir–vp

src/{com/sitech/{admin/{entity/,dao/,service,controller},common/util/},config/}

(5) Precautions:

The last instance ⑤ the order is required, that is, the-v–p operation is represented at-VP, and if-PV in the preceding statement, the-VP directory is created by default.

2, ls command

The missing save is used to print a list of the current directory, and if LS specifies a different directory, then the list of files and folders for the specified directory is displayed. The LS command not only allows you to view files contained in a Linux folder, but also to view file permissions (including directories, folders, files), view directory information, and more.

(1) command format: LS [options] [directory name]

(2) Command function: List all subdirectories and files in the target directory

(3) Command parameters:

-A, list all files in the directory, including the implied files that begin with the.

-C mate-lt sorted according to CTime and based on CTime (the last time the file state was changed)

Mate-L display CTime but sort by name

-L lists, in addition to the file name, the permissions, owner, file size, and more

-T is sorted in the order in which the files are modified

-U with-LT show access time and sort by access time

Match-L displays access time, but sorts by name

-V Sort by version

-X list items line by row, not column by bar

-x Sort by extension

-1 list only one file per line

-F can add a "/" to a subdirectory when it is displayed, and "*" After the file indicates that it is an executable program

(4) Common examples:

① listing the details of all files and directories under the/lijl_bj1/com/src folder

Enter Lijl_bj1 directory, ls–l–r (or-LR) src

② lists all the file directories in the current directory that begin with "s",

Ls–l s*

③ lists only subdirectories under the file:

Ls-f./SRC |grep/$ lists the subdirectories below the SRC directory

Results: com/,config/

④ list all the files and directories under the current working directory; add "/" to the name and "*" after the file name.

Ls–af

3, cd command

The CD command is the most basic command in Linux, and many other commands are built on top of it.

(1) command format: CD [directory name]

(2) Command function: Switch the current directory to DirName

(3) Common examples:

① into the system root directory

CD/

② go to the top level directory of the current directory: CD. or CD. //

③ Enter the current user's home directory

CD or cd~

④ jump to the specified directory

Cd/echncms/lijl_bj1 (root directory entry)

Jump to the specified directory, starting at the root, with a "/" in front of the directory name, and a subdirectory of the current directory to write the name.

⑤ return to the directory where it was before entering this directory

cd–

⑥ Use the parameters of the previous command as CD parameters

CD!$

(4) Precautions:

Jump to the specified directory, starting at the root, with "/" in front of the directory name, and the subdirectory of the current directory write name

4, pwd command

Use the "pwd" command to view the full path of the "Current working directory", each time you operate in the terminal, there will be a current working directory, when you are not sure of the current location, you can use the PWD command to view the current directory and the exact location of the file system.

(1) command format: pwd [options]

(2) Command function: View the full path of "Current working directory"

Common parameters: Generally do not use any parameters, if the directory is a link, pwd–p display the actual path, rather than using the link path. If the current path is deleted, the PWD display is still the original path.

5, RM command

The function of this command is to delete one or more files or directories in a directory, it can also be a directory and all of the following files and directories are deleted, for the linked file, just delete the link, the original file remains unchanged.

(1) command format: RM [options] File

(2) command function: Delete one or more files or directories in the specified directory, if you do not use the-r option, RM does not delete the directory, and if you use RM to delete files, you can usually still restore the files to their original status.

(3) Command parameters:

-F,--force ignores files that do not exist and never gives hints

-I,--interactive for interactive deletion

-r,-r,--Recursive instructs RM to recursively delete all directories and subdirectories listed in the parameters

-V,--verbose detailed display of the steps performed

(4) Command example:

① Delete file, the system will first ask whether to delete, Rm–i Log.log, after entering y will be deleted

② forcibly delete file, the system no longer prompts Rm–f Log1.log

③ Delete any. log files, ask Rm–i *.log before deleting

④ Delete all files in Test1 subdirectory and subdirectories rm–r test

⑤ Delete all files in the Test2 subdirectory and subdirectories without asking

RM–RF test2 (Common)

⑥ deleting files that begin with-f

RM---F

⑦ Custom Recycle Bin features:

Myrm () {d=/tmp/$ (date +%y%m%d%h%m%s), Mkdir–p $D, MV "[email protected]" $D && echo "moved to $D OK";}

6, RmDir command

RmDir is a commonly used command, the function of which is to delete an empty directory before a directory is deleted must be empty. Note (The Rm–r dir command can be used in place of rmdir, but there is a significant risk) that the parent directory must also have write permissions to delete a directory.

(1) command format: rmdir [options] ... Directory...

(2) Command function: This command removes one or more subdirectory entries from a directory and must also have write access to the parent directory when deleting a directory.

(3) Command parameters:

-p recursively deletes the directory dirname, when a subdirectory is deleted and its parent directory is empty, it is also deleted, and the system displays the appropriate information on the standard output if the entire path is deleted or some path is reserved for some reason.

-v,--verbose show instruction execution process

(4) Command example:

①rmdir cannot delete a non-empty directory rmdir lijl_bj1, the command cannot be deleted directly from the non-directory

②rmdir–p when a subdirectory is deleted so that it also becomes an empty directory, the drop

7, MV Command

The MV command is the abbreviation for move, which can be used to move files or rename files (rename), which is a common command under Linux, often used to back up files or directories.

(1) command format: MV [option] source file or directory destination file or directory

(2) Command function: Depending on the second parameter type in the MV command (target file or target directory), the MV command renames the file or moves it to a new directory. When the second parameter type is a file, the MV command completes the file rename, at which point the source file can only have one (or the source directory name), 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 it, deletes the original file, and the link to the file is lost.

(3) Command parameters:

-B: Overwrite the previous backup if you need to overwrite the file

-f:force mandatory Meaning, if the target file already exists, will not be asked to directly overwrite

-I: If the target file already exists, you will be asked to overwrite it.

-T:--target–directory, which specifies the target directory for MV, which is used when moving multiple source files to a directory, where the target directory is before the source file.

(4) Command example:

① File name change: Rename file Test.log to Test1.txt

MV Test.log Test1.txt

② moving files: Move the Test1.log file to Test3

MV Test1.log Test3

③ moving the file log1.txt,log2.txt,log3.txt to the directory test

MV Log1.txt log2.txt log3.txt test

Mv–t/opt/soft/test/log1.txt Log2.txt Log3.txt

④ Rename the file file1 to File2, and if file2 already exists, ask whether to overwrite

Mv–i Log1.txt Log2.txt

⑤ file file1 renamed to File2, instant file2 exists, is also directly covered out

Mv–f Log3.txt Log2.txt

Log3.txt content directly covers the log2.txt content,-f This is a dangerous option, use time must keep a clear mind, generally it is best not to add it.

⑥ the move of the directory

MV Dir1 Dir2

If the directory Dir2 does not exist, rename the directory Dir1 to Dir2 or move Dir1 to Dir2

⑦ move all files under the current folder to the top level directory

MV *.. /

⑧ moving files in one subdirectory of the current directory to another subdirectory

MV Test3/*.txt TEST5

⑨ file is overwritten before making a simple backup, preceded by parameter-B

MV Log1.txt–b Log2.txt

Note:-B does not accept parameters, and MV will read the environment variable Version_control as a backup strategy. --backup This option specifies a total of four backup strategies if the target file is present:

1.control=none or off: not backed up.

2.control=numbered or T: Digitally numbered backup

3.control=existing or nil: if there is a digitally numbered backup, continue numbering the backup M+1...N:

There is a digitally numbered file log2.txt.~1~ before performing the MV operation, then execution will produce log2.txt~2~, and so on. If you don't have a digitally numbered file before, use the simple backup described below.

4.control=simple or never: Use simple backup: A simple backup is made before being overwritten, a simple backup can only have one copy, and a simple backup will be overwritten when it is overwritten again.

MV does not create a new directory, only the source directory or source files can be moved into the created directory, if the destination file or folder does not exist, will only be renamed, rather than moving files or directories

8, CP command

The CP command is used to copy files or directories and is one of the most commonly used commands in a Linux system. In general, the Shell will set an alias, when copying the file at the command line, if the target file already exists, you will be asked whether to overwrite, regardless of whether you use the parameter-I, but if the CP is executed in the shell script, no parameter-I will not ask whether to overwrite, This means that the command line and shell scripts perform somewhat differently.

(1) command format: CP [Options] ... [-t] source purpose

or CP [Options] ... Source... Directory

or CP [options] ...-t directory source ...

(2) Command function: Copy the source file to the target file, or copy multiple source files to the target file

(3) Command parameters:

-I,--Interactive before overwriting (invalidates the previous-n option)

-N,--no-clobber do not overwrite existing files (invalidates the previous-i option)

-r,-r,--recursive copy all items in directory and directory

(4) Command example:

① copy a single file to the destination directory, the file does not exist in the destination file

CP Log.log TEST5

When the-a parameter is not present, the time of the two files is different, and the time of the two files is consistent when the-a parameter is brought.

When the ② destination file exists, it asks whether to overwrite it.

CP Log.log TEST5

When the target file is present, it asks whether to overwrite it, because the CP is an alias of Cp–i, when the target file exists, the-f flag is added immediately, and the overwrite is also asked.

③ copying the entire directory

Cp–a test3 Test5

Note that the destination directory is not the same, and the destination directory exists, and the entire source directory is copied to the target directory.

④ copy of Log.log create a link file Log_link.log

Cp–s Log.log Log_link.log

⑤ that Log_link.log is caused by the-s parameter, the establishment is a "shortcut", so you will see at the far right of the file, it will show where the file is linked to.

9, Touch command

Linux touch commands are not commonly used, and are generally used when using make, to modify file timestamps, or to create a new file that does not exist.

(1) command format: Touch [options] ... File...

(2) Command function: The touch command parameter can change the date and time of the document or directory, including the access time and the time of the change.

(3) Command parameters:

-A or--time = Atime--time =access or--time=use only change access time

-C or--no-create do not create any documents

-D uses the specified datetime instead of the current time

-M or--time=mtime or--time=modify only change the change time

-R Sets the date and time of the specified document or directory to the same date and time as the reference document and directory

-T uses the specified date instead of the current time

(4) Common examples:

① Create a nonexistent file:

Touch Log2015.log Log2016.log

If Log2014.log does not exist, the file is not created

② update Log.log the same time and Log2015.log timestamp

Touch–r Log2015.log Log.log

③ time stamp of the set file

Touch–t 201505191357.50 Log.log

-T time uses the value specified as a new value for the corresponding time stamp of the specified file. The time specified here is a decimal number in the following form:

[[Cc]yy] mmddhhmm[. SS]

Here, CC is the first two digits of the number of years, that is, "century"; yy is the last two digits of the number of years, that is, the number of years in a century. If the value of CC is not given, touch will limit the number of years Ccyy to 1969--2068. MM for the number of months, DD for the day will be the number of years Ccyy limited to 1969--2068 within. MM is the number of months, DD is the number of days, HH is the number of hours (several), MM is the number of minutes, SS is the number of seconds. The set range of seconds here is 0--61, which can handle leap seconds. These numbers consist of a time in the time zone specified by the environment variable TZ. Due to system limitations, the time earlier than January 1, 1970 was wrong.

The Cat command

The purpose of the cat command is to connect files or standard input and print, which is commonly used to display the contents of a file, or to connect several files to display, or to read from a standard input and display it, often in conjunction with redirection symbols.

(1) command format: Cat [Options] [file] ...

(2) Command function: Cat has three major functions:

① Show entire file at once: Cat filename

② create a file from the keyboard: Cat>filename can only create new files, cannot edit existing files

③ merge several files into one file: Cat file1 file2>file

(3) Command parameters:

-a,--show-all equivalent to-vet

-B,--Number-nonblank to non-null output line number

-e equivalent to –ve

-e,--show-ends show $ at end of each line

-N,--number for all lines of output, numbering the number of rows for all outputs starting from 1

-S,--squeeze-blank a blank line that has more than two lines in a row and is substituted for a row

-T vs.-VT equivalence

-T,--show-tabs to display the ^i character as a

(4) Examples of use:

① the file contents of Log2012.log with line number after input log2013.log this file

Cat–n Log2012.log Log2013.log

② Append the contents of the Log2012.log and Log2013.log files to the log.log after adding the line number (blank line).

Cat–b Log2012.log Log2013.log Log.log

③ the file contents of Log2012.log with line number after input log.log this file

Cat–n Log2012.log>log.log

④ Note that the TAC is a reverse listing, the TAC is the cat in turn, so it functions as cat instead, the cat is displayed continuously on the screen from the first line to the last row, while the TAC is displayed on the screen in reverse from the last line to the first line.

One, more commands

More command function similar to the CAT,CAT command is the entire contents of the file from top to bottom display on the screen, more will be a page by page of the display for users to read pages, and the most basic instruction is to press the blank key (space) on the next page, press the B key will go back to a page, There is also the ability to search for strings, and the more command reads the file backwards from the front, so it loads the entire file at startup.

(1) command format:

More [-dlfpcsu][-num][+/pattern][+linenum][file ...]

(2) Command function: More commands and Cat's function is to view the contents of the file, but the difference is that more can be viewed by the page to view the contents of the file, but also support the direct jump switch and other functions.

(3) Command parameters:

+n starting from Nth line

-N defines the screen size as n rows

+/pattern searches for the string before each file is displayed, and then starts from the first two lines of the string

-C clear the screen from the top and then display

-p to page a file by clearing the window instead of scrolling, similar to the-C option

(4) Common Operation command:

Enter down n rows, need to be defined, default to 1 rows

Ctrl+f, space bar scroll down one screen

Ctrl+b Back to previous screen

= Output the line number of the current line

: F output File name and line number of the current line

V Call VI Editor

Q Exit More

(5) Common examples:

① Displays the contents of the file from line 3rd

More +3 Log2012.log

② finds the first line from a file that appears with the "Day3" string, and displays the output from the first two lines at that point

More +/day3 Log2012.log

③ set the number of rows per screen displayed

More-5 Log2012.log

The bottom shows that the screen shows the proportion of the total number of files, press CTRL+F or SPACEBAR will display the next screen 5 content, the percentage will also change.

④ column A directory of files, because the content is too much, we should learn to use more to page, this has to be combined with the pipeline |

Ls–l | More-5

5 files per page, press CTRL+F or SPACEBAR to display the next 5 file information

Tail command.

The tail command writes the file to the standard output stream starting at the specified point, using the-f option of the tail command to easily see the log file being changed, TAIL–F filename will display the most up-to-date contents of the filename on the screen, and not only refresh, so you can view the latest file content. (often used to view log files on the server)

(1) command format: tail [necessary parameters] [selection parameters] [file]

(2) Command function:

Used to display the end of the specified file, do not specify a file, as input information for processing, often view log files.

(3) Command parameters:

-F Loop Read

-Q does not display processing information

-V system that is detailed processing information

Number of-c< > bytes displayed

-n< lines > Display rows

(4) Command example:

① Show file End content

Tail–n 5 Log2014.log Display the contents of the last 5 lines of the file

② loop to view the contents of a file

Tail-f Test.log

③ displaying files starting from line 5th

Tail–n +5 Log2014.log

OK, friends, here, our 12 commonly used commands are finished, there is an interest to take a closer look, you will learn a lot of things!

Commands that Linux must remember

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.