Sorting of common Linux commands

Keywords: Linux

1. Delete folders and files

rm command

Permission: all users can use rm command to delete directories on the terminal.

Format:

rm [option] DirName

Description of rm command options

The [options] in the command generally include the following:

-i  #Ask for confirmation one by one before deleting.
-f  #Even if the original file attribute is set to read only, it can be deleted directly without confirming one by one.
-r  #Delete the directory and the following files one by one.

(- r is downward recursion. No matter how many levels of directories there are, they will be deleted together
-f means to delete directly without any prompt)

Example of rm command

Example 1: delete all C language program documents; Ask for confirmation one by one before deleting.

rm -i *.c

Example 2: delete the Finished subdirectory and all files in the subdirectory.

rm -r Finished

Note: Linux does not have a recycle bin. Be careful when using the rm command. It can't be recovered after deletion.

rm -rf folder
rm -r folder

Example 3:

#Delete folder instance:
rm -rf /var/log/httpd/access
#The / var/log/httpd/access directory and all files and folders under it will be deleted

#Delete file usage instance:
rm -f /var/log/httpd/access.log
#The file / var/log/httpd/access.log will be forcibly deleted

Reference: [commands for creating and deleting files and folders in Linux learning]( (42 messages) commands for creating and deleting files and folders in linux learning blog of Dapeng Xiaozhan - CSDN blog _linuxcommand for deleting files)

2. Copy of files

#Command format: cp [-adfilprsu] source file destination file
cp [option] source1 source2 source3 ... directory

Parameter Description:

-a #archive means to copy all directories
-d #If the source file is a link file, the attributes of the link file are copied instead of the file itself
-f #Force: in case of repetition or other questions, the user will not be asked, but forced replication
-i #If the destination file already exists, you will be asked if you really want to overwrite it
-l #Establish a hard link connection file instead of copying the file itself
-p #Copy with the properties of the file instead of using the default properties
-r #Recursive replication, used for directory replication (= = replication folder = =)
-s #Copy to a symbolic link file, the shortcut file
-u #If the target file is older than the source file, update the target file 

cp -a copy a file while retaining the attributes of the original file

cp -r dirname (source file) destdi (destination file)

After copying a directory, its file properties change
To make the copied directory exactly the same as the original directory, including file permissions, you can use cp -a

Example: copy file1 in / test1 directory to / test3 directory, and change the file name to file2. You can enter the following command:

cp /test1/file1 /test3/file2

Reference: [usage of linux command cp -a]( (42 messages) usage of linux command cp -a blog of 335046781 - CSDN blog)

[how linux copies and moves folders]( (42 messages) how linux copies folders and moves folders _yanzj's blog - CSDN blog _howlinux copies folders)

3. Move files, folders

Format:

mv [option(option)] Source file or directory destination file or directory

Use command:

mv  webdata  /bin/usr/

It can be extended:

mv    /usr/lib/*    /zone

Is to move everything under / usr/lib / to / zone /.

mv    /usr/lib/*.txt    /zone

Is to move all files ending in txt under lib to / zone. Other types, and so on.

The details are as follows:

effect

mv command to rename a file or directory or move a file from one directory to another. This command is equivalent to the combination of ren and move commands in DOS system. Its permission is for all users.

format

mv [options] Source file or directory destination file or directory

[options] main parameters (several important parameters)

  • -i: Interactive operation. If mv operation will result in overwriting the existing target file, the system asks whether to rewrite it and asks the user to answer "y" or "n", so as to avoid overwriting the file by mistake.
  • -f: Interactive operation is prohibited. mv operation will not give any indication when it wants to overwrite an existing target file. After specifying this parameter, the i parameter will no longer work. - r indicates mv to move all directories and subdirectories listed in the parameter recursively.

Second parameter

  • When the second parameter type is file, the mv command completes the file rename, which renames the given source file or directory to the given target file name.
  • When the second parameter is the existing directory name, there can be multiple source files or directory parameters. The mv command moves the source files specified by each parameter to the target directory. When moving files across file systems, mv copies the files first, and then deletes the original files, and the links to the files will also be lost.

reference resources: How to move all files in folders and files under linux practical commands

4. Enter and return to the specified directory

#get into
cd file/file_1
#Return to the previous level
cd ..

5. Lists the file directory name and file name

Command format

ls [option] [file name]

Command function
It is used to print the current directory list or print the files and file list in the specified directory. ls command can also view file permissions, directory information, etc. when printing the file list.

-a #List all files in the directory, including implied files starting with

-l #Use a long listing format

Reference: [Linux ls command]( (42 messages) Linux ls command _micjlxx blog - CSDN blog _lscommand)

**-l * * detailed explanation

[linux ls -l details]( (42 messages) linux ls -l details July rain - CSDN blog)

6. Compare differences between two files

diff(option)(parameter)

-b or – ignore space change does not check for differences in space characters

-i or – ignore case does not check for differences in case

Reference: [Linux diff command]( (42 messages) Linux diff command, calligraphy, ink and ink -CSDN blog)

cmp [option] [File 1] [Document 2]

Function: the cmp command compares the contents of two files byte by byte. If the contents of two files are complete, the cmp command does not display any content. If the contents of two files are different, the number of bytes and lines of the first difference will be displayed. If the file is "-" Or not given, the content is read from standard input. Generally, cmp command is used to compare binary files, and diff command is used to compare text files

Reference: [Linux cmp command and diff command]( (42 messages) Linux cmp command and diff command the small building listens to the spring rain all night, and sells apricot flowers in the deep lane in the Ming Dynasty - CSDN blog)

7. View disk usage

df

Parameters:

-a #All: list of all file systems
-T #Displays the file system type

Reference: [linux] - df command]( (42 messages) [linux] - df command _hgelin blog - CSDN blog)

8. View file space

du 

Function: displays the disk space used by each file and directory, that is, the size of the file (in block by default)

-h #Display in K M G to improve readability

Reference: [du, df and free in Linux and their differences]( (42 messages) du, df and free in Linux and their differences_ "Huahua" childe_ Dragon blog - CSDN blog)

9. View user and group names

id command

id Option parameters
id User name displays information about the specified user
id –u User name displays the name of the specified user uid
id –g User name displays the primary group of the specified user gid
id –un User name displays the name of the specified user
id –gn User name displays the primary group of the specified user gid
#If the user name is not added, the current user information will be displayed

whoami command

whoami(option)

The whoami command is used to print the currently valid user name, which is equivalent to executing the id -un command.

reference resources: Linux system management: detailed explanation of whoami command

linux id usage

10. Find file

find command format:

find   path  -option  [ -print ]  [ -exec   -ok   |xargs  |grep  ] [  command  {} \;  ]

Parameters of find command:

1) Path: the directory path to find.

  ~ express $HOME catalogue
   . Represents the current directory
   / Represents the root directory 

2) print: indicates that the result is output to standard output.

3) exec: execute the shell command given by this parameter on the matching file.
The form is command {};, Attention {} and; Space between

4) ok: same as exec,
The difference is that you will be prompted to confirm whether to execute the command before executing the command

5) |xargs plays the same role as exec and plays a role of undertaking

The difference is that xargs is mainly used to undertake deletion operations, while - exec can be used, such as copy, move, rename, etc

6) options: indicates the search method

Options the following options are commonly used:

-name   filename               #Find a file named filename
-perm                                #Find by execution permission
-user    username             #Find by file owner
-group groupname            #Find by group
-mtime   -n +n                   #Find files by file change time, - N refers to within n days and + n refers to before n days
-atime    -n +n                   #Find files by file access time, - N refers to within n days and + n refers to before n days
-ctime    -n +n                  #Find files by file creation time, - N refers to within n days and + n refers to before n days
-nogroup                          #Check the file without valid group, that is, the group of the file does not exist in / etc/groups
-nouser                            #Check that there is no valid owner of the file, that is, the owner of the file does not exist in / etc/passwd
-type    b/d/c/p/l/f             #Query is a block device, directory, character device, pipeline, symbolic link, ordinary file
-size      n[c]                    #Look up a file with a length of n blocks [or n bytes]
-mount                            #Do not cross the file system mount point when querying files
-follow                            #If a symbolic link file is encountered, the file to which the link refers is tracked
-prune                            #Ignore a directory

Reference: [detailed explanation of find command under Linux]( (43 messages) detailed explanation of the find command under Linux _liangkk blog - CSDN blog)

11. Character interception

cut

The cut command cuts bytes, characters, and fields from each line of the file and writes them to standard output.

  • -b: Splits in bytes. These byte positions ignore multi byte character boundaries unless the - n flag is also specified.
  • -c: Split in characters.
  • -d: Custom separator, tab by default.
  • -f: Used with - d to specify which area to display.
  • -n: Unwrap multibyte characters. Only used with the - b flag.

Reference: [Linux character interception command - cut]( (43 messages) Linux character interception command - cut_; craftsman - CSDN blog)

12. Exercise link

Linux command line exercise

Posted by NeoGeo on Thu, 18 Nov 2021 07:30:10 -0800