Linux command Daquan----common file operation commands

Source: Internet
Author: User
Tags create directory readable uppercase letter disk usage

Lin Bingwen Evankaka original works. Reprint please specify the source Http://

Abstract: This article mainly describes the Linux command Daquan----common file Operation command, and attached an example


This command is often used to show what files are in the current directory, LS the most commonly used parameters are three:-a-l-F.
Description: The files on Linux are "." The files at the beginning are treated as hidden files by the system, only the LS command is not visible to them, and the hidden files are displayed with Ls-a in addition to the generic filenames.

Ls-l (this parameter is the lowercase of the letter L, not the number 1)
Note: This command can use the long format to display the contents of the file, if you need to see more detailed file information, it is necessary to use the Ls-l this command.

The first field that represents the properties of the file. Linux files are basically divided into three properties: readable (R), writable (W), executable (x). But here are 10 squares to add (when the actual program is implemented, it is actually 10 bit bits). The first small is a special representation, indicating a directory or a link file, and so on, d means a directory, such as drwx------; L represents a Nexus file, such as lrwxrwxrwx, or a horizontal "-" indicating that this is a file. The rest of the grid is one unit per 3 squares. Because Linux is a multi-user multitasking system, so a file may be used by many people at the same time, so we have to set the permissions of each file, its file permissions are arranged in the order of the location (in-rwxr-xr-x for example):
RWX (Owner) r-x (Group) r-x (Other)
This example indicates that the user is readable, writable and executable, the same group of users can be read, not writable, executable, other users can read, not write, can be executed. In addition, some of the execution of the program properties is not X, but s, which means that the user who executes the program may temporarily have the same power as the owner to execute the program. Generally appear in the system management such as instructions or programs, let the user execute, with root identity.
The second field, which indicates the number of files. If it is a file, then this number is naturally 1, if it is a directory, then it is the number of files in the directory.
The third field indicates the owner of the file or directory. If the user is currently in their home, then this column is probably the name of its account.
The fourth field indicates the group to which it belongs. Each user can have more than one group, but most of the users should belong to only one group, but only if the system administrator wants to give a user special permission, he or she may be given another group.
Column five, which indicates the file size. The file size is expressed in byte, and the empty directory is generally 1024byte, you can of course use other parameters to make the file display of different units, such as the use of ls–k is the KB to display the size of a file units, but generally we still mainly byte-based.
The sixth field indicates the date of creation. Expressed in "month, day, time" format, such as 15 5:46 for August 15 5:46 minutes.
The Seventh field indicates the file name. We can display the hidden file name with Ls–a.

Ls–f (Note that is the uppercase F)
Note: Using this parameter means adding more symbols for the file type after the file, such as * for executable,/for directory, @ for the Nexus file, because the-f parameter is used. But now basically all Linux distributions of LS have built-in the-f parameter, that is, we can see a variety of resolution symbols without entering this parameter.

Of course, the above commands can also be used together, as follows

Linux CD commands can be said to be the most basic Linux command statements, the other command statements to operate, are based on the use of the CD command. Toggle current directory to DirName

Note: Enter the system root directory, the above command after the execution of the LS command to see, the current directory has been to the system root directory

Cd.. or CD. //

Description: Enter the system root directory to use the CD. "Always back, you can reach the root directory

Cd.. /.. //
Description: Use the CD command to implement the parent directory into the parent directory of the current directory.

mkdir, RmDir
The mkdir command is used to create a new directory, RmDir is used to delete the established directory

mkdir Create directory, if the directory exists will be an error

RmDir Deleting a directory

Note that if the Linlin content is not empty

rmdir command can only delete empty folders, if the folder is not empty, it will not be deleted, it does not have the-f option, so your command is wrong.
To delete a non-empty folder, you can use the RM command, plus RF two options, such as:


The CP command is used to copy one or more source files or directories to a specified destination file or directory. It can copy a single source file into a specific file with a specified file name or a directory that already exists. The CP command also supports copying multiple files at once, and when copying multiple files at a time, the target file parameter must be a directory that already exists, or an error will occur.

-A: This parameter has the same effect as specifying the "-DPR" parameter;-D: When a symbolic connection is copied, the destination file or directory is also established as a symbolic connection, pointing to the original file or directory connected to the source file or directory;-F: Forcibly copying the file or directory, regardless of whether the destination file or directory already exists; I: Ask the user before overwriting the existing files;-L: Make a hard connection to the source file, not copy the file;-P: Preserves the properties of the source file or directory;-R/R: Recursively handles all files in the specified directory with subdirectories;-S: Establish symbolic connections to source files, not copy files; U: When this parameter is used, the file will only be copied when the source file is changed more than the target file is updated or the target file corresponding to the name does not exist;-S: Replaces the default suffix of the file with the specified suffix "SUFFIX" when backing up the file;-B: Backup the target file before overwriting the existing file target; V: Verbose display of actions performed by the command

The specific usage is: Cp–r source file Destination file (target). The parameter r is a copy of a subdirectory in a homologous file.

As below, copy the LINLIN.C under/home/linlin1 to/home/linlin2

As below, copy the LINLIN.C under/home/linlin1 to/home/linlin2 and rename it to hello.c

When we use the CP command to copy files under Linux, sometimes we will need to overwrite some files with the same name, when overwriting the file, we will have to keep pressing Y to determine the execution overwrite. The number of files is not much good, but if hundreds of estimates press Y to vomit blood, so toss a half-day summed up a method:

CP aaa/*/bbb Copy directory AAA under All to/BBB directory, at this time if the/BBB directory has the same name as the AAA file, you need to press Y to confirm and will skip the sub-directory under the AAA directory. Cp-r aaa/*/bbb This time still needs to press Y to confirm the operation, but does not ignore subdirectories. Cp-r-a aaa/*/bbb still needs to press Y to confirm the operation, and the AAA directory and subdirectories and file attributes are also passed to the/bbb. Cp-r-a aaa/*/bbb successful, no prompt pressed Y, passed the Directory property, did not skip the directory.

Copy all content under one folder to another folder

This command is used to delete files, and DOS under the RM (delete an empty directory) is a difference, we must pay attention to. There are three common parameters for RM commands:-i,-r,-f.

    -F,--force    ignores nonexistent files and never gives hints. -I    ,--interactive for interactive Delete-    R,-R,--recursive   instructs RM to delete all directories and subdirectories listed in the parameters recursively. -    V,--verbose    details the steps performed      --help     Display this help information and exit      --version  output version information and exit
rm–r Directory Name: This operation can be deleted with the subdirectories below this directory, functionally and rmdir similar.
Rm–f file name (directory name): This operation can be forced to delete.

Commonly used with R and F combination-RF

As below, interactively delete

Force Delete all

The function of this command is to move the directory or file, and the extended function is to rename the directory or file. When you use this command to move a directory, he will also remove it along with subdirectories under that directory.

-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 (destination) already exists, you will be asked to overwrite it! -U: If the target file already exists and source is newer, update-T  :--target-directory=directory move all source arguments to directory, That is, the target directory for the MV, which is used when moving multiple source files to a directory, when the destination directory is in front of the source file.
File renaming

MV Original file (directory) name a new file (directory) name.

File move

The du command can display the disk space occupied by the current directory, and the DF command can display the disk space remaining on the current disk. If the du command does not add any parameters, then the entire disk usage is returned, if the directory is added later, it is the directory on disk usage

To view the current directory and the size of the subdirectory:

View the entire disk size


There are three major features of cat:
1. Display the entire file at once. $ cat FileName
2. Create a file from the keyboard. $ cat > FileName
Only new files can be created and existing files cannot be edited.
3. Merge several files into one file: $cat file1 file2 > file

-N or--number starts at 1 for all output lines number-B or--number-nonblank and-n similar, except for blank lines that do not number-s or--squeeze-blank when encountering a blank line that has more than two consecutive lines, replace the blank line with a row-V or --show-nonprinting

Its usage is as follows:

Cat text displays text in this file;

Cat File1 File2 Displays the contents of the file1,file2 in sequence;

Cat File1 File2>file3 The contents of File1,file2, and then "redirects (>)" to file3 files.
"" "is a very interesting symbol, is the right redirection means that the left side of the result as input, and then entered into the File3 file. One thing to note here is that File3 is a file that has not existed before redirection, and if File3 is a file that already exists, then its own content is overwritten and becomes file1+file2 content. If there is no name for the file on the left and a filename on the right, for example:

Append the contents of the text.c and text1.c files to the text2.c after adding the line number (blank line not added).

Also, if you use the following instructions:
Cat File1>>file2: This will become the File1 file content "append" to the File2 file, while the content of the file2 still exist, this redirect 〉〉 than the common, you can use more.

Here are two instructions for displaying a generic text file. If a text file is too long for more than one screen, it is not ideal to use cat, so you can try more and less two instructions. The more instruction can make more than one page of the file temporarily stay on the screen, wait until you press any of the keys before continuing to display. and less in addition to more features, you can also use the arrow keys to scroll up or down the file, so you feel free to browse, read the article, less is a very good choice.

MORE: Option file name

+n starting from joys N rows
-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
-D hint "press space to continue, ' Q ' To quit (press SPACEBAR to continue, pressing Q key to exit)", disable the ringing function
-L ignores Ctrl+l (page break) characters
-p to page a file by clearing the window instead of scrolling, similar to the-C option
-S displays a contiguous number of empty rows as a row
-U Remove the line from the contents of the file

Search for the string Lin before each file display, and then start the display after the first two lines of the string


The less tool is also a tool for paging through files or other output, which is a very powerful tool for Linux to view the contents of a file. The usage of less is more resilient than more. In more, we have no way to turn front, can only look back, but if use less, you can use [PageUp] [PageDown] and other key functions to go to look at the file, more easily used to view the contents of a file! In addition, in less you can have more search function, not only to search down, but also to search upward.

Less option file name


-B < buffer size > Set buffer size-e  When the file display ends, automatically leaves the-F  force to open special files such as peripheral code, directory and binary file-G  only flag the last search keyword  -i Ignore search case-m displays a  percentage like the more command-n  displays the line number of each line-o < file name > save less output in the specified file-Q  does not use warning tone-S to  display continuous empty behavior line-S  the line too long will be out of the partial discard-x < number > the "tab" key is displayed as the specified number of spaces/strings: Search down the "string" function? String: Search Up "string" function N: Repeat previous search (with/or? related) N: Reverse repeat previous search (with Or? About) b  turn back one page D  back half page H  display Help interface Q  exit less command u  forward scroll half page y forward one  line space key scroll a line enter [PageDown]: [PageUp]:   Flip one page up

Exit Press Q

Search down the string containing Lin

There are three commands in the Linux system that can be used to view all the files, namely the cat, more, and less commands. Their access to the use of the file is also relatively simple command file name, but the three are different. command can display the entire file at once, if the file is relatively large, the use is not very convenient;
The 2.more command pauses the screen when it is full, and you can press the empty SPACEBAR to continue with the next screen, or press the Q key to stop the display.
The 3.less command can also be paged to display files, and the more command is the difference is that it supports scrolling up and down the screen, when the end of the browsing, as long as the less command prompt ":" Press the Q key.
In addition, in most cases the more and less commands match the pipe character to page out the content that needs to be displayed on the screen.


Also used to view the contents of a file

Tail syntax format:
tail [-f] [-c number |-N number |-M number |-B number |-K number] [File]
Tail [- R] [-N number] [file]
Instruction for use:
the tail command writes the file specified by the files parameter to standard output starting at the specified point. If no file is specified, standard input is used. The number variable specifies how many cells are written to standard output. The value of the number variable can be a positive or negative integer. If the value is preceded by A + (plus sign), the file is written to standard output starting at the number of cells specified at the beginning of the file. If the value is preceded by a-(minus sign), the file is written to standard output starting at the number of cells specified at the end of the file. If the value is preceded by A + (plus sign) or-(minus sign), the file is read from the unit number specified at the end of the file.
Main parameter:
-B number starts reading the specified file from the 512-byte block position represented by the number variable.
-C number reads the specified file starting at the byte position represented by the number variable.
-F If the input file is a regular file or if the filename parameter specifies a FIFO (first-in, in-out), then the tail command does not terminate after the last specified unit of the input file is copied, but continues to read and copy additional cells from the input file (when those cells are available). If the File parameter is not specified and the standard input is a pipe, the-f flag is ignored. The tail-f command can be used to monitor the growth of files that another process is writing.
-K number reads the specified file starting at the 1KB block position represented by the number variable.
-M number reads the specified file starting from the multibyte character position represented by the # variable. Use this flag to provide consistent results in a single-byte and double-byte character code set environment.
-N number reads the specified file from the first or last row position, represented by the sign of the number variable (+ or-or none), and is shifted by the Line No.
-R Displays the output in reverse order from the end of the file. The default value of the-R flag is to display the entire file in reverse order.

If the file is larger than 20,480 bytes, the-R flag displays only the last 20,480 bytes. The-r flag is valid only with the-n flag. Otherwise, it will be ignored.

The tail-f command can be used to monitor the growth of files that another process is writing. Especially useful when looking at the log, you update the log in real time, it is displayed in real time

PWD [--help][--version]
Description: Perform the PWD command to instantly know the absolute path name of your current working directory.

This command is used to clear the screen, it does not require any parameters

Used to find which line in a file matches a string.
Parameter description:

Such as. Display lines containing St, and their line numbers

Will not appear to show the ST's

Finding a directory recursively based on file content

# grep ' energywise ' *           #在当前目录搜索带 ' energywise ' line file # grep-r ' energywise ' *        #在当前目录及其子目录下搜索 ' energywise ' File # grep-l -R ' energywise ' *     #在当前目录及其子目录下搜索 ' energywise ' file, but does not display matching rows, only matching files are displayed

As follows

grep function is very powerful, here is only a brief explanation, interested students come down to study it!


$ find  -name "*.txt"-print used to find all the ' *.txt ' files in the current directory and subdirectories $ find  -name "[a-z]*"-print used in the current directory and subdirectories to find the file name beginning with an uppercase letter $ find/etc-name "host*"-print find files in the/etc directory with file names beginning with host $find  -name "[a-z][a-z][0--9][0--9].txt"-print find files in the current directory The name starts with two lowercase letters followed by two digits, and finally the. txt file
For example, the current directory looks for files that end in. c

1. Look for a file named "" in a directory find/home/lijiajia/-name 2. Find files in file names that contain a character (such as "Elm") find/home/lijiajia/-name ' *elm* ' find/home/lijiajia/-name ' elm* ' find/home/lijiajia/-name ' *elm ' 3. Query according to the characteristics of the file find/home/lijiajia/-amin-10 #查找在 Files accessed in the last 10 minutes of the system find/home/lijiajia/-atime-2 #查找在系统中最后48小时访问的文件find/home/lijiajia/-empty #查找在系统中为空的文件或者 Folder find/home/lijiajia/-group Cat # Find files that belong to Groupcat in the system (tried, the command is incorrect.) find/home/lijiajia/-mmin-5 # Find files modified in the last 5 minutes of the system find/home/lijiajia/-mtime-1 modified files in #查找在系统中最后24 hours fin d/home/lijiajia/-nouser #查找在系统中属于作废用户的文件 (don't understand what that means) find/home/lijiajia/-amin #查找在系统中最后10分钟访问的文件find /home/ftp/pub-user Lijiajia #查找在系统中属于lijiajia这个用户的文件 (PS: All the above is done under the/home/lijiajia/folder) 4. Find a file using a mixed Find method Find/tmp-siz E +10000000c-and-mtime +2 #查找 a file larger than 10000000 bytes in the/tmp directory and modified within 48 hours Find/tmp-user Tom-or-user George #查找/T The MP directory belongs to Tom or George of the two user files Find/tmp! -USR Fred #查找/tmp DirectoryFiles that do not belong to Fred 5. Find and display files find/home/lijiajia/-name ' '-ls #在目录下查找名为 "" files and display information about these files 

Copyright NOTICE: This article for Bo Master Lin Bingwen Evankaka original article, reproduced please indicate the source Http://

Linux command Daquan----common file operation commands

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: 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.