mycpen

Mycpen

记录学习历程与受益知识
github
telegram
bilibili

03_Linux Basics - File Types - Primary and Secondary Prompts - First Prompt - 3 Times - stat - Other Basic Commands

1. stat and ll#

//		stat and ll  stat can only view itself. ll can view the contents under the folder.
//		Option combinations (order does not matter)  e.g. ls -Al  (displays 2 functions together)
//		du -sh  counts the size of files and folders
//		mkdir -p  creates a new directory if the parent directory does not exist
					If the folder exists, it does not report an error
    				//	Often used in scripting
	  		  -v		Provides a reminder (displays that it has been created successfully) (not very useful)

stat and ll and ls -l -d#

//		stat and ll and ls -l -d
			stat can only view the file information of itself (the folder itself)
			ll can view the contents under the folder (inside the folder)
			ls -l -d only views its own detailed information, not the inside (the folder itself)

image-20220310192151920

image-20220310192158823


2. Set {xx,xxx}#

Create multiple folders with one command#

#Note: Key point {
	//		mkdir hunantv/{weishi,jingshi,dushi}		Create multiple folders with one command
		Create folders under the hunantv folder in the China_voice path (hunantv is under China_voice)
		{weishi,jingshi,dushi} represents a set with a common parent directory

image-20220310191803814

		When creating only one folder, do not use { } brackets, as { } will become part of the filename.
//		One command to complete (not commonly used in daily use) with several levels inside
		When the parent directory does not exist, add the -p option

image-20220310191848767


3. {1..100}#

#Note: mkdir sc{1..100}
#Note: rm -rf sc{1..100}
//		mkdir sc{1..100} creates files from 1 to 100
		{1..100} represents the set from 1 to 100 combined with the previous sc
		mkdir can create multiple folders at once
		//	rm -rf sc{1..100}
			rm -rf sc*  	Both can delete
//		mandatory command's
		arguments
//		mkdir -p  creates a new directory if the parent directory does not exist
					If the folder exists, it does not report an error

image-20220310192017460

image-20220310192023763

//		mkdir -v	Provides a reminder

image-20220310192037604


4. du -sh#

//		du -sh  View directory size
		Command  Option  Argument

5. cd#

//		cd -  Returns to the last path
		cd .  Enters the current directory
		cd ..  Returns to the parent directory
		cd ~  Returns to the current user's home directory
		cd    Returns to the current user's home directory
cd command
Purpose: Change working directory (Change Directory)	
Format: cd  [directory location]
#Note: Special directories 
.     Current directory
..     Parent directory
~     User's home directory
-     Last working directory (like the back button on a remote control)
//		useradd xulilin  Create a new user
		cd ~xulilin  Enters the home directory of user xulilin
		[root@localhost xulilin]# pwd
		/home/xulilin
//		mkdir	Create a new directory
		touch qijian	Create an empty file
		touch hengshan/yilin.txt

6. cp -r#

//		cp /etc/hosts songshan/	Copy files without -r
		cp hengshan/ songsahn/ -r  Copy folders with -r

7. file#

//		file taishan  View file type

8. File Types#

//		Classic file types: directory		directory
					Empty file		empty
					Text file		text

image-20220310192555585

//		du -sh taishan/  View file size
//		rm -rf tianshan/	  Delete

9. cp#

//		cp When copying:
		Copy, paste, and rename in one step (renaming must not exist afterwards)
		cp a.txt abc.txt (There are 2 files a.txt and abc.txt in the directory)
		cp a.txt songshan/feng.txt (Copy to songshan and rename to feng.txt)
		cp hengshan/ songshan/nanyue_hs -r (Copy folders with -r)
			Copy hengshan to songshan and rename to nanyue_hs

10. mv#

//		mv Cut, paste, and rename in one step (renaming must not exist afterwards)
		mv a.txt hengshan/aaa.txt (Cut and paste, rename to aaa.txt)
		mv songshan/ hengshan/shaolinshi (Move, paste, and rename to shaolinshi)
			Moving and renaming does not require the -r option (this is different from cp)

11. Primary and Secondary Prompts#

//		Primary prompt
		[Logged user@hostname Working directory]
		Secondary prompt
		#  $
 		[root@localhost xuxia]#
		[root@localhost wuxia]# 
		[xulilin@localhost ~]$ 
//		Linux is a multi-user, multi-tasking operating system
		Root user's home directory  /root
		Regular user's home directory	/home with the same name as the username

12. echo#

//		echo outputs, equivalent to print
		PS1 is a built-in variable in the Linux system, no need for us to define it

13. First Prompt#

		First prompt variable PS1   prompt symbol variable

[\u@\h \W]$ Explanation#

//		# echo $PS1		# Note: First prompt
		[\u@\h \W]\$
		Explanation:
		\u  refers to the current user  user
   		\u  the username of the current user
		\h  refers to the current hostname   hostname
		\W refers to the name of the current working directory  working
		\$  outputs # when the user is root, and $ when not root
		\t  Current time

Temporarily Modify PS1 Variable#

//		# PS1='[\h@\u \t@\W]\$'  Temporarily modify PS1 variable (no need to change, just know)	\t Current time
[localhost@root 11:01:07@~]#
[localhost@xulilin 11:03:08@boot]$PS1='[\h#\u \t@\W]\$'
[localhost#xulilin 11:03:32@boot]$  // # can also be used

First and Second Prompts#

//		# echo  $PS1
		[\u@\h \W]\$
		# echo  $PS2   Second prompt
		>
//		If a line is not finished, the second prompt > will be displayed (just a reminder)

14. $+Variable Name#

		$PS1  $+Variable Name  Reference a variable
//		# echo  $PS1
		[\u@\h \W]\$
//		sg="tangliangfei"  Define a variable sg and assign the value tangliangfei on the right to the left
		echo $sg  Outputs the value of variable sg
		In shell programming, referencing a variable requires $+Variable Name
		Shell programming is command programming in Linux
//		mv="liangly"
		echo $sg  $mv
		tangliangfei liangly  (both displayed)

15. id#

//		id command
		[root@localhost china_voice]# id  
		uid=0(root) gid=0(root) group=0(root) environment=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
		Root user's uid is 0
		User id is the user's number uid
		Group id is the group's number gid

16. Linux Commands#

//		Linux commands
			Instructions or programs used to implement a certain type of functionality 
			Command execution depends on the interpreter program (e.g., /bin/bash)
//		Human <——> bash <——> Kernel <——> Hardware
		bash is the interpreter, equivalent to translating programs written in C language
		Classification of Linux commands
			Internal commands: Part of the Shell interpreter
			External commands: Program files independent of the Shell interpreter
//		shell: A type of program used to interpret user input commands and then tell the kernel to execute those commands
			bsh
			sh
			csh
			bash
			ksh
		bash is the most popular shell—almost all Linux systems default to bash

image-20220310194610950

Human——BASH——Kernel

image-20220310194628296

(General understanding)

17. cat /etc/shells#

//		cat /etc/shells  View which shells are available on this machine
		/bin/sh   Older shell
		/bin/bash  
		/usr/bin/sh
		/usr/bin/bash
//		sh  Type sh, use sh, no prompt, exit by typing bash or exit (parent-child process)
//		Switching shells  sh and bash commands  exit

18. ln -s#

#Note: Link files light blue
//		ln -s
		ln creates a link file (shortcut)
		Do not delete the original file, otherwise the link file will become unusable (flashing red)
		ln -s source_file link_file		ln -s xulilin xll
		-s symbol link (soft link)
		# mkdir changsha
		# ln -s changsha cs  
		# ll
		lrwxrwxrwx. 1 root root  8 Sep  17 11:19 cs -> changsha 

19. echo $SHELL#

//		# echo $SHELL View the default shell in the Linux system
		/bin/bash

20. env#

//		# env  View the current user's environment variables (predefined variables in the Linux system)  environment
		Variables that many processes of the current user can use
		Environment variables—> global variables

21. set#

		# set  View all variables (custom variables and environment variables)

22. Internal and External Commands#

//		bash - GNU Bourne-Again SHell
		BASH_BUILTINS  builtin internal  built in
		Additional installed commands—> external commands
		Commands obtained by installing the bash program—> internal commands of bash
		yum install tree -y	tree command is an external command, requires additional installation
		mkdir ip external command  cd echo exit internal command

23. which#

//		which bash  			View where the bash command is stored
		/usr/bin/bash

24. rpm#

rpm -qf#

#Note: rpm -qf	Query which software package the command was installed from
#Note: First use which to view the absolute path of the command
//		rpm -qf /usr/bin/bash  Query which software package installed /usr/bin/bash
		bash-4.4.19-10.el8.x86_64

rpm#

//		rpm is the command for software management in Linux, such as installing and uninstalling software
		-qf 	Query which software installed the file
//		# which ip
		/usr/sbin/ip
		# rpm -qf /usr/sbin/ip
		iproute-5.3.0-1.el8.x86_64
//		bin  binary  

25. /usr#

#Note: /usr	Directory for storing installed software	unix system resource
//		/usr   is the directory for storing installed software in Linux   unix system resource
		Equivalent to Program Files in Windows
//		Link files light blue
		Normal files dark blue
//		/  Root directory
		/root   	 Home directory of the root user
		/home/ Directory with the same name as the username   Home directory of regular users

26. Relationship between / and /root#

//		What is the relationship between the root directory and the root user's home directory?
		/root  is under the / directory, it is a containment relationship
//		~  represents the home directory, when the path of the folder you are in is the home directory, it will display ~
		Regular users can only enter their own home directory, while root users can enter any home directory↓↓↓

Entering Other Users' Home Directories#

[xulilin@localhost /]$ cd ~xulilin	# Note: Regular users can only enter their own home directory
[xulilin@localhost ~]$ cd ~cali
-bash: cd: /home/cali: Permission denied
[xulilin@localhost ~]$ exit
Logout
[root@localhost ~]# cd  ~cali
[root@localhost cali]# pwd
/home/cali
[root@localhost cali]# 

27. hostname#

//		# hostname View the hostname
		localhost.localdomain

Temporarily and Permanently Modify Hostname#

Temporary Modification#

//		# hostname sanchuang-linux  Temporarily modify the hostname (cannot use underscores _)
		Then log in again su - root	(current terminal login)/ or establish a new connection (open a terminal login) It will be invalid after reboot
//		Why modify the hostname?
			For easy identification, to distinguish different servers

Permanent Modification#

//*		Permanently modify the hostname
		# vim /etc/hostname  Change this file, give it a new name.
		# cat /etc/hostname 
		sanchuang-linux
		Effective after reboot
//		reboot  Restart
//		Not necessarily have to log in to the virtual machine for xshell to log in
//		Prompt
			PS1	First prompt
			PS2	Second prompt

28. vim#

//		Using the vim editor
		vim is a character interface text editor in Linux —> Notepad  can only write text
		Install vim		yum install vim -y
//		vim sc.txt
		1. Press the letter key i to enter insert mode
		2. Enter content, which can be in Chinese or English
		3. Press ESC key to return to command mode
		4. Enter :wq to exit and save
			  :q!  Exit without saving
			  :q  Exit (can exit if no modifications)

29. cat#

//		# cat sc.txt   View the content of the text file

30. Shortcuts#

//		Shortcuts
		//	ctrl + l
		//	ctrl + c	Forcefully terminate command  # ^ C
				For example, ping www.baidu.com ctrl + c
		//	tab	Complete command  # mkd (then press the tab key twice) all commands starting with mkd are listed
				Complete path  # mkdir wang If unique, it completes directly, otherwise press twice to list wang…
				Press once when uniqueness can be recognized
				Press twice when uniqueness cannot be recognized, then input yourself
		// Up and down arrow keys can recall previously used commands

31. pwd#

//		pwd  View working directory
		cd    Change working directory
		cd ~ (cd)  Enter user home directory
		cd -	Return to the last directory
		cd ~liangluyao  Enter liangluyao's home directory
		cd ..  Return to the parent directory

32. Alias#

//		ll is an alias for ls -l --color=auto
//		# alias  View which predefined aliases are in Linux
//		# alias c=clear  Temporarily define c as an alias for clear
		clear is a command in the Linux system  Commands with options must be enclosed in '' (single quotes) 
		When a new terminal is opened, it will no longer be effective
		c is a custom name, when defining an alias, do not conflict with existing system commands

Cancel Alias#

//		unalias	Cancel alias
		# alias mkdir=ls
		# unalias mkdir  Cancel alias

Permanently Modify Alias#

//		# vim /root/.bashrc  Permanently modify alias, requires re-login, or su to switch user to root
		.bashrc is a hidden file  Press i to enter for alias definition——>ESC  :wq

33. ls#

//		ls command
		Purpose: List (List) display directory contents 
		Format: ls  [options]...  [directory or file name]
        Common command options
        -l : Display in long format
        -d: Display the attributes of the directory itself
        -t: Sort by file modification time
        -r : Display the directory contents in reverse alphabetical order
        -a: Display information about all subdirectories and files, including hidden files
        -A: Similar to “-a”, but does not display information about “.” and “..” directories
        -h: Display information in more readable byte units (K, M, etc.)
        	//  Must be used with -l
        	-h, --human-readable
    			with -l and -s, print sizes like 1K 234M 2G etc.
        -R: Recursively display contents
        	Display layer by layer, far less effective than tree, very cumbersome
//		ls	View current directory
        ls /boot  View the directory under /boot
        ls -l -d /boot  Only display the folder's own information ls -ld / ls -dl
        ls -t  Time  Sort by file modification time
        ls -r  Reverse alphabetical order
        ls -l -h /boot  Display the size of each file in /boot
        		-h must be used with -l to display size K M

\ls -l#

//		\ls -l  Does not display the listed file type colors

ls -l |grep "^d"#

//		# ls -l|grep "^d"  
		Explanation as follows
//		| Pipe symbol: A way for processes to communicate with each other
		Function: Sends the output of the previous command to the next command as input
			A middleman, borrowing flowers to present to Buddha
//		grep: Text filtering command
			Filters based on matching strings, displaying lines with matching content

//		^d  Indicates lines starting with d
//		# ll|grep "txt"
        -rw-r--r--. 1 root root  0 Sep  17 15:58 a.txt
        -rw-r--r--. 1 root root  0 Sep  17 16:02 sc10.txt
        # ll|grep "^d"
        drwxr-xr-x. 5 root root 48 Sep  17 09:51 hunantv
        drwxr-xr-x. 4 root root 42 Sep  17 09:54 jiangsutv
        # ll|grep "^-"
        -rw-r--r--. 1 root root  0 Sep  17 15:58 a.txt
        -rw-r--r--. 1 root root  0 Sep  17 16:02 sc10.txt
        # ll|grep "^-"|wc -l
        11

34. ASCII#

//		ASCII American Standard Code for Information Interchange
		Numbers, uppercase letters, lowercase letters ASCII code values from small to large
//		One byte = 8 bits
		101010101
//		man ls  ——》/-h  Search for -h
//		# man ls
            Press Enter: Scroll down one line at a time
            /-h  Search for lines containing -h
            n   Search for next
            N   Search for previous
            q   Exit	

35. File Types#

//		File Type		Abbreviation		Application
        Regular file	    -		 Save data
        Directory 	      d		  Store files  directory
        Symbolic link file   l		Point to other files  link
        Character device file   c		character  --》Files related to character display
        Block device file		b		 block --> Block files that store data
        s 	socket file
        p 	pipe file

36. Explanation of 「drwxr-xr-x. 5 root root 48 Sep 17 09:51 hunantv」#

//		[root@sanchuang-linux china_voice]# ls -l
		Total usage 0
		drwxr-xr-x. 5 root root 48 Sep  17 09:51 hunantv
            d represents file type  directory
            rwxr-xr-x Permissions  r read   w write   x execute
            . Related to SELinux permissions
            5 Number of links to the file
            root  User 
            root  Group
            48  File size
            Sep  17 09:51  File creation time

37. wc#

//		wc is a counting command that can count how many lines, how many words, how many bytes are in a file
			-l Count lines  lines
		wc -l  Count lines

Do Not Display Filename#

//# wc -l a.txt  (wc -l can also count the number of lines in a file) Direct counting will display the filename
	3 a.txt
//# cat a.txt |wc -l (cat pipes to wc will not display the filename)
	3
		//# cat /etc/passwd|wc -l
			27
//# cat /etc/passwd|grep "liang"
    liangluyao:x:1001:1001::/home/liangluyao:/bin/bash
    liangluyao2:x:1002:1002::/home/liangluyao2:/bin/bash
    # cat /etc/passwd|grep "root"
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin

Practice#

//		1. Count how many character device files are in the /dev directory
		 ll /dev|grep "^c"|wc -l
		2. Count how many block device files are in the /dev directory
		 ll /dev|grep "^b"|wc -l
        3. Count how many folders and regular files are in the /etc/ directory
         ll /dev|grep "^- "|wc -l  Files
         ll /dev|grep "^d"|wc -l	Directories
         ll -R /etc|grep "^- "|wc -l Count all regular files in /etc and subfolders more thoroughly (including subdirectories)
         ll -R /etc|grep "^d"|wc -l Count all folders in /etc and subfolders more thoroughly (including subdirectories)
        4. Count how many lines are in the /etc/ssh/sshd_config file
		     cat /etc/ssh/sshd_config |wc -l
//		Count how many files are in the folder
    	ll | grep wc 
//		Count how many lines are in the file
	    cat | wc

38. du#

//		du  Count the space occupied by directories and files  disk usage (estimated value, not very accurate)
        *du -sh  Count the summarized size (commonly used) golden combination
            -s
              -s, --summarize Summarized size
                      display only a total for each argument
            -h  
             -h, --human-readable
                      print sizes in human readable format (e.g., 1K 234M 2G)
            -a Count files and folders
                 -a, --all
              write counts for all files, not just directories

Difference between du -sh and ll -h#

        du -sh /boot
        ll -h /boot
//		# ll -h /boot  Can only count the size of files, will not summarize all files in the folder
        Total usage 134M
        du -sh can summarize
        ll -h just tells you how many words and how many characters are in the file
        # du -sh /boot
        140M	/boot
        # ll -h -d /boot
        dr-xr-xr-x. 6 root root 4.0K Sep  13 14:38 /boot
//		English letters and numbers occupy 1 byte
        Chinese letters: utf8 encoding 3 bytes
        ll -h counts how many characters are in a file
			 Counts how many bytes of text are in the file
		du counts how many blocks are occupied
			Counts how many blocks of disk space the file occupies
//		1 block size is 4096 (4K)

image-20220311071612898

//		touch	Create an empty file
				Update time
//		stat hosts  View three times
        Last accessed
        Last modified
        Last changed

39. Change to English Encoding#

//		Change to English encoding
        # LANG=en_US.UTF8
        # ll hosts 
        -rw-r--r--. 1 root root 158 Sep 17 16:57 hosts

40. 3 Times#

# stat hosts 
  File: hosts
  Size: 158       	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 17162314    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:default_t:s0
Access: 2020-09-17 16:57:57.748799777 +0800
Modify: 2020-09-17 16:57:57.748799777 +0800
Change: 2020-09-17 16:57:57.748799777 +0800
 Birth: -
	Time:	
		Access time  access time  --》atime     Open this file  cat
		Modify time  modify time  --》mtime    Modify the contents, add, delete content, etc.  vim
		Change time  change time  --》ctime    Change the file's attributes (name, permissions, size, time, user, group)
		Accurate to nanoseconds
		touch hosts	Updates all three times (changes all)

41. chmod#

//		chmod Modify permissions  change mode 
			chmod a+w hosts

image-20220311072045096

		chown Change owner and group  change owner

image-20220311072106068

		vim hosts  Input  :wq  Modify data
//		+0800 indicates East Eight District  Shanghai, Beijing in China 
		Urumqi  East Six District
//		# cp /etc/passwd .  Copy /etc/passwd to the current directory
		Now the cp we type is all cp -i, cp is an alias
        # alias
        alias c='clear'
        alias cp='cp -i'
 		 -i, --interactive Interactive
              prompt before overwrite (overrides a previous -n option)

42. Method to Overwrite Files without Prompting during cp#

//		Method to overwrite files during cp without prompting
		1. Cancel alias  --》not recommended
			# unalias cp
		2. Use the absolute path of the cp command  --》recommended
		  # which cp
			alias cp='cp -i'
			  /usr/bin/cp
			# /usr/bin/cp feng changsha/ -r
			# /usr/bin/cp feng changsha/ -r  Copy again without prompting
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.