Getting Started with Linux: Basic Instructions

This article will introduce some basic instructions that we may encounter when we first learn Linux, so that we can have a basic understanding of these instructions. 

Table of contents

01. ls command

02. pwd command

03. cd command

04. touch command

05. mkdir command (important)

06. rmdir command&& rm command (important):

07. man command (important)

08. cp command (important)

09. mv command (important):

10. cat

11. more command

12. less command (important)

13. head command

14. tail command

15. Time-related instructions

16. cal

17. grep command

18. zip/unzip command:

19. tar command (important): pack/unpack, don’t open it, look at the content directly

20. bc command

21. uname -r command:

22. Several important hotkeys [Tab], [ctrl]-c, [ctrl]-d

23 find command: (very important) -name


01. ls command

Syntax: ls [options][directory or file], [ ] means optional

Function: For a directory, this command lists all subdirectories and files in the directory. For files, the file name is listed along with other information.

Common options:

  • -a Lists all files in the directory, including implicit files starting with . There is only one . representing the current directory and .. representing the upper directory
  • -d displays the directory like a file, rather than the files beneath it. For example: ls -d specifies the directory
  • -i Outputs the index information of the i node of the file. For example, ls -ai specifies the file
  • -k indicates the size of the file in k bytes. ls -alk specifies the file
  • -l List file details.
  • -n Use numeric UID, GID instead of name. 
  • -F Attach a character after each file name to indicate the type of the file. "*" indicates an executable ordinary file; "/" indicates a directory; "@" indicates a symbolic link; "|" indicates FIFOs; "= ” means sockets. (Directory type identification)
  • -r sorts the directory in reverse order.
  • -t Sort by time.
  • -s outputs the size of the file after the l file name. (Size sorting, how to find the largest file in a directory)
  • -R List files in all subdirectories. (recursion)
  • -1 only outputs one file per line

Commonly used commands in the early stage:

ls -l ls -a ls

02. pwd command

Syntax: pwd

Function: Display the directory where the user is currently located

Common options: none

03. cd command

In the Linux system, the files and directories on the disk are organized into a directory tree, and each node is a directory or file.

Syntax: cd directory name

Function: Change the working directory. Change the current working directory to the specified directory.
Example:

  • cd .. : Return to the upper directory
  • cd /home/litao/linux/ : absolute path
  • cd ../day02/ : relative path
  • cd ~: Enter the user's home directory
  • cd -: Return to the recently visited directory

04. touch command

Syntax: touch [options]... file...

Function: The touch command parameter can change the date and time of a document or directory, including access time and change time, or create a new file that does not exist.

Common options:

  • -a or --time=atime or --time=access or --time=use only changes the access time.
  • -c or --no-create Do not create any documentation.
  • -d Use the specified date and time instead of the current time.
  • -f This parameter will be ignored and will not be processed. It is only responsible for solving the compatibility problem of the BSD version of the touch command.
  • -m or --time=mtime or --time=modify only change the change time.
  • -r sets the date and time of the specified document or directory to be the same as the date and time of the reference document or directory.
  • -t Use the specified date and time instead of the current time

In the early stage, I mainly used touch to create files directly:

touch test.txt

05. mkdir command (important)

Syntax: mkdir [options] dirname...

Function: Create a directory named "dirname" in the current directory

Common options:

  • -p, --parents can be a path name. At this time, if some directories in the path do not yet exist, after adding this option, the system will automatically create those directories that do not yet exist, that is, multiple directories can be created at one time;

Example:

Create a directory:

mkdir test

Recursively create directories within directories

mkdir -p test/test1/test2

06. rmdir command&& rm command (important):

rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is a delete command.

Syntax: rmdir [-p][dirName]

Applicable objects: All users with permission to operate the current directory

Function: Delete empty directories

Common options:

  • -p When the subdirectory is deleted, if the parent directory also becomes an empty directory, the parent directory will be deleted together.

The rm command can delete files or directories at the same time

Syntax: rm [-firv][dirName/dir]

Applicable to: All users

Function: Delete files or directories

Common options:

  • -f Even if the file attribute is read-only (i.e. write-protected), it will be deleted directly, force force
  • -i Ask for confirmation one by one before deleting
  • -r deletes the directory and all files under it, r recursively

07. man command (important)

Linux commands have many parameters, and it is impossible for us to remember them all. We can get help by checking the online manual. The command to access the Linux man page is

man syntax: man [options] command

Common options:

  1. -k Search online help based on keywords
  2. num is only found in chapter num
  3. -a displays all chapters, such as man printf. By default, it starts searching from the first chapter and stops when it is known. Use the a option. When pressing q to exit, it will continue to search until all chapters. All searches completed.

Explain, the manual is divided into 8 chapters

  1. It's a normal command
  2. It is a system call, such as open, write, etc. (Through this, you can at least easily find out what header files need to be added to call this function)
  3. It is a library function, such as printf, fread
  4. It is a special file, that is, various device files under /dev
  5.  It refers to the format of the file, such as passwd, which will explain the meaning of each field in the file.
  6. It is reserved for games and is defined by each game.
  7. It is an attachment and there are some variables, such as global variables such as environ, which are explained here.
  8. It is a command used for system management. These commands can only be used by root, such as ifconfig.
     

08. cp command (important)

Syntax: cp [options] source file or directory target file or directory

Function:  Copy files or directories

Description: The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, it will copy all previously specified files or directories to this directory. . If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.

Common options:

  • -f or --force forcefully copies a file or directory, regardless of whether the destination file or directory already exists.
  • -i or --interactive ask the user before overwriting the file
  • -r recursively processes files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file.
  • -R or --recursive recursive processing, processing files and subdirectories in the specified directory together
     

09. mv command (important):

The mv command is the abbreviation of move. It can be used to move files or rename files (move (rename) files). It is a commonly used command in Linux systems and is often used to back up files or directories.

Syntax: mv [options] source file or directory target file or directory

Function:

  1.  Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command renames the file or moves it to a new directory.
  2.  When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (it can also be the source directory name). It will rename the given source file or directory to the given one. Target file name.
  3. When the second parameter is the name of an existing directory, there can be multiple source files or directory parameters, and the mv command will move all the source files specified by each parameter to the target directory.

Common options:

  • -f: force means force. If the target file already exists, it will be overwritten directly without asking.
  • -i: If the destination file (destination) already exists, it will ask whether to overwrite it.
     

10. cat

Syntax: cat [options][file]

Function: View the contents of the target file

Common options:

  • -b output line numbers for non-empty lines
  • -n numbers all lines of output
  • -s does not output multiple blank lines

If you do not enter a file name and there is only one cat, it will display what you input, that is, the standard input stream file on the screen. Just understand it here. I will talk about it in a later article. Everything in Linux is a file. Cat does not The specified file will be read from the default standard input stream file.

11. more command

Syntax: more [options][file]

Function: The more command has a function similar to cat, and the more command is used to display file contents in pages. It can view files page by page, making it convenient for users to view large files (press enter to page backward). Therefore, cat is suitable for viewing smaller files, while more is suitable for viewing larger files.

Common options:

  • q exitmore

12. less command (important)

  1. The less tool is also a tool for paging display of files or other output. It should be said that it is an orthodox tool for viewing file contents in Linux and is extremely powerful.
  2. The usage of less is more flexible than that of more. In more, we have no way to turn forward, we can only look back.
  3. But if you use less, you can use the [pageup][pagedown] and other key functions to browse files forward and backward, which is easier to view the contents of a file!
  4. In addition, you can have more search functions in less, not only you can search down, but you can also search up.

Syntax: less [parameter] file

Function:

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.

Commonly used options: (First open the file with less, then enter the options):

  • -i ignore case when searching
  • -N displays the line number of each line
  • /string: function to drill down for "string"
  • ?String: Function to search upward for "string"
  • n: Repeat the previous search (related to / or ?)
  • N: Repeat the previous search in reverse (related to / or ?)
  • q:quit

13. head command

Head and tail are as easy to understand as their names. They are used to display a certain number of text blocks at the beginning or end. Head is used to display the beginning of the file to the standard output, while tail is used to view the end of the file. .

Syntax: head [parameter]... [file]...

Function:

head is used to display the beginning of the file to the standard output. The default head command prints the first 10 lines of its corresponding file.

Common options:

  • -n<number of lines> Number of lines to display

14. tail command

The tail command writes the file to the standard output starting from the specified point. Use the -f option of the tail command to conveniently check the changing log file. tail -f filename will display the last content of filename on the screen, and not only refresh , so that you can see the latest file content.

Syntax: tail[required parameters][selected parameters][file]

Function: Used to display the content at the end of the specified file. When no file is specified, it will be processed as input information. Commonly used to view log files.

Common options:

  • -f loop reading
  • -n<number of lines> displays the number of lines

15. Time-related instructions

date display

date specifies the format to display time: date +%Y:%m:%d
date usage: date [OPTION]... [+FORMAT]

1. In terms of display, the user can set the format to be displayed. The format is set to a plus sign followed by several marks. The list of commonly used marks is as follows:


  • %H : hours (00..23)
  • %M : Minutes (00..59)
  • %S : seconds (00..61)
  • %X: equivalent to %H:%M:%S
  • %d : day(01..31)
  • %m: month (01..12)
  • %Y: Complete year (0000..9999)
  • %F: equivalent to %Y-%m-%d

2. In terms of setting time


  • date -s //Set the current time. Only root permissions can set it, others can only view it.
  • date -s 20080523 //Set to 20080523, which will set the specific time to empty 00:00:00
  • date -s 01:01:01 //Set the specific time and the date will not be changed.
  • date -s “01:01:01 2008-05-23″ //This can set the entire time
  • date -s “01:01:01 20080523″ //This way you can set the entire time
  • date -s “2008-05-23 01:01:01″ //This way you can set the entire time
  • date -s “20080523 01:01:01″ //This way you can set the entire time

3. Timestamp

Time->Timestamp: date +%s

Timestamp->Time: date -d@1508749502

The Unix timestamp (Unix epoch, Unix time, POSIX time or Unix timestamp in English) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), regardless of leap seconds.

16. cal

The cal command can be used to display the Gregorian (Solar) calendar. The Gregorian calendar is the calendar currently used internationally, also known as the Gregorian calendar and commonly known as the Gregorian calendar. The "Gregorian calendar", also known as the "solar calendar", is based on the earth's orbit around the sun as one year. It is common in Western countries, so it is also called the "Western calendar".

Command format: cal [parameter][month][year]

Function: Used to view calendar and other time information. If there is only one parameter, it represents the year (1-9999). If there are two parameters, it represents the month and year.

Common options:

  • -1 Displays the system’s monthly calendar for the current month
  • -3 Display the system’s calendar of the previous month, current month, and next month
  • -j displays the day of the year (the date of the year is calculated in days, starting from January 1st, and the number of days of the current month in the year is displayed by default)
  • -y displays the calendar for the current year

17. grep command

Syntax: grep [options] Search string files

Function: Search for a string in the file and print out the found lines

Common options:

  • -i: Ignore the difference in case, so the cases are treated as the same
  • -n: Output the line number by the way
  • -v: reverse selection, that is, display the line without the 'search string' content

18. zip/unzip command:

Syntax: zip compressed file.zip directory or file

Function: Compress directories or files into zip format

Common options:

  • -r recursively processes all files and subdirectories in the specified directory.

Common options for unzip:

  • -d<directory> specifies the directory where the file will be stored after decompression.

Example:

Compress the test2 directory: zip test2.zip test2/*
and extract it to the tmp directory: unzip test2.zip -d /tmp

19. tar command (important): pack/unpack, don’t open it, look at the content directly

tar [-cxtzjvf] files and directories.... Parameters:


  • -c: Parameter command to create a compressed file (meaning create);
  • -x: Parameter command to unpack a compressed file!
  • -t: View the files in tarfile!
  • -z: Does it also have the attribute of gzip? That is, do I need to use gzip compression?
  • -j: Does it also have the attributes of bzip2? That is, do I need to use bzip2 compression?
  • -v: Display files during compression! This is commonly used, but it is not recommended to be used in background execution processes!
  • -f: Use the file name. Please note that the file name must be followed immediately after f! Don't add any more parameters!
  • -C: Extract to the specified directory

Example:

Example 1: Pack all the files in the entire /etc directory into `/tmp/etc.tar`
[root@linux ~]# tar -cvf /tmp/etc.tar /etc<== Only packaging, not compression!
[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <==After packaging, compress it with gzip
[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc < ==After packaging, use bzip2 to compress
. Pay special attention to the file name after the parameter f. You choose it yourself. We are accustomed to using .tar as the identification.
If the z parameter is added, .tar.gz or .tgz will be used to represent the gzip compressed tar file ~
If the j parameter is added, .tar.bz2 will be used as the file extension name ~
When the above command is executed, it will be displayed A warning message:
『`tar: Removing leading `/" from member names`』 That is a special setting about absolute paths.

Example 2: Check what files are in the above /tmp/etc.tar.gz file?
[root@linux ~]# tar -ztvf /tmp/etc.tar.gz
Since we use gzip compression, when we want to check the files in the tar file, we have to add the z parameter! This is very important!

Example 3: Decompress the /tmp/etc.tar.gz file under /usr/local/src
[root@linux ~]# cd /usr/local/src
[root@linux src]# tar -zxvf /tmp/ etc.tar.gz
By default, we can decompress the compressed file anywhere! Taking this example,
I first change the working directory to /usr/local/src and extract /tmp/etc.tar.gz. The
extracted directory will be in /usr/local/src/etc! In addition, if you enter /usr/local/src/etc
, you will find that the file attributes in this directory may be different from /etc/!

Example 4: Under /tmp, I just want to unzip etc/passwd in /tmp/etc.tar.gz [root@linux ~]# cd /tmp [
root@linux tmp]# tar -zxvf / tmp/etc.tar.gz etc/passwd
I can use tar -ztvf to check the file name in the tarfile. If I only need one file,
I can download it through this method! Notice! The root directory / in etc.tar.gz has been removed!

Example 5: Back up all files in /etc/ and save their permissions!
[root@linux ~]# tar -zxvpf /tmp/etc.tar.gz /etc
The -p attribute is very important, especially when you want to retain the attributes of the original file!
Example 6: In /home, only files newer than 2005/06/01 are backed up
[root@linux ~]# tar -N "2005/06/01" -zcvf home.tar.gz /home

Example 7: I want to back up /home, /etc, but not /home/dmtsai
[root@linux ~]# tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc

Example 8: Pack /etc/ and unpack it directly under /tmp without generating a file!
[root@linux ~]# cd /tmp
[root@linux tmp]# tar -cvf - /etc | tar -xvf -This
action is a bit like cp -r /etc /tmp~ It still has its uses!
What should be noted is that the output file becomes - and the input file also becomes -, and there is another |,
which represents standard output, standard input and pipeline commands respectively.

20. bc command

The bc command can easily perform floating point operations (calculator)

21. uname -r command:

Syntax: uname [options]

Function: uname is used to obtain information about the computer and operating system.

Additional explanation: uname can display basic information such as the version of the operating system used by the Linux host and the name of the hardware.

Common options:

  • -a or –all output all information in detail, in order: kernel name, host name, kernel version number, kernel version, hardware name, processor type, hardware platform type, operating system name

22. Several important hotkeys [Tab], [ctrl]-c, [ctrl]-d

  1. [Tab] button---has the functions of "command completion" and "file completion"
  2. [Ctrl]-c key---"stop" the current program
  3. [Ctrl]-d key---usually represents: "End Of File, EOF or End OfInput"; in addition, it can also be used to replace exit
  4. Copy: ctrl + insert
  5. Paste: shift + insert. ctrl + c / ctrl + v will not work

23 find command: (very important) -name

  1. The find command under Linux searches for files in the directory structure and performs the specified operation.
  2. The find command under Linux provides quite a few search conditions and is very powerful. Because find has powerful functions, it also has many options, most of which are worth our time to understand.
  3. Even if the system contains a Network File System (NFS), the find command is also valid in the file system, as long as you have the corresponding permissions.
  4. When running a very resource-consuming find command, many people tend to execute it in the background, because traversing a large file system may take a long time (here refers to a file system of more than 30G bytes).

Syntax: find pathname -options

Function: Used to search for files in the file tree and process them accordingly (possibly accessing the disk)

Common options:

  • -name searches for files by file name.

This article ends, thank you everyone!

Guess you like

Origin blog.csdn.net/qq_72916130/article/details/133176805