Linux common commands

Posted by evdawg80 on Thu, 24 Feb 2022 10:26:13 +0100

Basic format of Linux commands

The basic format of commands in Linux is: Command [- option] [parameter]
For example: ls -la /etc

  1. Individual commands do not follow this format
  2. When there are multiple options in a single, they can be written together
  3. Short and full options
    For example: - a and – all

jurisdiction

In Linux systems, permissions are represented by the following characters

  1. File type: - binary file d directory l soft link file
  2. r means readable, w means writable, and x means executable
  3. u owner g all groups o others
    r. w and x are followed by several - to represent who can use them
    For example, r - means that everyone is readable, r - means that the group is readable, and three - means that others can use it. The same is true for r and w
    For example:
    drwxr-x---.  2 root root      6 9 June 30, 2020 sudoers.d
    
    This file is represented as a directory. Everyone (the owner of this file) can read, write and execute, and others only have execution permission

Directory processing command

ls

Command name: ls
Command English original meaning: list
Command path: / bin/ls
Execute permission limit: all users
Function Description: display directory file
Syntax: ls option [- ald] [file or directory]
   - a: show all files, including hidden files
   - l: detailed information display
   - d: View directory properties
   - i: check the file id number
   - h: displays the file size

When using the ls command, you can add a path at the end of the command to display the specified path

ls /usr
bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp

mkdir

Command name: mkdir
make directories
Command path: / bin/mkdir
Execution Authority: all users
Syntax: mkdir -p [directory name]
  Function Description: create a new directory
If one directory does not exist, it will be created recursively. If the other directory does not exist, it will be created recursively

Basic use

a folder is created in the tmp directory

[root@localhost tmp]# mkdir /tmp/a

Three folders a/b/c are created in the tmp directory. There are b under a and c under b

[root@localhost tmp]# mkdir -p /tmp/a/b/c

If you want to create multiple directories at the same time, you can add spaces after another directory and continue to add new directories. In this way, we have added a1, a2 and a3 directories under the tmp directory at the same time, and there are other directories under the three directories

[root@localhost tmp]# mkdir -p  /tmp/a1/b1 /tmp/a2/b2 /tmp/a3/b3

cd

Command name: cd
Command English original meaning: change directory
Command path: shell built-in command
Execution Authority: all users
Syntax: cd [directory]
  Function Description: switch directory
  ..: Indicates the previous level
  .: Indicates the current directory

Switch directory to etc / alternatives

[root@localhost etc]# cd /etc/alternatives/

pwd

Command name: pwd
Original English meaning of the command: print working directory
Command path: / bin/pwd
Execution Authority: all users
Syntax: pwd
Function Description: displays the current directory

[root@localhost alternatives]# pwd
/etc/alternatives

rmdir

Only empty directories can be deleted

Command name: rmdir
English original meaning of the command: remove empty directories
Command path: / bin/rmdir
Execution Authority: all users
Syntax: rmdir [directory name]
Function Description: delete empty directory

Deleted / b1 folder under tmp/a1

[root@localhost alternatives]# rmdir /tmp/a1/b1

cp

Command name: cp
Command English original meaning: copy
Command path: / bin/cp
Execution Authority: all users
Syntax: cp [original file or directory] [destination directory]
   - r: copy directory
   - p: keep the attributes of the copied file
Function Description: copy directory or file

Copy a1 to a2

[root@localhost alternatives]# cp -r /tmp/a1 /tmp/a2

If you want to copy multiple files or directories at one time, you only need to add multiple original file directory addresses and finally add the target directory. Next, we will add a1 and a2 to a3 together

[root@localhost alternatives]# cp -r /tmp/a1 /tmp/a2 /tmp/a3

If we want to change the name when copying, just add the desired name after the target directory
Copy the a1 folder to a3 and rename it a5

[root@localhost alternatives]# cp -r /tmp/a1  /tmp/a3/a5

mv

Command name: rm
Command English original meaning: move
Command path: / bin/mv
Execution Authority: all users
Syntax: mv [original text or directory] [target directory]
Function Description: cutting files and renaming

Copy a1 to a3

[root@localhost ~]# mv /tmp/a1 /tmp/a3

The renaming operation is consistent with the cp command. If the original file directory is the target directory, the renaming operation is realized without moving

rm

Command name: rm
English original meaning of the command: remove
Command path: / bin/rm
Execution Authority: all users
Syntax: rm [file or directory]

   - r: delete directory
   - f: forced deletion
Function Description: delete file

When deleting a file, you will be asked whether you want to delete the file. If - f is added, you will not be asked and can be deleted directly

Normal deletion

[root@localhost ~]# rm -r /tmp/a3
rm: Enter directory"/tmp/a3"? y
rm: Enter directory"/tmp/a3/a1"? y
rm: Delete directory "/tmp/a3/a1/b2"?y
rm: Delete directory "/tmp/a3/a1"?y
rm: Delete directory "/tmp/a3"?y

Force deletion

[root@localhost ~]# rm -rf /tmp/a3

touch

Command name: touch
Command path: / bin/touch
Execution Authority: all users
Syntax: touch [file name]
Function Description: create an empty file

Create file in current directory

[root@localhost tmp]# touch touchFile

Creates an empty file in the specified directory

[root@localhost /]# touch /tmp/touchFile2

cat

File name: cat
Command path: / bin/cat
Execution Authority: all users
Syntax: cat [file name]
Function Description: display file content
   - n: displays the line number

Displays the issue file under / etc

[root@localhost /]# cat -n  /etc/issue
     1  \S
     2  Kernel \r on an \m
     3  

more

Command name: more
Command path: / bin/more
Execution Authority: all users
Function Description: display file contents in pages
Syntax: more [file name]
  space or f: page turning
  Enter: line feed
  Q or Q: exit

less

Command name: less
Command path: / usr/bin/less
Execution Authority: all users
Syntax: less [file name]
Function: display files in pages, press and hold PageUp and wave PageDown to turn pages up and down
When viewing a file, use / name to search the content and press n to view the next one

head

Command name: head
Command path: / usr/bin/head
Execution Authority: all users
Syntax: head [file name]
Function: display the first few lines of the file
   - n: displays the number of rows
10 lines are displayed by default

[root@localhost /]# head /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
[root@localhost /]# head -n 5 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10

tail

Command name: tail
Command path: / usr/bin/tail
Execution Authority: all users
Syntax: tail [file name]
Function: display the last few lines of the file
   - n: displays the number of rows
10 lines are displayed by default

ln

Command name: ln
Command path: / bin/ln
Execution Authority: all users
Syntax: ln -s [source file] [target file]
   - s: create soft connection
Function: generate linked files

The difference between soft link and hard link

  1. When generating a connection, if - s is not applicable, a hard link is generated
  2. The permission of soft connection is three rwx, which means that everyone can modify it
  3. The file size of soft connection is small, only a dozen bytes.
  4. When viewing the directory information, the soft connection will have an arrow pointing to the soft file after the name.
  5. Soft connection has an l before the permission, which means it is a link
  6. The permission of the soft connection does not represent the permission of the source file
  7. The soft link is equivalent to a shortcut under windows. It is just a link to the original file. The hard link has the same file attributes as the original file, including file id, size and permission. If you modify the hard link, the original file will also be modified, and the modified hard link file will be synchronized to the original file in real time.
  8. Hard links cannot be created across drive letters, while soft links can
  9. Hard links cannot point to directories, but only to files. Otherwise, an error will be reported that hard links are not allowed to point to directories

Soft connection

[root@localhost /]# ln -s /tmp/a /tmp/a.soft
drwxr-xr-x. 3 root root  15 2 February 23:20 a
lrwxrwxrwx  1 root root   6 2 March 24:00 a.soft -> /tmp/a

Hard link

[root@localhost /]# ln /etc/issue /tmp/issue.hard
[root@localhost /]# ls -l /tmp/issue.hard
-rw-r--r--. 2 root root 23 10 May 23, 2020 /tmp/issue.hard
[root@localhost /]# ls -l /etc/issue
-rw-r--r--. 2 root root 23 10 May 23, 2020 /etc/issue

We can see that the hard link is the same as the original file in terms of permission, size and file id

Topics: Linux bash server