Linux learning record -- Basic bash shell commands

man bash

bash manual query:
Use the man Command + the name of the tool to find,
For example: man xterm

When using the man command to view the command man pages, these man pages are displayed by the pager.
Click q to exit the manual page.

Preferred section name for Linux man pages

NameDisplays the command name and a short description
SynopsisCommand syntax
ConfigurationCommand configuration information
DescriptionGeneral description of the command
OptionsDescription of command options
Exit StatusExit status indication of command
Return ValueReturn value of command
ErrorsError message for command
EnvironmentDescribes the environment variables used
FilesFiles used by the command
VersionsVersion information of the command
Conforming ToStandards followed by the command
NotesOther helpful information
BugsProvide a way to submit a bug
ExampleShow command usage
AuthorsCommand Developer Information
CopyrightCopyright status of command source code
See AlsoOther commands similar to this command

Search man pages using keywords:

Syntax: man -k keyword

Content area of the Linux man page

Area numberContent covered
1Executable program or shell command
2system call
3Library call
4Special documents
5Document format and Convention
7Overview, agreements and miscellaneous
8Superuser and system administrator commands
9kernel subroutines

Browse file system

Linux Filesystem

Linux Files are stored in a single directory structure, which is called a virtual directory( virtual directory). 
Linux The virtual directory structure contains only one called( root)The base directory of the directory.
The directories and files under the root directory will be listed one by one according to the directory path to access them.

Linux Special directories called mount points are created on the root drive( mount point). 
A mount point is a directory in a virtual directory that is used to allocate additional storage devices.
Virtual directories make files and directories appear in these point directories, but they are actually stored on another drive.

Common Linux directory names

/The root directory of the virtual directory. Files are not usually stored here
/binBinary directory, which stores many user level GNU tools
/bootThe startup directory stores startup files
/devDevice directory, where Linux creates device nodes
/etcSystem profile directory
/homeHome directory, where Linux creates user directories
/libLibrary Directory, which stores the library files of the system and applications
/mediaMedia directory, a common mount point for removable media devices
/mntMount directory, another common mount point for removable media devices
/optOptional directory, commonly used to store third-party software packages and data files
/procProcess directory, which stores the relevant information of existing hardware and current process
/rootHome directory of root user
/sbinThe system binary directory stores many GNU administrator level tools
/runThe run-time directory stores the run-time data during system operation
/srvService directory, which stores relevant files of local services
/sysSystem directory, which stores relevant files of system hardware information
/tmpTemporary directory where you can create and delete temporary working files
/usrUser binary directory, where a large number of user level GNU tools and data files are stored
/varVariable directory is used to store frequently changing files, such as log files

Traverse directory

cd  Switch directory
	cd destination

destination Parameter expression:
	Use absolute file path
	Use relative file path

Absolute file path

The absolute file path defines the exact location of the directory in the virtual directory structure, starting with the root directory of the virtual directory, which is equivalent to the full name of the directory.

Absolute file paths are always preceded by forward slashes(/)As a starting point, indicate the root directory of the virtual file system.
Example: /usr/bin

Use the absolute file path to switch to a specific location in the file system cd Specify the full pathname after the command.
Example: cd /usr/bin


pwd The command can display shell The current directory of the session, which is called the current working directory.

Relative file path

Relative file path allows the user to specify a directory file path based on the current location.
Relative file paths do not have forward slashes representing the root directory(/)It starts with the directory name (if the user is going to switch to a directory under the current working directory) or a special character.
Example: cd Relative file path

Special characters available for relative file paths:
		- Single dot character( . ),Represents the current directory
		- Double dot character( .. ),Indicates the parent directory of the current directory (i.e. back to the previous directory)

List of files and directories


The most basic form of the command will display the files and directories in the current directory.

Basic list function

- Available band -F Parametric ls Command makes it easy to distinguish between files and directories. 
	-F Parameter has a forward slash after the directory name(/),To facilitate users to distinguish in the output.
	The executable is followed by an asterisk( * ). 
- ls -a Command displays hidden files along with normal files and directories.
	Hide files to . Start with a sign.
- -R The parameter is called the recursion option. It lists the files in the subdirectories contained in the current directory.

Show long list

- ls -l Displays additional information.
	-l Parameter produces output in a growth list format that contains more information about each file in the directory.
- The first line of the output shows the total number of blocks contained in the directory. Each subsequent line contains the following information about the file (or directory):
	File types such as directories( d),Documents( -),Character file( c)Or block device( b);
	File permissions
	Total number of hard links to documents;
	User name of the file owner;
	Group name of the file group;
	The size of the file (in bytes);
	Last modification time of the document;
	File name or directory name
	ls -alF

Filter output list

ls The command will output all non hidden files in the directory.
When the user specifies the name of a specific file as a filter, ls The command displays only information about the file.

Question mark( ?)Represents a character;
Asterisk( *)Represents zero or more characters.
	Example: ls -l my_scr?pt
		ls -l my*         (* ?(can be placed anywhere in the filter)
The question mark can be used to replace a single character anywhere in the filter string.

Using asterisks and question marks in the filter is called file extension matching( file globbing),Refers to the process of pattern matching using wildcards.

The formal name of a wildcard is called a metacharacter wildcard( metacharacter wildcards). 

In addition to asterisks and question marks, there are more metacharacter wildcards that can be used for file extension matching. Brackets can be used.

We use brackets and two characters that may appear in a particular position: a or i. Brackets indicate a character position and give multiple possible choices.

You can list the characters to be selected, or you can specify a character range, such as a letter range[a – i]. 
Alternatively, you can use exclamation marks( !)Exclude unwanted content

ls- l The access time will not be displayed because the modification time is displayed by default. To view the access time of the file, add:–time=atime. 

process the file

create a file


An empty file was created.
Can be used to change the modification time of the file.
touch  -a   Change access time

Copy file


Copy file cp The command requires two parameters -- a source object and a target object.
cp source destination
When source and destination When all parameters are file names, cp The command copies the source file into a new file,
And with destination Naming. A new file is like a new file with a new modification time.

new file test_two And documents test_one The modification time is different. If the target file already exists, cp The command may not remind you of this.
It's better to add**-i**Options, forcing shell Ask if you want to overwrite an existing file.

cp Imperative-R Parameters are powerful. You can use it to recursively copy the contents of the entire directory in one command.

Tab key automatic completion

Tab key automatic completion allows you to press the tab key when entering the file name or directory name, and let the shell help complete the content.

The trick of using tab key auto completion is to give the shell enough file name information to distinguish the required file from other files.

Link file

yes Linux One advantage of the file system. If it is necessary to maintain two or more copies of the same document on the system,
In addition to saving multiple separate copies of physical files, you can also save one copy of physical files and multiple virtual copies.
This virtual copy is called a link. A link is a placeholder in a directory that points to the real location of a file.

A symbolic link is a real file that points to another file stored somewhere in the virtual directory structure.

The contents of these two files linked by symbols are different from each other.

have access to ln -s File symbolic links create symbolic links.

The name of the symbolic link sl_data_file be located ln At the second parameter position in the command. Appears after the symbol file name in the long list->The symbol indicates that the file is linked to a file data_file A symbolic link on.

Symbolic link sl_data_file Only 9 bytes, and data_file There are 1092 bytes. that is because sl_data_file Just pointing data_file nothing more. Their contents are not the same. They are two completely different files. (similar to hyperlink)

ls -i You can view the of a file or directory inode The number, which is the unique number used for identification, is assigned by the kernel to objects in the file system.

A hard link creates a separate virtual file that contains the information and location of the original file. But they are fundamentally
 Is the same file. Referencing a hard linked file is equivalent to referencing a source file.

rename file


stay Linux In, renaming a file is called moving. mv The command can move files and directories to another location or rename them

Note that moving a file changes the file name from fall Change to fzll,but inode The number and timestamp remain unchanged. that is because mv Only file names are affected.

You can also use mv To move the location of the file. Does not change the of the file inode Number or timestamp.

Delete file


stay Linux In, delete( deleting)It's called removal( removing) . bash shell The command to delete files in is rm. 

in use rm When ordering, cultivate the habit of always joining-i Good habit of parameters.

You can also use wildcards to delete groups of files. Don't forget to use wildcards**-i**Option to protect your files.

Processing directory

Create directory


d The beginning indicates the directory.

mkdir -p To create multiple directories and subdirectories at the same time, you need to add**-p**parameter

Delete directory


By default, rmdir The command only deletes empty directories because we are New_Dir A file was created in the directory my_file,therefore rmdir The command refused to delete the directory. To solve the problem, you must delete the files in the directory before using it on an empty directory rmdir Command.

It can also be used on the entire non empty directory rm Command. Use**-r Option allows the command to go down to the directory, delete the files in it, and then delete the directory itself.**

This method can also go down to multiple subdirectories, which is effective when deleting a large number of directories and files.

To delete a directory and all its contents, use the-r Parameters and-f Parametric rm Command.

rm -rf The command has neither a warning message nor an audible prompt. This must be a dangerous tool, especially when you have super user rights. Be careful and check again and again that what you want to do is as expected.

view file contents

View file types

file You can probe inside the file and view the file type

The file in the above example is a text((text) file. file The command can not only determine the text information contained in the file,
It can also determine the character encoding of the text file, ASCII. 

Shows a file of type symbolic link. Note, file The command can even tell you which file it links to.
The last example is a binary executable. file Command can determine the platform for which the program is compiled and the requirements
 What type of library do you want? This can be a useful feature if you have binaries from unknown sources:

View entire file command
cat test_one
-n All lines are numbered.
cat -n test1
 If you only want to add line numbers to lines with text, you can use**-b**Parameters.
-T Parameters can be used^I Character combination to replace all tabs in the text.

2.more command
more The command displays the contents of the text file, but stops after each page of data is displayed.
more The command is a paging tool.
more The command only supports basic moves in text files.

3.less command
 than more More advanced.
It provides some very practical features, which can flip back and forth in text files, and some advanced search functions.

View some files

1. tail Command:
	Displays the contents of the last few lines of the file. The default is the last 10 lines of the file.

	tail log_file: 
	Can to tail Add to command**-n Parameter to modify the number of rows displayed**. 
	-f Parameter is tail A prominent feature of the command. It allows you to view the contents of the file when it is used by other processes.

	tail The command remains active and continuously displays what is added to the file. This is a great way to monitor the system log in real time.

2.head Command:
	Displays the contents of those lines at the beginning of the file. The first 10 lines of text of the file are displayed by default.


ls Command lists files and subdirectories in a directory.
touch Create an empty file and change the access time of an existing file.
cp Copy files elsewhere.
Link file.
mv Command to move files.
rm Command to remove files.
also mkdir and rmdir Command performs tasks on the directory.
cat,more and less Command to view the entire contents of the file.
tail and head Command to view the beginning and end of a file.

Learning References:

	Linux Command line and shell Complete script programming (3rd Edition)

Tags: Linux bash

Posted on Tue, 19 Oct 2021 03:18:55 -0400 by Kor Ikron