Linux Commands-Files

Source: Internet
Author: User
Tags bz2 clear screen create directory parent directory

Viewing file information: ls

LS is the abbreviation of the English word list, its function is to list the contents of the directory, is one of the most commonly used commands, it is similar to the dir command under DOS.

Linux文件或者目录名称最长可以有265个字符,“.”代表当前目录,“..”代表上一级目录,以“.”开头的文件为隐藏文件,需要用 -a 参数才能显示。

LS Common parameters:

Parameters meaning
-A Displays all subdirectories and files in the specified directory, including hidden files
-L Display file details in a list
-H Display file size in a humane way with-l

Similar to file operations under DOS, in the Unix/linux system, it is also possible to use special characters to refer to multiple filenames simultaneously, which are called wildcards.

wildcard characters meaning
* File represents all characters in the file name
LS te* Find files that start with Te
LS *html Finding files that end in HTML
Represents any character in a file name
Ls?. C Find only the first character, any file with a. c suffix
LS A.? Only 3 characters are found, the first 2 characters are a., the last character arbitrary file
[] [and] Enclose character groups to indicate that any of the character groups can be matched. "-" is used to represent the range of characters.
[ABC] Match any of A, B, c
[A-f] Match any one of the characters from a to F range
LS [a-f]* Find files that begin with any one of the characters from a to F range
LS a-f Find files with file name a-f when "-" loses wildcard characters outside square brackets
\ If you want to use wildcards as normal characters, you can precede them with escape characters. “?” and "*" in square brackets without the use of escape characters to lose the role of a wildcard character.
LS \*a Find files with file name *a
Output REDIRECT Command:>

Linux allows the command execution results to be redirected to a file that should be displayed on the terminal and saved to the specified file.

such as: ls > test.txt (test.txt If not present, created, existing overwrite its contents)

Attention:>输出重定向会覆盖原来的内容,>>输出重定向则会追加到文件的尾部。

Split screen display: more

When viewing content, when the information is too long can not be displayed on a screen, the rapid scrolling screen, so that users cannot see the contents of the file, you can use the more command, only one page at a time, press the SPACEBAR to display the next page, press the Q key to exit the display, press the H key to get help.

Pipeline: |

Pipe: The output of one command can be piped as input to another command.

Pipeline we can understand the real life of the pipe, the pipe of a plug into the other side, take out, here "| "The left and right are divided into two ends, and at the end of the thing (write), and take something (read).

Clear screen: Clear

The clear function is to clear the display on the terminal (similar to the DOS CLS Clear screen feature), or you can use the shortcut key: Ctrl + L ("L" for the letter).

Switch working directory: CD

When using Unix/linux, it is often necessary to change the working directory. The CD command helps the user to switch the working directory.Linux所有的目录和文件名大小写敏感

The CD can be followed by an absolute path, or with a relative path. If you omit the directory, the default is to switch to the current user's home directory.

Command meaning
Cd Switch to the current user's home directory (/home/user directory), when the user logs in, the default directory is the user's home directory.
CD ~ Switch to the current user's home directory (/home/user directory)
Cd. Switch to current directory
Cd.. Switch to Parent directory
CD- Access to the directory where you last came

Attention:

    • If the path starts from the root path, the path is preceded by a "/", such as "/mnt", which usually goes to a folder in a directory that is not preceded by "/".
Show current path: pwd

The PWD command allows you to display the current working directory, which is simple and can be entered directly into PWD without parameters.

Create directory: mkdir

You can create a new directory by using the mkdir command. Parameter-p to create a directory recursively.

Note that the name of the new directory cannot be the same as the directory or file already in the current directory, and the directory creator must have write access to the current directory.

Delete directory: RmDir

You can use the rmdir command to delete a directory. The directory must be left, and the directory must be empty, or the deletion will fail.

Delete File: RM

You can delete files or directories from RM. Use the RM command with caution because the file cannot be recovered after it is deleted. To prevent files from being mistakenly deleted, you can use the-I parameter after RM to confirm the files that you want to delete individually.

Common parameters and meanings are shown in the following table:

Parameters meaning
-I. For interactive execution
-F Forced deletion, ignoring nonexistent files without prompting
-R To delete the contents of a directory recursively, this parameter must be added when deleting a folder

To create a linked file: ln

A Linux link file is similar to a shortcut under Windows.

Linked files are broken down into soft links and hard links.

Soft Link: Soft link does not occupy disk space, the source file is deleted and the soft link is invalid.

Hard Links: Hard links can only link to normal files and cannot be linked to directories.

Use format:

ln 源文件 链接文件ln -s 源文件 链接文件

If the 没有-s option represents the creation of a hard-link file, two files occupy the same size of hard disk space, even if the source file is deleted, the link file still exists, so the-s option is more common form.

Note: If the soft link file and the source file are not in the same directory, the source file must use an absolute path and the relative path cannot be used.

View or merge file contents: Cat Text Search: grep

The grep command in a Linux system is a powerful text search tool that grep allows for pattern lookups of text files. If a matching pattern is found, grep prints all rows that contain the pattern.

The general format of grep is:

grep [-选项] ‘搜索内容串’文件名

When you enter a string parameter in the grep command, it is best to enclose it in quotation marks or double quotation marks. For example: grep ' a ' 1.txt.

Common Options Description:

Options meaning
-V Display all lines that do not contain matching text (equivalent negation)
-N Display matching lines and line numbers
-I. Ignore case

The grep search content string can be a regular expression.

A regular expression is a logical formula for a string operation, which is a "rule string" that is used to express a filter logic for a string, using predefined specific characters and combinations of these specific characters.

grep Common Regular Expressions:

Parameters meaning
^a Beginning of line, searching for lines beginning with M; Grep-n ' ^a ' 1.txt
ke$ At the end of the line, search for the line ending with ke; grep-n ' ke$ ' 1.txt
[Ss]igna[ll] Match one of a series of characters in []; Search for lines matching words SignaL, SignaL, SignaL, SignaL; Grep-n ' [Ss]igna[ll] ' 1.txt
. Matches a non-newline character; matches between E and E have any one character, can match eee,eae,eve, but does not match ee,eaae;grep-n ' E.E ' 1.txt

Find Files: Find

The Find command is very powerful and is often used to search for eligible files in a specific directory, or to search for files that are owned by a particular user.

Common usage:

Command meaning
Find./-name test.sh Find all files named test.sh in the current directory
Find./-name ' *.sh ' Find all files with the suffix. Sh in the current directory
Find./-name "[a-z]*" Find all files in the current directory that begin with uppercase letters
Find/tmp-size 2M Find files that are equal to 2M in the/tmp directory
Find/tmp-size +2m Find files larger than 2M in the/tmp directory
Find/tmp-size-2m Find files less than 2M in the/tmp directory
Find./-size +4k-size-5m Find files larger than 4k and less than 5M in the current directory
Find./-perm 0777 Find a file or directory with permission 777 under the current directory
Copy file: CP

The function of the CP command is to copy the given file or directory to another file or directory, equivalent to the Copy command under DOS.

Common Options Description:

Options meaning
-A This option is typically used when copying a directory, preserving links, file attributes, and recursively copying the directory, simply keeping the file's original properties.
-F A target file that already exists without prompting
-I. Interactive replication, which prompts the user to confirm before overwriting the target file
-R If the given source file is a directory file, the CP will recursively replicate all subdirectories and files in that directory, and the destination file must be a directory name.
-V Show copy Progress

Moving files: MV

Users can use the MV command to move files or directories, or to rename files or directories.

Common Options Description:

Options meaning
-F Disable interactive operation and do not prompt if overwrite is available
-I. Confirm the interactive operation, if the MV operation will result in overwrite of the existing target file, the system will ask whether to rewrite and ask the user to answer to avoid overwriting the file by mistake.
-V Show Move Progress

Archive Management: Tar

Data in the computer often needs to be backed up, and tar is the most commonly used backup tool in Unix/linux, which archives a series of files into a large file or unlocks the file to recover the data.

Tar use format tar [parameters] package file name file

The tar command is special, and its arguments can be preceded by "-" or not.

Common parameters:

Parameters meaning
-C Generate an archive file, create a packaged file
-V List the details of the archive file and show the progress
-F Specify the file name, and F must be a. tar file, so you have to put the option last
-T List files included in the archive
-X Unlock the archive file

Note: Except that F needs to be placed at the end of the parameter, the order of the other parameters is arbitrary.

File Compression Decompression: gzip

The tar is used in conjunction with the GZIP command to package and compress files. Tar is only responsible for packaging files, but not compression, with gzip compressed tar packaged files, the extension is generally used xxxx.tar.gz.

Gzip uses the following format:

gzip  [选项]  被压缩文件

Common options:

Options meaning
-D Extract
-R Compress all subdirectories

Tar this command does not compress the function, it is just a packaged command, but adding an option in the tar command (-Z) can call gzip to implement a compression function, the implementation of a pre-packaged compression process.

Compression usage: Tar cvzf Compress package name file 1 File 2 ...

-z :指定压缩包的格式为:file.tar.gz

Decompression usage: Tar zxvf compress package name

-z:指定压缩包的格式为:file.tar.gz

Unzip to the specified directory:-C (uppercase "C")

File Compression Decompression: bzip2

Tar is used in conjunction with the BZIP2 command to implement file packaging, compression (usage and gzip).

Tar is only responsible for packaging files, but does not compress, with bzip2 compressed tar packaged files, its extension is generally used xxxx.tar.gz2.

Adding an option to the tar command (-J) can call bzip2 to implement a compression function that implements a pre-packaged compression process.

Compression usage: TAR-JCVF Compress package name file ... (Tar jcvf bk.tar.bz2 *.c)

Decompression usage: TAR-JXVF Compressed package name (Tar jxvf bk.tar.bz2)

File compression decompression: Zip, unzip

The destination file for the ZIP archive does not need to specify an extension, and the default extension is zip.

Compressed file: Zip [-r] Destination file (no extension) source file

Unzip the file: unzip-d unzip the directory file after the archive

View command location: which

Linux Commands-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.