[Linux] Elementary_common commands

Note: Commands are case sensitive


1. ls command:

        It is the abbreviation of list. Through the ls command, you can not only view the files contained in the linux folder, but also view the file permissions (including directory, folder, and file permissions) to view directory information and so on.

ls -a 列出目录所有文件,包含以.开始的隐藏文件
ls -A 列出除.及..的其它文件
ls -r 反序排列
ls -t 以文件修改时间排序
ls -S 以文件大小排序
ls -h 以易读大小显示
ls -l 除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来

PS: Multiple options of Linux commands can be used at the same time. (ls is a command, followed by -a is an option)

         

Two, cd command:

        Change directory, enter a certain folder.

cd + [folder name] enter the folder

Extended command: pwd command

pwd (print work directory) Print the current directory to show the absolute path of the current working directory

  Extended command: tree command

tree Displays the tree structure of files and directories starting from the root directory (the one without the folder name behind is the current location)

Enter the dir folder.

The above paths are relative paths, absolute paths can also be used.

Here, the absolute path is used to enter the folder named d3.

 PS: / (slash: path separator) in Windows, the path separator is a backslash \  

 cd - return to last directory

PS: If it is an ordinary user, then  /home  is the home directory, and if it is the root user ~ is the home directory.

cd /home enters the '/home' directory' cd ~ enters the root directory

expand:

./  表示当前路径
../ 表示上一个路径

cd .. Return to the previous directory (you can also do without the slash) 
cd ../.. Return to the previous two directories

Three, touch command:

        Generate a normal file.

4. mkdir command:

         Make Directory, for creating folders.

        

mkdir -p t1/t2/t3 creates a string of paths at a time 

PS: Pay attention to the separator, mkdir -p / t1 /t2 /t3, you can’t find the folder if you write here.


Five, rm command:

        Remove, delete one or more files or directories in a directory, if the -r (recursive) option is not used, rm will not delete the directory . If you use rm to delete a file, you can usually still get the file back.

     

        
rm -r delete directory

       

       
 rm -rf code forcefully delete all files, do not confirm one by one -f forcefully delete

       
 rm -rf *.c Forcefully delete files ending with .c

 PS: Only the matching files in the path will be deleted, and the files in other directories will not be deleted.

sudo rm -rf /* The ultimate delete command, clear all files on the entire computer (server) including system files

 ! ! ! PS: Linux does not have a recycle bin, if you delete it, you delete it (although it can still be restored, it is troublesome). If you can use the rm command, you don’t need it. If you want to use it, you must back it up. ! ! !

Six, man command:

                In the Manuals manual, if you don’t know which command to use, just man it, and a description of the corresponding command will appear.

        Here man mkdir, there are also explanations of many options later. (It doesn’t feel very useful, there are command tutorials on the Internet) 

       

        You can also use man man, man calls out the manual by himself. Here we must focus on understanding the first three, you can man printf, here printf is also a library function.

Seven, cp command:

       Copy file, to copy a source file to a target file, or to copy multiple source files to a target directory.

-i Prompt 
-r Copy the directory and all items in the directory 
-a The copied file has the same time as the original file

Eight, mv command:

        Move file, move the file or modify the file name, according to the second parameter type (such as a directory, move the file; if it is a file, reorder the file).

        When the second parameter is a directory, the first parameter can be multiple files or directories separated by spaces, and then move the multiple files specified by the first parameter to the directory specified by the second parameter.

double naming

Nine, cat command:

         concatenate concatenation, print the contents of the file.

cat -n test.txt prints the contents of the file, -n with line number

Extended command: tac command

        reverse print

10. echo command:

        Print content to the display.

echo "hello world" > file.txt Redirect the content input, it will overwrite the original content, if there is no file, a new file will be generated 



echo "hello world" > > file.txt Append redirection, append content later

Eleven, more command:

        The function is similar to cat, which is suitable for reading long texts. More will display page by page to facilitate users to read page by page. The most basic command is to press the space key (space) to display the next page, and press the b key to go to the next page. Back (back) a page display.

//在file.txt文件里面写上10000个 字符串
count=0; while [ $count -le 10000 ]; do echo "hello bit ${count}"; let count++; done > file.txt

Use Q to exit the more command.

PS: If you don’t know how to exit when reading text or doing something here, use Ctrl + C to terminate the front-end process.

Expansion: less command 

        less is similar to more, but with less you can browse the file at will, while more can only move forward, not backward, and less does not load the entire file before viewing it.

        Use D to turn one page forward, use B to turn one page backward, and use Q to exit the less command.

12. head command:

        head is used to display the beginning of the file to the standard output. By default, the head command prints the first 10 lines of the corresponding file, and the corresponding number of lines can be printed with options.

Extension: tail command:

        It is used to display the content at the end of the specified file. The default is 10 lines. When no file is specified, it will be processed as input information. Commonly used to view log files.

Q: Get the content of lines 1000-1009 of a text?

        A:

        A temporary file is used here to come and go, so is there a way to not use a temporary file?

Expansion: pipeline" | " 

        

         The meaning here is to directly transfer the content to the tail command.

         Pipeline files are memory-level files, not on disk.

Extension: xargs

      eXtended ARGuments, received as arguments.

echo "-l -a -i" | xargs ls Use -l -a -i as a parameter for ls to use.

Thirteen, wc command:

        Word Count, the function is to count the number of bytes, words, and lines in the specified file, and output the statistical results

-c counts bytes 
-l counts lines 
-m counts characters 
-w counts words, a word is defined as a string separated by blanks, tabs or newline characters

Fourteen, date command:

        Display or set the system date and time.

-d<string> Display the date and time pointed to by the string. Strings must be enclosed in double quotes.

PS: This is called a timestamp, which is the number of seconds from 1970, January, 1st, 0 o'clock to the present. 

Format conversion:

        

Format conversion:

        

15. Cal command:

        The user displays the Gregorian (Gregorian) calendar, if there is only one parameter, it means the year (1-9999), if there are two parameters, it means the month and the year.

 

Sixteen, find command! Very important!

        Used to find files in the file tree and process them accordingly.

-name find files by filename

 Extension: whereis command

        The whereis command can only be used to search for program names, and only search for binary files (parameter -b), man description files (parameter -m) and source code files (parameter -s). If the parameter is omitted, all information is returned. whereis and locate are based on the built-in database of the system to search, so the efficiency is very high, and find is to traverse the hard disk to find files. 

Seventeen, which command

       Check out the location of the executable.

PS: It should be noted here that the essence of commands under Linux is executable files.

Extension: alias

        Give the command an alias. If you only enter alias, all current alias settings can be listed. The effect of alias is only limited to the login operation (the alias will become invalid after exiting). If you want to automatically set the alias every time you log in, you can set the alias of the command in .profile or .cshrc.

         

PS: The which command can also expand the original name.

        

 Eighteen, grep command:

        Global Regular Expression Print, a powerful text search command.

PS: grep is case sensitive

-i ignore case 
-v reverse match 
-r recursively find files

Can also be used to find files or folders

Nineteen, zip/unzip compression and packaging commands:

zip test.zip *.c pack all files ending with .c into test.zip

 

unzip test.zip -d ~/dir/t1/t2/t3 Unzip the compressed package to the specified directory

 

Twenty, tar command:

        Used to compress and decompress files. tar itself does not have a compression function, but only a packaging function, and the compression and decompression are completed by calling other functions.

PS: packed and compressed. Packaging refers to turning a large number of files or directories into a total file; compression refers to turning a large file into a small file through some compression algorithms.

-c Create a new compressed file 
-f Specify the compressed file 
-x Extract the file from the compressed package 
-t Display the contents of the compressed file 
-z Support gzip compression 
-v Display the operation process 

tar -czvf cod.taz code

PS: tar is packaging, tgz is compression, pay attention to write code.tar.tgz in some places 

PS: There are too many files and too big, don’t use -v to view the process, printing takes time.

tar -ztf code.tgz View the contents of the compressed package

tar -xzvf code.tgz -C project Unzip to the specified folder


expand:

Interview question: How can I check the Linux architecture and kernel version ?

uname -a view architecture (x86_64)     
uname -r kernel version

Intel's architecture:

                                x8086

                                x86 (x86_32)

                                x86_64 (x64)

Commonly used hotkeys:

         1. Quickly press [Tab] twice to find the command

         2. Enter a long command, write a little bit, and press [Tab] to complete the command.

         3. [Ctrl + C], stop the front end

         4. [Ctrl + D], quick exit

         5. [Ctrl + R], history command, input command fragment search.

         6. Shutdown: halt, shutdown (the cloud server does not shut down)

         7、Reboot the robot

History command history:

        View the commands entered in history.

history > cmd.txt Save historical commands to the cmd.txt file 

Guess you like

Origin blog.csdn.net/weixin_45423515/article/details/125836511