Linux basic commands

Posted by medar on Sat, 06 Nov 2021 16:53:58 +0100

Linux system can use man [command] to view the use documents of each command.

Directory management

  • The directory structure of Linux is a tree structure, and the top-level directory is the root directory /.

  • Other directories are added to the tree by mounting and removed by unmounting.

First, we need to know what absolute path and relative path are.

Absolute path

Write from the root directory /, for example: / usr/share/doc.

Relative path

For example, when / usr/share/doc wants to go under / usr/share/man, it can be written as:... / man.

Common commands for working with directories

Common commands for processing directories:

  • ls: list directories.
  • cd: switch directories.
  • pwd: displays the current directory.
  • mkdir: create a new directory.
  • rmdir: delete an empty directory.
  • cp: copy files or directories.
  • rm: remove a file or directory.
  • mv: move files and directories (can also be used to modify the names of files and directories).

You can use man [command] to view the use documents of various commands, such as man cp.

ls

In Linux systems, the ls command is probably the most frequently run.

grammar

ls [-aAdfFhilnrRSt] Directory name

parameter

  • -a: All files are listed together with hidden files (files starting with). In common use.
  • -l: Long data list, including file attributes, permissions and other data (commonly used).
  • -i: Displays inode node information for the file.

List all files in the directory (including attributes and hidden files)

[root@sail /]# ls -al
total 68
dr-xr-xr-x. 18 root root  4096 Oct 25 21:28 .
dr-xr-xr-x. 18 root root  4096 Oct 25 21:28 ..
lrwxrwxrwx.  1 root root     7 Jul 11  2019 bin -> usr/bin
dr-xr-xr-x.  5 root root  4096 Jul 11  2019 boot
drwxr-xr-x  19 root root  2980 Oct 31 22:57 dev
drwxr-xr-x. 77 root root  4096 Oct 31 22:54 etc
drwxr-xr-x.  3 root root  4096 Oct 25 21:14 home
lrwxrwxrwx.  1 root root     7 Jul 11  2019 lib -> usr/lib
lrwxrwxrwx.  1 root root     9 Jul 11  2019 lib64 -> usr/lib64
drwx------.  2 root root 16384 Jul 11  2019 lost+found
drwxr-xr-x.  2 root root  4096 Apr 11  2018 media
drwxr-xr-x.  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x.  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x  90 root root     0 Oct 31 22:54 proc
dr-xr-x---.  5 root root  4096 Oct 31 22:54 root
drwxr-xr-x  23 root root   640 Oct 31 22:55 run
lrwxrwxrwx.  1 root root     8 Jul 11  2019 sbin -> usr/sbin
drwxr-xr-x.  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x  13 root root     0 Nov  1  2021 sys
drwxrwxrwt. 10 root root  4096 Oct 31 22:55 tmp
drwxr-xr-x. 13 root root  4096 Jul 11  2019 usr
drwxr-xr-x. 19 root root  4096 Jul 11  2019 var

cd

cd is the abbreviation of Change Directory, which is the command used to change the working directory.

grammar

cd [Relative path or absolute path]

Switch to user directory

[root@sail /]# cd home
[root@sail home]# 

Go back to the next level

[root@sail home]# cd ..
[root@sail /]# 

Go back to the root directory

[root@sail /]# cd /
[root@sail /]# 

Go back to the root directory

[root@sail /]# cd root
[root@sail ~]# 

pwd

pwd is the abbreviation of Print Working Directory, that is, the command to display the current directory.

grammar

pwd [-P]

parameter

-P: Show the exact path instead of using the link path.

Simply show the current working directory

[root@sail ~]# pwd
/root

If it is a link, to display the real address, you can use the - P parameter

[root@sail ~]# cd /bin
[root@sail bin]# pwd
/bin
[root@sail bin]# pwd -P
/usr/bin

mkdir

make directory to create a new directory

grammar

mkdir [-mp] Directory name

Parameters:

  • -m: Permissions for the configuration file. You can configure it directly without looking at the default permissions (umask).
  • -p: Help you directly create the required directory (including the upper level directory) recursively.

Create directory

[root@sail home]# mkdir sail
[root@sail home]# ls
admin  sail

Create multi-level directory

# There is no way to create a multi-level directory directly
[root@sail home]# mkdir test1/test2/test3/test4
mkdir: cannot create directory 'test1/test2/test3/test4': No such file or directory
# The - p parameter needs to be added
[root@sail home]# mkdir -p test1/test2/test3/test4
[root@sail home]# ls
admin  test1

Create a directory with rwx--x--x permission

[root@sail home]# mkdir -m 711 test2
[root@sail home]# ls -l
total 12
drwx------ 3 admin admin 4096 Oct 29 09:56 admin
drwxr-xr-x 3 root  root  4096 Oct 31 23:19 test1
drwx--x--x 2 root  root  4096 Oct 31 23:21 test2

rmdir

Delete directory

grammar

rmdir [-p] Directory name

parameter

-p: It is also deleted together with the next level (empty) directory

remove empty directories

[root@sail home]# rmdir test2
[root@sail home]# ls
admin  test1

Delete non empty directory

# The directory cannot be deleted because it is not empty
[root@sail home]# rm test1
rm: cannot remove 'test1': Is a directory
# Add the parameter - p to delete test1/test2/test3/test4 in turn
[root@sail home]# rmdir -p test1/test2/test3/test4
[root@sail home]# ls
admin

This rmdir can only delete empty directories. You can use the rm command to delete non empty directories.

cp (copy file or directory)

Syntax:

[root@www ~]# cp [-adfilprsu] source destination
[root@www ~]# cp [options] source1 source2 source3 .... directory

Options and parameters:

  • -a: Equivalent to - pdr. For pdr, please refer to the following instructions; (common)
  • -p: Copy it together with the attributes of the file instead of using the default attributes (commonly used for backup);
  • -d: If the source file is a link file attribute, copy the link file attribute instead of the file itself;
  • -r: Recursive continuous replication is used for directory replication behavior; (common)
  • -f: To force, if the target file already exists and cannot be opened, remove it and try again;
  • -i: If the destination file already exists, you will first ask about the progress of the action (common) when overwriting
  • -l: Create a hard link link file instead of copying the file itself.
  • -s: Copy it into a symbolic link, that is, a "shortcut" file;
  • -u: If destination is older than source, upgrade destination!

Test:

# Find a directory with files. I'll find the root directory here
[root@kuangshen home]# cd /root
[root@kuangshen ~]# ls
install.sh
[root@kuangshen ~]# cd /home

# Copy install.sh under the root directory to the home directory
[root@kuangshen home]# cp /root/install.sh /home
[root@kuangshen home]# ls
install.sh

# Copy again and add the - i parameter to increase the coverage query?
[root@kuangshen home]# cp -i /root/install.sh /home
cp: overwrite '/home/install.sh'? y # n is not covered, y is covered

rm (remove file or directory)

Syntax:

rm [-fir] File or directory

Options and parameters:

  • -f: force means to ignore nonexistent files and no warning message will appear;
  • -i: In interactive mode, the user will be asked whether to move before deletion
  • -r: Recursive deletion! Most commonly used in directory deletion! This is a very dangerous option!!!

Test:

# Delete the install.sh just created in the cp instance!
[root@kuangshen home]# rm -i install.sh
rm: remove regular file 'install.sh'? y
# If you add the - i option, you will actively ask oh, to avoid you deleting the wrong file name!

# Try not to use rm -rf on the server/

mv (move files and directories, or change names)

Syntax:

[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory

Options and parameters:

  • -f: force means that if the target file already exists, it will not be queried but directly overwritten;
  • -i: If the destination file already exists, you will be asked whether to overwrite it!
  • -u: If the target file already exists and the source is relatively new, it will be updated

Test:

# Copy a file to the current directory
[root@kuangshen home]# cp /root/install.sh /home

# Create a folder test
[root@kuangshen home]# mkdir test

# Move the copied file to the directory we created and view it
[root@kuangshen home]# mv install.sh test
[root@kuangshen home]# ls
test
[root@kuangshen home]# cd test
[root@kuangshen test]# ls
install.sh

# Rename the folder and view it again!
[root@kuangshen test]# cd ..
[root@kuangshen home]# mv test mvtest
[root@kuangshen home]# ls
mvtest

file management

create a file

touch

Create a file in the current directory

grammar

touch file name 

demonstration

[root@sail home]# touch f1
[root@sail home]# ls
admin  f1

echo

Input content to specified file

grammar

echo "content" >>file name

demonstration

[root@sail home]# echo "I am f1 file" >>f1
[root@sail home]# cat f1
I am f1 file

Basic properties

File properties

introduce

Linux system is a typical multi-user system. Different users are in different positions and have different permissions.

In order to protect the security of the system, the Linux system makes different provisions for different users to access the same file (including directory file).

In Linux, we can use ls – l or ll commands to display the attributes of a file and the users and groups to which the file belongs, such as:

[root@sail /]# ls -l
total 60
lrwxrwxrwx.  1 root root     7 Jul 11  2019 bin -> usr/bin
dr-xr-xr-x.  5 root root  4096 Jul 11  2019 boot
drwxr-xr-x  19 root root  2980 Oct 31 22:57 dev
drwxr-xr-x. 77 root root  4096 Oct 31 22:54 etc
drwxr-xr-x.  3 root root  4096 Oct 31 23:28 home
lrwxrwxrwx.  1 root root     7 Jul 11  2019 lib -> usr/lib
lrwxrwxrwx.  1 root root     9 Jul 11  2019 lib64 -> usr/lib64
drwx------.  2 root root 16384 Jul 11  2019 lost+found
drwxr-xr-x.  2 root root  4096 Apr 11  2018 media
drwxr-xr-x.  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x.  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x  89 root root     0 Oct 31 22:54 proc
dr-xr-x---.  5 root root  4096 Oct 31 22:54 root
drwxr-xr-x  23 root root   640 Oct 31 22:55 run
lrwxrwxrwx.  1 root root     8 Jul 11  2019 sbin -> usr/sbin
drwxr-xr-x.  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x  13 root root     0 Oct 31 23:00 sys
drwxrwxrwt.  8 root root  4096 Nov  5 03:42 tmp
drwxr-xr-x. 13 root root  4096 Jul 11  2019 usr
drwxr-xr-x. 19 root root  4096 Jul 11  2019 var
[root@sail /]# ll
total 60
lrwxrwxrwx.  1 root root     7 Jul 11  2019 bin -> usr/bin
dr-xr-xr-x.  5 root root  4096 Jul 11  2019 boot
drwxr-xr-x  19 root root  2980 Oct 31 22:57 dev
drwxr-xr-x. 77 root root  4096 Oct 31 22:54 etc
drwxr-xr-x.  3 root root  4096 Oct 31 23:28 home
lrwxrwxrwx.  1 root root     7 Jul 11  2019 lib -> usr/lib
lrwxrwxrwx.  1 root root     9 Jul 11  2019 lib64 -> usr/lib64
drwx------.  2 root root 16384 Jul 11  2019 lost+found
drwxr-xr-x.  2 root root  4096 Apr 11  2018 media
drwxr-xr-x.  2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x.  2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x  89 root root     0 Oct 31 22:54 proc
dr-xr-x---.  5 root root  4096 Oct 31 22:54 root
drwxr-xr-x  23 root root   640 Oct 31 22:55 run
lrwxrwxrwx.  1 root root     8 Jul 11  2019 sbin -> usr/sbin
drwxr-xr-x.  2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x  13 root root     0 Oct 31 23:00 sys
drwxrwxrwt.  8 root root  4096 Nov  5 03:42 tmp
drwxr-xr-x. 13 root root  4096 Jul 11  2019 usr
drwxr-xr-x. 19 root root  4096 Jul 11  2019 var

The first column of the display result is the file attribute, which represents the type and permission of the file.

The first character in the file attribute represents the type of the file:

  • d: Catalogue
  • -: documents;
  • l: link file;
  • b: Interface equipment for storage in the device file (random access device);
  • c: Serial port devices in device files, such as keyboard and mouse (one-time reading device).

In the following characters, three are a group, and all are combinations of three parameters of rwx. Of which:

  • r stands for read.
  • w stands for write able.
  • x stands for execute.

The position of these three permissions will not change. If there is no permission, a minus sign will appear instead.

The attributes of each file are determined by the 10 characters in the first part on the left, as shown in the following figure:

Numbers from left to right are 0 - 9:

  • Bit 0 determines the file type.
  • Bits 1 - 3 determine that the owner (the owner of the file) has permissions for the file.
  • Bits 4 - 6 determine that the owning group (the same group user of the owner) has the permission of the file.
  • Bits 7 - 9 determine that other users have permissions on the file.

Of which:

  • Bits 1, 4 and 7 indicate read permission. If it is represented by r character, it has read permission; if it is represented by - character, it has no read permission.

  • Bits 2, 5 and 8 indicate write permission. If w character is used, there is write permission. If - character is used, there is no write permission.

  • Bits 3, 6 and 9 indicate the executable permission. If it is represented by the x character, it has the execution permission; if it is represented by the - character, it has no execution permission.

Owner: for a file, it has a specific owner, that is, the user who owns the file.

Group: in Linux system, users are classified by group. A user belongs to one or more groups.

Users other than file owners can be divided into the same group of file owners and other users.

Therefore, the Linux system specifies different file access permissions according to file owners, users in the same group of file owners and other users.

In the above example, the boot file is a directory file, and both the owner and the group are root.

modify

chgrp

Change the group to which the file belongs.

grammar

chgrp [-R] Group name file name

parameter

-R: Recursively change the file group. When changing the group of a directory file, if the - R parameter is added, the group of all files in the directory will be changed.

chown

You can change the file owner or the file group at the same time.

grammar

chown [–R] Master name file name
chown [-R] Subject name:Group name file name

parameter

-R: Recursively change the ownership of the file. When changing the owner of a directory file, if the - R parameter is added, the owner of all files in the directory will be changed.

chmod

Change 9 properties of the file

grammar

chmod [-R] xyz File or directory

Set properties in numbers

There are two ways to set Linux file attributes, one is number and the other is symbol.

There are nine basic permissions for Linux files: owner, group and others. Each has its own read, write and execute permissions.

The permission character of the file is rwx rwx rwx. These nine permissions are grouped in three.

Among them, we can use numbers to represent each permission. The score comparison table of each permission is as follows:

  • r: 4
  • w: 2
  • x: 1

The three permission scores of owner, group and others of each identity need to be accumulated. For example, when the permission is: rwxrwx --- the score is:

  • owner = rwx = 4+2+1 = 7
  • group = rwx = 4+2+1 = 7
  • others = --- = 0+0+0 = 0
chmod 770 filename

Content view

summary

In Linux system, use the following command to view the contents of the file:

  • cat: displays the contents of the file starting from the first line.
  • tac: starting from the last line, we can see that tac is written upside down of cat.
  • nl: the line number will be output on the way when it is displayed.
  • more: displays the contents of the file page by page.
  • less: similar to more, but better than more, it can turn the page forward.
  • head: just look at the first few lines.
  • tail: just look at the last few lines.

command

cat

The contents of the file are displayed starting from the first line.

grammar

cat [-AbEnTv]

demonstration

[root@sail ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

parameter

  • -A: The integration option equivalent to - vt can list some special characters instead of blanks.
  • -b: List line numbers. Only non blank lines are displayed. Blank lines are not marked with line numbers.
  • -E: Displays the ending line breaking byte $.
  • -n: Print out the line number. There will also be line numbers along with blank lines, which is different from the option of - b.
  • -T: Display the [tab] key as ^ I.
  • -v: List some special characters that you can't see.

tac

tac is the reverse of cat command, and the meaning is also obvious: display the file content from the last line.

demonstration

[root@sail ~]# tac /etc/sysconfig/network-scripts/ifcfg-eth0
ONBOOT=yes
BOOTPROTO=dhcp
DEVICE=eth0

nl

View the file and display the line number

grammar

nl [-bnw] file

demonstration

[root@sail ~]# nl /etc/sysconfig/network-scripts/ifcfg-eth0
     1	DEVICE=eth0
     2	BOOTPROTO=dhcp
     3	ONBOOT=yes

parameter

  • -b: There are two ways to specify the line number:
    • -b a: the line number (similar to cat -n) is also listed whether it is empty or not.
    • -b t: if there is an empty line, do not list the line number of the empty line (default value).
  • -n: There are three main methods of line number representation:
    • -n ln: the line number is displayed on the far left of the screen.
    • -n rn: the line number is displayed on the rightmost side of its own field without adding 0.
    • -n rz: the line number is displayed on the rightmost side of its own field, plus 0.
  • -w: The number of digits occupied by the line number field.

more

Page by page.

Key

During the operation of the more program, you have several keys to press:

  • Blank key: represents turning down a page.
  • Enter key: represents turning down one line.
  • /: means to search down the keyword in the displayed content.
  • f: Immediately display the file name and the number of lines currently displayed;
  • q: The representative leaves more immediately and no longer displays the contents of the file.
  • b or ctrl b: represents turning back the page, but this action is only useful for files, not pipelines.

less

Page by page

Key

  • Spacebar: turn down one page.
  • Up key: turn down a page.
  • Down key: turn up a page.
  • /: the function of searching down "string".
  • ?: The function of searching up for "string".
  • n: Repeat the previous search (related to and / or?).
  • N: Reverse repeats the previous search (related to and / or?).
  • q: Leave the less program.

head

Take out the first few lines of the file

grammar

head [-n number] file

parameter

-n: Followed by a number represents the meaning of displaying several lines. The first 10 lines are displayed by default.

[root@sail ~]# head -n 15 /etc/csh.login
# /etc/csh.login

# System wide environment and startup programs, for login setup

#add sbin directories to the path
foreach p ( /usr/local/sbin /usr/sbin )
	switch (":${PATH}:")
	case "*:${p}:*":
		breaksw
	default:
		if ( $uid == 0 ) then
                        set path = ( ${p} ${path:q} )
		else
                        set path = ( ${path:q} ${p} )
		endif

tail

Take out the last few lines of the file

grammar

tail [-n number] file

parameter

  • -n: Followed by a number represents the meaning of displaying several lines. The last 10 lines are displayed by default.
[root@sail ~]# tail -n 15  /etc/csh.login
set history=1000

if ( -d /etc/profile.d ) then
        set nonomatch
        foreach i ( /etc/profile.d/*.csh /etc/profile.d/csh.local )
                if ( -r "$i" ) then
	                        if ($?prompt) then
	                              source "$i"
	                        else
	                              source "$i" >& /dev/null
	                        endif
                endif
        end
        unset i nonomatch
endif

link

There are two kinds of Linux links, one is called Hard Link, and the other is called soft link or Symbolic Link.

Hard link

In general, the ln command generates hard links.

Hard links are linked through index nodes.

In Linux, no matter what type of file is saved in the disk partition, it is assigned a number called Inode Index.

In Linux, multiple file names point to the same inode. For example:

If A is the hard link of B (both A and B are file names), the inode node number in A's directory entry is the same as that in B's directory entry.

An inode node corresponds to two different file names, and the two file names point to the same file.

A and B are completely equal to the file system. Deleting either of them will not affect the access of the other.

The function of hard link is to allow a file to have multiple valid path names, so that users can establish hard links to important files to prevent accidental deletion.

The reason is as described above, because there are more than one connection to the index node corresponding to the directory, deleting only one connection does not affect the index node itself and other connections. The connection between the data block of the file and the directory will not be released until the last connection is deleted.

In other words, the condition for the real deletion of files is that all hard connected files related to them are deleted.

Soft link

Another connection is called Symbolic Link, also known as Symbolic Link.

Soft links are similar to Windows shortcuts. It is actually a special file.

In symbolic links, a file is actually a text file that contains the location information of another file. For example:

A is the soft link of B (both a and B are file names). The inode node number in the directory entry of a is different from that in the directory entry of B. A and B point to two different inodes, and then point to two different data blocks.

Only the pathname of B is stored in the data block of A (the directory entry of B can be found according to this).

There is A master-slave relationship between A and B. if B is deleted, A still exists (because the two are different files), but it points to an invalid link.

demonstration

[root@sail home]# touch f1 # Create a test file f1
[root@sail home]# ls
admin  f1
[root@sail home]# ln f1 f2 # Create a hard link file f2 for f1
[root@sail home]# ln -s f1 f3 # Create a symbolic connection file f3 for f1
[root@sail home]# ls -li # -The i parameter displays the inode node information of the file
total 4
1048909 drwx------ 3 admin admin 4096 Oct 29 09:56 admin
 664707 -rw-r--r-- 2 root  root     0 Nov  6 22:09 f1
 664707 -rw-r--r-- 2 root  root     0 Nov  6 22:09 f2
 664708 lrwxrwxrwx 1 root  root     2 Nov  6 22:09 f3 -> f1

It can be seen that the inode node of the hard link file f2 is the same as that of the original file f1, which is 664707, but the inode node of the soft link file is different.

[root@sail home]# echo "I am f1 file" >>f1 # Enter character to f1
[root@sail home]# cat f1
I am f1 file
[root@sail home]# cat f2
I am f1 file
[root@sail home]# cat f3
I am f1 file
[root@sail home]# rm -f f1
[root@sail home]# cat f2
I am f1 file
[root@sail home]# cat f3
cat: f3: No such file or directory

It can be seen that when the original file f1 is deleted, the hard link f2 is not affected, but the soft link f3 is invalid.

The following conclusions can be drawn:

  • Deleting the symbolic connection f3 has no effect on f1 and f2.
  • Deleting hard connection f2 has no effect on f1 and f3.
  • Deleting the original file f1 has no effect on the hard link f2, resulting in the failure of the soft link f3.
  • At the same time, delete the original file f1 and hard connection f2, and the whole file will be deleted.

Topics: Linux