Home > Article > Operation and Maintenance > What are the common commands for Linux files and disks?
#View file information: ls ls is the abbreviation of the English word list. Its function is to list the contents of the directory. It is one of the most commonly used commands by users. It is similar to the dir command under DOS; The Linux file or directory name can have a maximum length of 265 characters. "." represents the current directory, and '..' represents the upper-level directory. Files starting with '.' are hidden files and need to be displayed with the -a parameter. ##lsCommon parameters -a displays all subdirectories and files in the specified directory, including hidden files -l displays file details in list format -h works with -l to display the file size in a humane way ##View directory filtering
ls -lh 已k为单位显示查询出的文件大小 ls -a 显示所有文件,包含隐藏文件 ls -lha 注意,这里参数的顺序可以更改,所表述的含义是相同的。 ls 路径: 查询指定路径的文件列表 * - 通配符,代表任意字符(0到多个) ? - 通配符,代表一个字符 () - 中间为子shell的起始与结束 [] - 中间为字符组合 [abc] -匹配字符abc [a-z]-匹配a-z之间所有字符 {} - 中间为命令区块组合 && - 当前一个指令执行成功时,执行后一个指令 || - 当前一个指令执行失败时,执行后一个指令 ls test* *表示后面不论接几个字符都接受(没有字符也接受) ls test? ?表示后面当且仅当接一个字符时才接受 ls test??? ???表示一定要接三个字符 cp test[1~5] /tmp test1, test2, test3, test4, test5若存在,则复制到/tmp目录下 若文件名为*.a ,则使用ls \*.a进行匹配
##Output redirection command:> Linux allows the command execution results to be redirected to a file, and the content that should be displayed on the terminal is saved to the specified file; ls -lha > ls.txt displays the query information in the ls.txt file. (If ls.txt does not exist, create one, and if it exists, overwrite its content) Note: ">" means overwriting the source file, and ">>" output redirection will be appended to the end of the file; You can use gedit filename to open this file to view the redirection results; ##Split screen display: more When viewing content, when the information is too long to be displayed on one screen, rapid scrolling will occur, making it difficult for the user to see the content of the file. At this time, the more command can be used to display only one page at a time. Press the space bar to display it. On the next page, press the q key to exit the display, and press the h key to get help; more: When there is too much query content, you can use the more keyword to display the query results in split screens; more ls -lha displays the query results in split screen format; ##Pipeline: | Pipes: The output of one command can be used as the input of another command through a pipe; The pipeline is actually a piece of memory used for buffering. All output content is cached first and then retrieved from the cache; Display the contents of a certain text in the terminal: cat file name; cat linux notes.txt | more "|" represents a pipeline (cache), which caches the content first and then displays it in split screen after all the caching is completed; ##Clear screen: clear Clear is used to clear the display content on the terminal. The same DOS command is the cls command. You can also use the shortcut key: Ctrl L ##Switch working directory: cd When using Unix/Linux, you often need to change the working directory; The cd command can help users switch working directories. All directory and file names in Linux are case-sensitive; cd can be followed by an absolute path or a relative path. If the directory is omitted, it defaults to the current user's home directory; The usage of cd is as follows:
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 the current directory; cd ..: switch to the upper-level directory; cd -: Switch to the last directory;
##Display the current path: pwd Use the pwd command to display the current working directory. This command is very simple. Just enter pwd directly without parameters; ##Create directory: mkdir A new directory can be created through the mkdir command. The parameter -p can create directories recursively; It should be noted that the name of the new directory cannot have the same name as an existing directory or file in the current directory, and the directory creator must have write permissions on the current directory; The usage of mkdir is as follows:
mkdir test creates a folder named test; mkdir a/b/c –p creates a folder named c that exists in the b directory under the directory;
Note that this type of folder creation is called recursive directory creation ##Delete directory: rmdir You can use the rmdir command to delete a directory. You must leave the directory, and the directory must be an empty directory, otherwise it will prompt that the deletion failed; ##Delete files: rm Files or directories can be deleted through rm. Be careful when using the rm command because files cannot be recovered after being deleted; In order to prevent accidentally deleting files, you can use the -i parameter after rm to confirm the files to be deleted; Commonly used parameters and their meanings are shown in the following table:
Soft link: Soft link does not occupy disk space. If the source file is deleted, the soft link will become invalid. Hard links: Hard links can only link ordinary files, not directories.
Use format:
ln source file link file ---hard link ln -s source file link file ---soft link
A hard link is equivalent to a file with multiple file names. Creating a hard link will cause the counter in the file to be 1. Deleting a hard link or the source file counter will be -1. A counter of 0 means the file is actually deleted; Soft link files are equivalent to shortcuts in Windows. Deleting the source file will invalidate the soft link file; The new file of the hard link occupies the same size of hard disk space as the source file, so soft link (-s) is generally used; 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 cannot use a relative path. ##Text search: grep The grep command in Linux system is a powerful text search tool. grep allows pattern search on text files. If a matching pattern is found, grep prints all lines containing the pattern; The general format of grep is:
grep [-option] ‘Search content string’ file name When entering string parameters in the grep command, it is best to enclose them in quotes or double quotes. For example: grep ‘a’ 1.txt;
Common option description:
-v displays all lines that do not contain matching text (equivalent to negation); -n displays matching lines and line numbers; -I ignore case; The grep search content can be a regular expression;
##Find files: find The Find command is very powerful. It is usually used to search for files that meet the conditions in a specific directory. It can also be used to search for files with specific user attributes;
##Copy files :cp The function of the cp command is to copy the given file or directory to another file or directory, which is equivalent to the copy command in DOS; Description of common options:
-a This option is usually used when copying a directory. It retains links, file attributes, and the copied directory. In short, it maintains the original attributes of the file; -f no longer prompts that the directory file already exists; -i interactive copy, prompts the user for confirmation before overwriting the target file; -r If the given source file is a directory file, cp will copy all subdirectories and files in the directory, and the target file must be a directory name; -v displays copy progress;
##Move files: mv Users can use the mv command to move files or directories, or rename files or directories. Equivalent to clipping in Window; Description of common options:
-f interactive operation, no prompt will be given if overwritten; -i Confirm the interactive mode operation. If the mv operation will cause the overwriting of an existing target file, the system will ask whether to rewrite and require the user to answer to avoid overwriting the file by mistake; -v displays the move progress;
##Archive management: tar Data in the computer often needs to be backed up. tar is the most commonly used backup tool in Unix/Linux. This command can archive a series of files into a large file, and can also unzip the archive file to restore data; tar uses the format tar[parameter] to package filename file; The tar command is very special. You can use "-" in front of its parameters or not; Commonly used parameters:
-c generates archive files and creates packaging files; -v lists the detailed process of archive unarchiving and displays the progress; -f specifies the archive file name, f must be followed by a .tar file, so the option must be placed last; -t lists the files contained in the archive; -x Unlock the archive file;
Note: Except for f, which needs to be placed at the end of the parameter, the order of other parameters is arbitrary. ##File compression and decompression: gzip Use tar in conjunction with the gzip command to package and compress files; tar is only responsible for packaging files, but not compressing them. Use gzip to compress tar-packed files, and their extensions are generally XXX.tar.gz; Common options:
-d decompression; -r compresses all subdirectories;
gzip and tar commands can be used together, just add "z" before the available tar parameters. Unzip using “tar –zxvf filename” ##File compression and decompression: bzip2 Use tar in conjunction with the bzip2 command to implement file packaging and compression (the usage is the same as gzip); The tar value is responsible for packaging files, but not compressing them. Use bzip2 to compress tar-packed files, and their extensions are generally xxxx.tar.gz2; Adding an option (-j) to the tar command can call bizp2 to implement a compression function and implement the process of packaging first and then compressing; Compression method: tar jcvf compressed package name file... (tar –jcvf bk.tar.gz2 *.txt) Decompression method: tar jxcf compressed package name (tar jxvf bk.tar.bz2) ##File compression and decompression: zip, unzip The target file compressed by zip does not need to specify an extension, and the default extension is zip; Compressed file: zip [-r] target file (without extension) source file; Unzip the file: unzip –d Decompressed directory file compressed file.
The above is the detailed content of What are the common commands for Linux files and disks?. For more information, please follow other related articles on the PHP Chinese website!