Linux commands-common file operation commands

Source: Internet
Author: User

Linux commands-common file operation commands

Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka

Abstract: This article focuses on Linux commands-common file operation commands with examples

Ls

This command is often used to display the files in the current directory. The most common parameters of ls include-a-l-F.
Ls-
Note: For files on Linux, "files starting with" ls "are viewed as hidden files by the system and cannot be seen only by using the ls command. In addition to displaying common file names," ls-a "can be used to display hidden files.

 

Ls-l (this parameter is the lowercase letter of L, not the number 1)
Note: This command can display the file content in a long format. If you need to view more detailed file information, you need to use the ls-l command.

 

 

The first column indicates the attributes of the file. Linux Files are basically divided into three attributes: readable (r), writable (w), and executable (x ). However, there are ten grids that can be added here (10 bits are actually implemented in a specific program ). The first cell is a special cell, indicating a directory or a link file, etc. d indicates a directory, for example, drwx ------; l indicates a link file, for example, lrwxrwxrwx; if it is a horizontal, indicates that this is a file. The remaining grids are in the unit of 3. Because Linux is a multi-user multi-task system, a file may be used by many people at the same time, so we must set the permissions for each file, the permission positions of the files are arranged in the following order (-rwxr-xr-x is used as an example ):
Rwx (Owner) r-x (Group) r-x (Other)
In this example, the permissions are as follows: users are readable, writable, and executable; users in the same group are readable, writable, and executable; other users are readable, writable, and executable. In addition, the execution of some program properties is not X, but S, which indicates that the user who executes the program can temporarily execute the program with the same power as the owner. Generally, commands or programs such as System Management enable the user to have the root identity during execution.
The second column indicates the number of files. If it is a file, the number is naturally 1. If it is a directory, the number 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 his/her Home, this column is probably the name of his/her account.
The fourth column, indicating the group to which it belongs ). Each user can have more than one group, but most users should belong to only one group. Only when the system administrator wants to grant special permissions to a user, to another group.
The fifth column indicates the file size. The file size is represented by byte, while the empty directory is usually 1024 bytes. Of course, you can use other parameters to make the file display unit different, for example, if ls-k is used, kb is used to display the size unit of a file. However, we generally use byte as the main unit.
The sixth column indicates the creation date. In the format of "month, day, and time", for example, Aug 15 indicates on January 1, August 15.
The seventh column indicates the file name. We can use ls-a to display hidden file names.

 

Ls-F (Note: F is in upper case)
Note: this parameter is used to add symbols indicating the file type after a file. For example, * Indicates executable,/indicates directory, and @ indicates linked files, this is because the-F parameter is used. But now, basically all Linux releases have built-in the-F parameter for ls. That is to say, we can see all kinds of resolution symbols without entering this parameter.

Of course, the above commands can also be used together, as shown below:

 

Cd
The Linux cd command is the most basic command statement in Linux. Other command statements are created on the cd command. Switch the current directory to dirName
Cd/

Note: Go to the system root directory. Take a look at the ls command after the above command is executed. The current directory is already in the system root directory.

 


Cd... or cd ..//

 

Note: To enter the root directory of the system, you can use "cd .." to return to the root directory.

Cd ../..//
Note: run the cd command to access the parent directory of the current directory.

Mkdir, rmdir
The mkdir command is used to create a new directory, and rmdir is used to delete an existing Directory.

Mkdir: Creates a directory. If the directory exists, an error is returned.

 

Rmdir Delete directory

 

Note: If the linlin content is not empty

The rmdir command can only delete empty folders. If the folder is not empty, it cannot be deleted, and it does not have the-f option. Therefore, your command is wrong.
To delete a non-empty folder, run the rm command and add two options:

Cp

The cp command is used to copy one or more source files or directories to the specified target file or directory. It can copy a single source file into a specific file with the specified file name or an existing Directory. The cp command also supports copying multiple files at the same time. When copying multiple files at a time, the target file parameter must be an existing Directory; otherwise, an error occurs.

 

-A: the effect of this parameter is the same as that of the "-dpR" parameter specified at the same time.-d: When the symbolic connection is copied, the target file or directory is also established as a symbolic connection, and point to the original file or directory connected to the source file or directory;-f: forcibly copy the file or directory, whether or not the target file or directory already exists;-I: ask the user before overwriting an existing file;-l: Establish a hard connection to the source file instead of copying the file;-p: retain the properties of the source file or directory;-R/r: recursive processing, process all files in the specified directory with subdirectories;-s: establishes a symbolic connection to the source file, rather than copying the file;-u: after this parameter is used, the file will be copied only when the source file is updated later than the target file or the target file with the corresponding names does not exist.-S: During file backup, use the specified SUFFIX "SUFFIX" to replace the default SUFFIX of the file.-B: Back up the target file before overwriting the existing file target.-v: displays the commands executed in detail.

 

The specific usage is: cp-r source file (source) target file (target ). The r parameter is copied together with the subdirectory in the source file.

 

Copy linlin. c Under/home/linlin1 to/home/linlin2 as follows

Copy linlin. c Under/home/linlin1 to/home/linlin2 and change it to hello. c.

When we use the cp command in Linux to copy a file, sometimes we need to overwrite some files of the same name. When we overwrite the file, there will be a prompt: we need to constantly press Y to determine the execution overwrite. It would be nice to have a small number of files, but if hundreds of files are estimated to have vomiting blood by Y, I summarized a method for a long time:

 

Copy all files under the aaa directory under the cp aaa/*/bbb directory to the/bbb directory. If the/bbb directory contains files with the same name as aaa, press Y to confirm and skip the sub-directories in the aaa directory. Cp-r aaa/*/bbb still needs to be confirmed by Y this time, but the subdirectory is not ignored. Cp-r-a aaa/*/bbb still needs to be confirmed by Y, and the aaa directory and sub-directory and file attributes are also passed to/bbb. Cp-r-a aaa/*/bbb is successful, no prompt is displayed for pressing Y, the directory attribute is passed, and the directory is not skipped.

Copy all contents in one folder to another folder.

 

 

Rm
This command is used to delete files. It is different from the rm (delete an empty directory) in dos. Common Parameters of the rm command include-I,-r, and-f.
  

-F, -- force ignores non-existing files and never gives a prompt. -I, -- interactive deletion-r,-R, and -- recursive indicate that rm will recursively delete all directories and subdirectories listed in the parameter. -V, -- verbose detailed display steps -- help displays this help information and exits -- version outputs version information and exits
Rm-r directory name: this operation can be deleted along with the subdirectories under this directory. The function is similar to rmdir.
Rm-f file name (directory name): this operation can be forcibly deleted.

Generally, r and f combination-rf are used together.

 

Interactive Deletion

Force delete all


 

Mv
The function of this command is to move a directory or file. The extended function is to rename a directory or file. When you use this command to move a directory, it will be removed along with the subdirectories under the directory.

 

-B: If you need to overwrite the file, back up the file before overwriting. -F: force indicates that if the target file already exists, the file will not be directly overwritten.-I: If the destination file already exists, the file will be overwritten! -U: update (update)-t: -- target-directory = DIRECTORY move all source arguments into DIRECTORY will only be updated if the target file already exists and the SOURCE is newer, specifies the target directory of the music video. This option is applicable when multiple source files are moved to the same directory.
File rename

 

The original mv file (directory) name is the new file (directory) Name.

File Movement


Du, df
The du command displays the disk space occupied by the current directory, and the df command displays the remaining disk space of the current disk. If the du command does not add any parameters, the system returns the usage of the entire disk. If a directory is added to the disk, This is the usage of the directory on the disk.

View the size of the current directory and subdirectory:

View the entire disk size

 


Cat

 

Cat has three main functions:
1. display the entire file at a time. $ 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
Parameters:

-N or -- number indicates the number of all output rows starting from 1-B or -- number-nonblank is similar to-n, only when the blank row is not numbered-s or -- squeeze-blank, if there is a blank row with more than two consecutive rows, it is replaced by a blank row-v or -- show-nonprinting

 

Its usage is as follows:

Cat text displays the text file;

Cat file1 file2 displays file1 and file2 in sequence;

 

Cat file1 file2> file3 combines the content of file1 and file2, and then "Redirect (>)" to the file3 file.
">" Is a very interesting symbol. It means to redirect to the right, that is, to treat the result on the left as an input and then input it to the file file3. Note that file3 is a file that has not existed before redirection. If file3 is an existing file, its content is overwritten and changed to file1 + file2. If there is no file name on the left, and there is a file name on the right, for example:

 

Add the row number to the file content of text. c and text1.c (blank rows are not added) and then add the content to text2.c.

In addition, if you use the following command:
Cat file1> file2: This will be used to "APPEND" the content of file1 to the end of file2, while the content of file2 still exists. This type of redirection character> is more common than>, it can be used more.

 

More, less
These are two commands for displaying common text files. If a text file is too long to display more than one screen, it is not ideal to use cat, you can try the more and less commands. The More command can temporarily hold a file with More than one page on the screen. It will not be displayed until you press any key. In addition to more functions, less can also use the direction keys to scroll up or down the file, so you can browse freely. when reading the article, less is a very good choice.

More: Option file name

+ N is displayed from rows n of bytes.
-N defines the screen size as n rows
+/Pattern Search for the string (pattern) before each file is displayed, and then the string is displayed after the first two lines.
-C clear the screen from the top and then display
-D: Press space to continue, 'q' to quit (Press the space key to continue, Press the q key to exit )."
-L ignore Ctrl + l (page feed) characters
-P clears the window instead of scrolling the screen to change the page of the file, similar to the-c option.
-S: displays multiple consecutive empty rows as one row.
-U remove the offline lines in the file content

Search for the string lin before each file is displayed, and then display it after the first two lines of the string.

Less

The less tool is also a paging display tool for files or other outputs. It should be said that it is an orthodox linux tool for viewing file content and has extremely powerful functions. The usage of less is more flexible than that of more. At the time of more, we could not flip forward, but we could only look at it later. However, if less is used, you can use the [pageup] [pagedown] and other buttons to access and view the file, making it easier to view the content of a file! In addition, you can have more search functions in less, not only to search down, but also to search up.

Less option file name

Option

 

-B <buffer size> set the buffer size-e when the file is displayed, the system automatically leaves-f to force the special file to open, for example, the peripheral device code, directory, and binary file-g only mark the last search keyword-I ignore the case when the search-m shows the percentage similar to the more command-N shows the row number-o <File Name> Save less output content in the specified file-Q does not use warning sound-s to display consecutive null behavior a row-S will discard the excess part for a long time-x <number> display the "tab" key as a specified number space/string: search down the "string" function? String: the function of searching "string" up n: Repeat the previous search (and/or? Related) N: Repeat the previous search (and/or? () B flip one page backward d flip half page h display help interface Q exit less command u scroll forward half page y scroll forward one line space key scroll one line enter key scroll one page [pagedown]: scroll down a page [pageup]: Scroll up a page

 

Exit by Q

Search down strings containing lin

 

 

 

In Linux, there are three commands for viewing all the files: cat, more, and less. They use command file names in a simple way, but there are differences between the three.
1. The cat command can display the entire file at a time. If the file is large, it is not very convenient to use;
2. The more command will pause the screen when it is full. Press space to continue displaying the next screen or press the Q key to stop the screen.
3. the less command can also display files by page. The difference with the more command is that it supports the up and down keys to scroll the screen. When the browsing ends, you only need to prompt ": press the Q key.
In addition, in most cases, the more and less commands work with MPs queues to output the content that needs to be displayed on the screen by page.

Tail

It is also used to View File Content

Tail syntax format:
Tail [-f] [-c Number |-n Number |-m Number |-B Number |-k Number] [File]
Or
Tail [-r] [-n Number] [File]
Instructions for use:
The tail command writes the File specified by the File parameter to the standard output starting from the specified point. If no file is specified, standard input is used. The Number variable specifies the Number of units to be written to the 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 the standard output starting with the number of units specified at the beginning of the file. If there is a minus sign before the value, the file is written to the standard output starting from the number of units specified at the end of the file. If no + (plus sign) or-(minus sign) exists before the value, read the file starting from the specified unit number at the end of the file.
Main parameters:
-B Number reads the specified file from the location of the 512-byte block indicated by the Number variable.
-C Number reads the specified file from the byte location indicated by the Number variable.
-F if the input File is a regular File or if the File parameter specifies FIFO (first-in-first-out), the tail command will not terminate after the last specified unit of the input File is copied, instead, read and copy additional units from the input file (when these units 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 being written by another process.
-K Number reads the specified file from the 1KB block location indicated by the Number variable.
-M Number reads the specified file from the Multi-byte character position indicated by the Number variable. Use this flag to provide consistent results in the single-byte and double-byte character code set environments.
-N Number reads the specified file from the first or last row. The position is indicated by the Number variable symbol (+ or-or none) and is displaced by the Number of the row.
-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 only displays the last 20,480 bytes. The-r flag is valid only when it is used together with the-n flag. Otherwise, it will be ignored.

The tail-f command can be used to monitor the growth of files being written by another process. It is especially useful when you view logs. If you update logs in real time, they are displayed in real time.

 

Pwd
Pwd [-- help] [-- version]
Note: When executing the pwd command, you can immediately find the absolute path name of your current working directory.

 

Clear
This command is used to clear the screen without any parameters.

Grep
Used to find the line that matches the string in the file.
Parameter description:

 

-A: Search for data using text files in binary files-c: count the number of times that 'searchstring' is found-I: case-insensitive, so the case is treated as the same-n: by the way, the output line number-v is selected in reverse direction, that is, the line without the 'searchstring' content is displayed!
For example. Display the row containing the st and Its row number

 

 

If no st is displayed

 

 


 

Recursively search directories Based on the file content

 

# Grep 'energywise '* # search for files with the 'energewid' line in the current directory # grep-r 'energewid' * # search for files with the 'energewid' line in the current directory and Its subdirectories # grep-l-r 'energywis' * # search for the 'energywis' row file in the current directory and Its subdirectories, however, no matching lines are displayed. Only matching files are displayed.

As follows:

 

The grep function is very powerful. Here is a brief description. If you are interested, study it yourself!

Find

$ Find-name "*. txt "-print is used to find all '*. txt 'file in the current directory and subdirectory $ find-name "[A-Z] *"-print is used in the current directory and subdirectory to find files whose names start with an uppercase letter $ find/etc -name "host *"-print: In the/etc directory, find the file whose file name starts with host $ find-name "[a-z?=a-z=%0--9%0--9%.txt"-print your file.
For example, search for files ending with. c in the current directory.

 

 

1. find the name "elm. cc file find/home/lijiajia/-name elm. cc 2. find a character in the file name (such as "elm ") find/home/lijiajia/-name '* elm *' find/home/lijiajia/-name 'elm * 'find/home/lijiajia/-name' * elm '3. search for find/home/lijiajia/-amin-10 Based on the file characteristics # find the file found/home/lijiajia/-atime-2 accessed in the last 10 minutes of the system # search for the file in find/home/lijiajia/-empty # find a file or folder empty in the system find/home/lijiajia/-group cat # find it in the system file belonging to groupcat (tried, the command is incorrect .) Find/home/lijiajia/-mmin-5 # find the file found/home/lijiajia/-mtime-1 modified in the last 5 minutes of the system # search for the last 24 hours in the system find/home/lijiajia/-nouser # find the file that belongs to the void user in the system (what does it mean) find/home/lijiajia/-amin 10 # find the file found/home/ftp/pub-user lijiajia accessed in the last 10 minutes of the system # find the user who belongs to lijiajia IN THE SYSTEM file (PS: the above operations are performed in the/home/lijiajia/folder.) 4. find/tmp-size + 10000000 C-and-mtime + 2 # find a file in the/tmp directory that is larger than bytes and modified within 48 hours P-user tom-or-user george # find the file find/tmp that belongs to the tom or george users in the/tmp directory! -Usr fred # Find Files that do not belong to fred in the/tmp directory. find/home/lijiajia/-name 'elm. cc '-ls # Find the name "elm. cc file, and display the information of these files

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.