20 commands commonly used in Linux

Whether you are a back-end programmer or a front-end programmer, you cannot avoid dealing with Linux. There are many Linux commands, and here are only 20 commonly used ones, which are convenient for quick viewing, and you are welcome to add them.

1.mkdir command

Abbreviation for make directories, used to create directories Create directories

Syntax format: mkdir [parameters] [directory]

Common parameters:

parameterillustrate
-pCreate multi-level directories recursively
-mSet the permissions of the directory while creating the directory

Reference example:

  • Under the current directory, create a directory named dir
[root@wayne:~]# mkdir dir
  • Create a subdirectory dir under the directory /usr/wayne, and set the file owner to have read, write and execute permissions, and others have no access
[root@wayne:~]# mkdir -m 700 /usr/wayne/dir
  • Create two directories at the same time
[root@wayne:~]# mkdir dir1 dir2
  • Create directories recursively
[root@wayne:~]# mkdir -p dirs/subdir

2.rmdir command

Abbreviation for remove directory, used to delete empty directories

Syntax format: rmdir [parameter] [directory name]

Common parameters:

parameterillustrate
-pRecursively delete all parent directories in the specified directory path, if it is not empty, an error will be reported
-vDisplay the detailed execution process of the command

Reference example:

  • delete empty directory
[root@wayne:~]# rmdir dir
  • Recursively delete the specified directory tree
[root@wayne:~]# rmdir -p dirs/subdir/dir1
  • Show detailed execution process
[root@wayne:~]# rmdir -p -v dirs/subdir/dir1
rmdir: removing directory, 'dirs/subdir/dir1/'
rmdir: removing directory, 'dirs/subdir'
rmdir: removing directory, 'dirs'

3.touch command

Used to create a new empty file, or change the timestamp attribute of an existing file

Grammar format: touch [parameter] [file]

Common parameters:

parameterillustrate
-aChange the read time record of the file
-mChange the modification time record of a file

Reference example:

  • create empty file
[root@wayne:~]# touch myfile.txt
  • Batch create files
[root@wayne:~]# touch myfile{1..5}.txt
[root@wayne:~]# ls
myfile1.txt  myfile2.txt  myfile3.txt  myfile4.txt  myfile5.txt
  • Modify the access (access) time of the file
[root@wayne:~]# stat myfile.txt
File: 'myfile.txt'
...
Access: 2023-03-07 12:15:46.931368541 +0800
Modify: 2023-03-07 12:15:46.931368541 +0800
Change: 2023-03-07 12:15:46.931368541 +0800

[root@wayne:~]# touch -a myfile.txt
[root@wayne:~]# stat myfile.txt
File: 'myfile.txt'
...
Access: 2023-03-07 12:17:15.675569149 +0800
Modify: 2023-03-07 12:15:46.931368541 +0800
Change: 2023-03-07 12:17:15.675569149 +0800
  • Modify the modify (modification) time of the file
[root@wayne:~]# stat myfile.txt
File: 'myfile.txt'
...
Access: 2023-03-07 12:17:15.675569149 +0800
Modify: 2023-03-07 12:15:46.931368541 +0800
Change: 2023-03-07 12:17:15.675569149 +0800

[root@wayne:~]# touch -m myfile.txt
[root@wayne:~]# stat myfile.txt
File: 'myfile.txt'
...
Access: 2023-03-07 12:17:15.675569149 +0800
Modify: 2023-03-07 12:19:08.223649598 +0800
Change: 2023-03-07 12:19:08.223649598 +0800

4.rm command

Used to delete one or more files (directories) in a directory

Syntax format: rmdir [parameter] [directory name]

Common parameters:

parameterillustrate
-fIgnore non-existing files without warning messages
-iBefore deleting, the user will be asked whether to operate
-rdelete recursively
-vDisplay the detailed execution process of the command

Reference example:

  • Ask for confirmation one by one before deleting:
[root@wayne:~]# rm -i myfile.txt
rm: remove regular empty file 'myfile.txt'? 
  • Delete directly without any prompt:
[root@wayne:~]# rm -f myfile.txt
  • Recursively delete a directory and all files in it:
[root@wayne:~]# mkdir dirs/subdir/dir1 
[root@wayne:~]# rm -rf dirs/subdir/dir1 
  • Delete all files in the current directory:
[root@wayne:~]# rm -rf *
  • Clear all files in the system (<font color='red'>use with caution</font>):
[root@wayne:~]# rm -rf /*

5.mv command

Abbreviation for move, used to move files or rename them

Syntax format: mv [parameters]

Common parameters:

parameterillustrate
-iIf a file with the same name exists, ask the user whether to overwrite
-fWhen overwriting existing files, no prompts will be given
-bIf the file exists, create a backup of it before overwriting
-uThe move operation is only performed when the source file is newer than the target file, or the target file does not exist

Reference example:

  • Rename the file file1 to file2:
[root@wayne:~]# mv file1 file2
  • Move the file myfile into the directory dir:
[root@wayne:~]# mv myfile /dir
  • Move directory dir1 to directory dir2 (directory dir2 already exists, if it does not exist, rename it):
[root@wayne:~]# mv /dir1 /dir2
  • Move all files in the srcdir directory to the current directory:
[root@wayne:~]# mv srcdir/* .

6.cp command

Copy abbreviation, used to copy files or directories

Syntax format: cp [parameter] [file]

Common parameters:

parameterillustrate
-fIf the target file already exists, it will directly overwrite the original file
-iIf the target file already exists, it will ask whether to overwrite
-rCopy files and directories recursively
-dWhen copying a symbolic link, the target file or directory is also established as a symbolic link, and points to the original file or directory linked to the source file or directory
-lMake a hard link to the source file instead of copying the file
-sCreate a symbolic link to the source file instead of copying the file

Reference example:

  • Copy directory:
[root@wayne:~]# cp -r dir1 dir2/
  • Copy the file srcfile into the file dstfile:
[root@wayne:~]# cp srcfile  dstfile
  • Copy multiple files to the dir directory:
[root@wayne:~]# cp -r file1 file2 file3 dir
  • Copy all .c files in the /usr/wayne directory to the directory dstdir
[root@wayne:~]# cp -r /usr/wayne/*.c dstdir

7 cat command

Used to display the contents of the file

Grammar format: cat [parameter] [file]

Common parameters:

parameterillustrate
-nnumber all output lines
-bNo numbering for blank lines
-sTwo or more consecutive blank lines, replaced by one blank line

Reference example:

  • Print the file contents without line numbers:
[root@wayne:~]# cat file
  • Print file contents with line numbers:
[root@wayne:~]# cat -n file

8.pwd command

Abbreviation for print working directory, used to print the working directory

Reference example

View the current working directory path:

[[root@wayne:~]# pwd
/home/wayne

9.ls command

The abbreviation of list, which is used to list the contents of the specified directory and its related attribute information

Syntax format: ls [parameter] [file]

Common parameters:

parameterillustrate
-aShow all files and directories (including hidden files starting with ".")
-ASame as -a, but do not list "." (current directory) and ".." (parent directory)
-lList file and directory information using long format
-rDisplay files in reverse order (alphabetical order by default)
-tSort by last modification time
-SSort by file size
-RList all subdirectories recursively

Reference example:

  • view files in the current directory
[root@wayne:~]# ls -a
  • View all files in the current directory (including hidden files starting with .):
[root@wayne:~]# ls -a
  • View detailed information about files and directories:
[root@wayne:~]# ls -l
  • View all files under the root directory (/):
[root@wayne:~]# ls / 
  • List all files in the current working directory whose names begin with "s":
[root@wayne:~]# ls -ltr s*
  • List the details of all directories and files under the /bin directory:
[root@wayne:~]# ls -lR /bin
  • List all files and directories in the current working directory and sort them by file size:
[root@wayne:~]# ls -AS

10.cd command

Abbreviation for change directory, used to switch to the specified directory

Syntax format: cd [parameter] [directory name]

Common parameters:

parameterillustrate
-Return to last directory
~Switch to the current user directory
..Switch to the previous directory

Reference example:

  • Switch the current working directory to the dir directory
[root@wayne:~]# cd dir
  • Switch to the current user directory:
[root@wayne:~]# cd ~
  • Switch to the upper directory:
[root@wayne:~]# cd ..
  • Switch to the upper two directories:
[root@wayne:~]# cd ../..
  • Change to the root directory:
[root@wayne:~]# cd /

---------------—

The code word is not easy, just like it and go! Private messages are also welcome to communicate together!

Tags: AI

Posted by Scip on Tue, 07 Mar 2023 17:58:48 +1030