1. man command
statement: $ man command keyword
Function: View the comment description of the command
ps: q quit
2. cd command
$ cd destination
Function: switch directory
cd single point (.), indicates the current directory;
cd double dot (…), indicates the parent directory of the current directory.
3. ls command
function: display list
$ls [parameter] file path
Among them, the parameter description: -F: distinguish between files and folders
-a show hidden documents
-R for recursive option. It lists files in subdirectories contained under the current directory
ps:: ls –F –R can be merged: ls –FR
-l: Produces output in long listing format, containing more information about each file in the directory
For example: drwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Desktop
View file permissions
ls -l filename
ls -l /etc/sudoers
Information explanation:
File type, such as directory (d), file (-), character file (c) or block device (b);
File permissions;
The total number of hard links to the file;
Username of the file owner;
The group name of the group to which the file belongs;
The size of the file in bytes;
When the file was last modified;
File or directory name.
-h : Equivalent to -l, but the size of the file is displayed for easy viewing.
-d just show the folder name
$ ls -d *
Example: $ ls -Fd *Scripts
Scripts/
Where Scripts is a folder
ls l parameter: used to find files
Parameters: 1. Specific name
2. Wildcards: *: Any number of characters?: A single character
Brackets []: Specify content, such as [a,i] contains a or i, letter range [ai]
! to exclude unwanted content
$ ls -l my_scr[ai]pt
-rw-rw-r-- 1 christine christine 0 May 21 13:25 my_scrapt
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$ ls -l f[a-i]ll
-rw-rw-r-- 1 christine christine 0 May 21 13:44 fall
-rw-rw-r-- 1 christine christine 0 May 21 13:44 fell
-rw-rw-r-- 1 christine christine 0 May 21 13:44 fill
$ ls -l f[!a]ll
-rw-rw-r-- 1 christine christine 0 May 21 13:44 fell
-rw-rw-r-- 1 christine christine 0 May 21 13:44 fill
-rw-rw-r-- 1 christine christine 0 May 21 13:44 full
4. Commands related to file processing
$ touch folder name: create an empty folder
$cp copy file
cp source destination
cp source file target object
Where parameters:
-i : If the target file already exists, the cp command may not remind you of this. It is best to add the -i option to force the shell to ask whether it needs to overwrite existing files.
For example: $ cp -i test_one test_two
cp: overwrite ‘test_two’? n
n: no copy y: copy
$ cp -i test_one /home/christine/Documents/
$ ls -l /home/christine/Documents
total 0
-rw-rw-r-- 1 christine christine 0 May 21 15:25 test_one
The new file is now in the directory Documents, with the same name as the source file.
Explain that the previous example added a forward slash (/) to the end of the target directory name, which indicates that Documents is a directory rather than a file.
This helps with clarity and is very important when copying individual files. If you don't use the forward slashes, and the subdirectory /home/christine/Documents doesn't exist, you'll have trouble. In this case, attempting to copy a file to the Documents subdirectory will instead create a file called Documents, without even displaying an error message!
. The single dot represents the current working directory. If you need to copy a file with a long source object name into the current working directory, dots can simplify the task.
$ cp -i /etc/NetworkManager/NetworkManager.conf .
$ ls -l NetworkManager.conf
-rw-r–r-- 1 christine christine 76 May 21 15:55 NetworkManager.conf
-R : copy the contents of the entire directory recursively
rename file
mv command
Function: 1. Rename the file, just change the file name, not change the location
mv original file name new file name
$ mv fall fzll
2. Move the location of the file
$ ls -li /home/christine/fzll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44
/home/christine/fzll
$ ls -li /home/christine/Pictures/
total 0
$ mv fzll Pictures/
$ ls -li /home/christine/Pictures/
total 0
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fzll
$ ls -li /home/christine/fzll
ls: cannot access /home/christine/fzll: No such file or directory
The mv command moved the file fzll from /home/christine to /home/christine/Pirtures
3. Use the mv command to move the file location and modify the file name
$ ls -li Pictures/fzll
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44
Pictures/fzll
$ mv /home/christine/Pictures/fzll /home/christine/fall
$ ls -li /home/christine/fall
296730 -rw-rw-r-- 1 christine christine 0 May 21 13:44
/home/christine/fall
$ ls -li /home/christine/Pictures/fzll
ls: cannot access /home/christine/Pictures/fzll:
No such file or directory
The file fzll was moved from the subdirectory Pictures to the main directory /home/christine, and the name
Change to fall.
delete file
rm command
$ rm -i fall
rm: remove regular empty file ‘fall’? y
$ ls -l fall
ls: cannot access fall: No such file or directory
Note that the -i command parameter prompts you whether you really want to delete the file. No recycle bin or trash in bash shell, file
Once deleted, it cannot be retrieved. Therefore, when using the rm command, it is a good habit to always include the -i parameter.
5. Processing directory
mkdir command: creating a directory is very simple
$ mkdir New_Dir
$ ls -ld New_Dir
drwxrwxr-x 2 christine christine 4096 May 22 09:48 New_Dir
The system creates a new directory called New_Dir. Note that the long list of new directories starts with d. This means that New_Dir is not a file, but a directory.
To create multiple directories and subdirectories at the same time, you need to add the -p parameter:
$ mkdir -p New_Dir/Sub_Dir/Under_Dir
$ ls -R New_Dir
New_Dir:
Sub_Dir
New_Dir/Sub_Dir:
Under_Dir
New_Dir/Sub_Dir/Under_Dir:
The -p parameter of the mkdir command can create missing parent directories as needed. A parent directory is a directory that contains the next level of directory in the directory tree.
-R: Recurse, display subfiles of a folder
delete folder/directory
The basic command to delete a directory is rmdir.
$ touch New_Dir/my_file
$ ls -li New_Dir/
total 0
294561 -rw-rw-r-- 1 christine christine 0 May 22 09:52 my_file
$
$ rmdir New_Dir
rmdir: failed to remove ‘New_Dir’: Directory not empty
$
By default, the rmdir command only deletes empty directories. Because we created a file my_file under the New_Dir directory, the rmdir command refuses to delete the directory.
To solve this problem, the files in the directory must be deleted before using the rmdir command on an empty directory.
$ rm -i New_Dir/my_file
rm: remove regular empty file ‘New_Dir/my_file’? y
$ rmdir New_Dir
$ ls -ld New_Dir
ls: cannot access New_Dir: No such file or directory
rmdir doesn't have a -i option to ask if you want to delete a directory. This is why it is still beneficial to say that rmdir can only delete empty directories.
It is also possible to use the rm command on entire non-empty directories. Using the -r option causes the command to descend into a directory, delete the files in it, and then delete the directory itself.
$ ls -l My_Dir
total 0
-rw-rw-r-- 1 christine christine 0 May 22 10:02 another_file
$ rm -ri My_Dir
rm: descend into directory ‘My_Dir’? y
rm: remove regular empty file ‘My_Dir/another_file’? y
rm: remove directory ‘My_Dir’? y
$ ls -l My_Dir
ls: cannot access My_Dir: No such file or directory
For the rm command, the -r parameter has the same effect as the -R parameter. The -R parameter can also recursively delete files in the directory. Shell commands rarely take arguments in different case for the same function.
The ultimate way to delete a directory and all its contents in one fell swoop is to use the rm command with the -r parameter and the -f parameter.
$ tree Small_Dir
Small_Dir
├─ a_file
├─ b_file
├─ c_file
├─ Teeny_Dir
│ └─ e_file
└─ Tiny_Dir
└─ d_file
2 directories, 5 files
$ rm -rf Small_Dir
$ tree Small_Dir
Small_Dir [error opening dir]
0 directories, 0 files
The rm -rf command has neither warning messages nor sound prompts. This is certainly a dangerous tool, especially with superuser privileges. Use with caution and double check that what you are doing is as expected.
su command: switch user
su - : switch to root user, root password is required
su username: switch to the specified user
whoami: view the current user
yum: View installed packages
yum list installed: View installed packages
yum install package_name : install the package
$ are -
Password:
yum install xterm
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
-
base: mirrors.bluehost.com
-
extras: mirror.5ninesolutions.com
-
updates: mirror.san.fastserv.com
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package xterm.i686 0:253-1.el6 will be installed
–> Finished Dependency Resolution
Dependencies Resolved
[…]
Installed:
xterm.i686 0:253-1.el6
Complete!
Note In the above example, we used the su-command before running the yum command. This command allows you to switch to root
user. On Linux systems, # indicates that you are logged in as the root user. Should only run administrative tasks
Only temporarily switch to the root user (such as installing and updating software). You can also use the sudo command.
view file type
$file my_file
my_file: ASCII text
The file in the example above is a text (text) file. The file command can not only determine the text information contained in the file, but also determine the character encoding of the text file, ASCII.
$ file New_Dir
New_Dir: directory
You can use the file command as another way to differentiate directories
symbolically linked files
$ file sl_data_file
sl_data_file: symbolic link to ‘data_file’
Note that the file command can even tell you which file it links to
The return result of the file command to the script file.
Although this file is ASCII text, because it is a script file, it can be executed (run) on the system:
$ file my_script
my_script: Bourne-Again shell script, ASCII text executable
View file content
1, cat command
-n: All output lines plus line label
-b: non-empty output plus line label
-T: Replace all tab characters in the text with the ^I character combination
$ cat -n test1
1 hello
2
3 This is a test file.
4
5
6 That we’ll use to test the cat command.
2、more
3、less
4、tail file_name
By default, the last 10 lines of the file are displayed. The number of lines can be specified by -n. $ tail -n 2 file_name shows the last 2 lines.
The -f parameter is a prominent feature of the tail command. It allows you to view the contents of a file while other processes are using it.
The tail command remains active and continuously displays what is added to the file. This is a great way to monitor system logs in real time
Way.
5. head displays the first few lines of the file, similar to tail, but does not support -f.
6、
bash starts a new shell
$ ps -f
UID PID PPID C STIME TTY TIME CMD
501 1841 1840 0 11:50 pts/0 00:00:00 -bash
501 2532 1841 1 14:22 pts/0 00:00:00 ps -f
$ bash
$ bash
$ bash
$ ps --forest
PID TTY TIME CMD
1841 pts/0 00:00:00 bash
2533 pts/0 00:00:00 _ bash
2546 pts/0 00:00:00 _ bash
2562 pts/0 00:00:00 _ bash
2576 points/0 00:00:00 _ ps
In the above example, the bash command was entered three times, which actually created three subshells.
The ps -forest command shows the nesting structure between these subshells
The Linux file system merges all disks into one virtual directory. Before using the new storage medium, it needs to be placed under the virtual directory. This work is called mounting.
df View disk usage
df -h
display in human readable form
The grep command looks for lines in the input or specified files that contain characters matching the specified pattern. The output of grep is
the lines that contain the matching pattern.
The following two simple examples demonstrate the use of the grep command to search the file file1 used in Section 4.3.1.
$ grep three file1
three
$ grep t file1
two
three
$
The first example searches the file file1 for text that matches the pattern three. The grep command outputs the lines matching the pattern
. The second example searches the file file1 for text that matches the pattern t. In this example, two lines in file1 match
the specified pattern, and both lines are output.
Due to the popularity of the grep command, it has undergone a lot of updates. A lot of functionality has been added to the grep command. If you take a look
at its man page, you can see how omnipotent it is. 82 Chapter 4 More bash shell commands
If you want to perform a reverse search (output lines that do not match the pattern), you can add the -v parameter.
$ grep -vt file1
one
four
five
$
If you want to display the line number of the line matching the pattern, you can add the -n parameter.
$ grep -nt file1
2:two
3:three
$
If you only need to know how many lines contain matching patterns, you can use the -c parameter.
$ grep -ct file1
2
$
If you want to specify multiple matching patterns, you can use the -e parameter to specify each pattern.
$ grep -et -ef file1
two
three
four
five
$
This example outputs all lines containing the character t or the character f.
By default, the grep command uses basic Unix-style regular expressions to match patterns. Unix-style regular expressions use
special characters to define how to find matching patterns.
For more details on regular expressions, refer to Chapter 20.
Following is a simple example of using regular expressions in grep search.
$ grep [tf] file1
two
three
four
five
$
The square brackets in the regular expression indicate that grep should search for a match containing the t or f characters. Without regular expressions,
grep searches for text that matches the string tf.
The egrep command is a derivative of grep that supports POSIX extended regular expressions. POSIX extended regular expressions contain more
characters that can be used to specify matching patterns (see Chapter 20). Fgrep is another version that supports specifying the matching pattern
as a list of fixed-length strings separated by newlines. In this way, this list of strings can be put into a file, and then
used in the fgrep command to search for strings in a large file
— Compressed file
gzip: used to compress files.
gzcat: Used to view the contents of compressed text files.
gunzip: used to decompress files.
These tools are basically the same as the bzip2 tool.
$ gzip myprog
$ ls -l my*
-rwxrwxr-x 1 rich rich 2197 2007-09-13 11:29 myprog.gz
$
The gzip command compresses the files you specify on the command line. You can also specify multiple file names or even use wildcards on the command line to
compress files in batches at one time.
$ gzip my*
$ ls -l my*
-rwxr–r-- 1 rich rich 103 Sep 6 13:43 myprog.c.gz
-rwxr-xr-x 1 rich rich 5178 Sep 6 13:43 myprog.gz
-rwxr –r-- 1 rich rich 59 Sep 6 13:46 myscript.gz
-rwxr–r-- 1 rich rich 60 Sep 6 13:44 myscript2.gz
----Archive data
Although the zip command can compress and archive data into a single file, it is not a standard archiving
tool in Unix and Linux. Currently, the most widely used archiving tool on Unix and Linux is the tar command.
The tar command was originally used to write files to tape devices for archiving, but it can also write output to files. This
usage has been commonly used to archive data on Linux.
The following is the format of the tar command:
tar function [options] object1 object2 ...
The function parameter defines what the tar command should do, as shown in Table 4-8.
Table 4-8 Functions of the tar command
Function Long Name Description
-A --concatenate Append an existing tar archive to another existing tar archive
-c --create Create a new tar archive
-d --diff Check archive and filesystem differences
–delete delete from existing tar archive
-r --append append files to end of existing tar archive
-t --list list contents of existing tar archive
-u --update append a file to the tar archive that is newer than a file of the same name already in the tar archive -x --extract extract files
from an existing tar archive
specific behavior. Table 4-9 lists
common options among these options that can be used with the tar command.
Table 4-9 tar command options
Option description
-C dir switch to the specified directory
-f file output the result to a file or device file
-j redirect the output to the bzip2 command to compress the content
-p preserve all file permissions
-v when processing files show file
-z redirect output to gzip command to compress content
These options are often used in combination. First, you can create an archive file with the following command:
tar -cvf test.tar test/ test2/
The above command creates an archive file named test.tar containing the contents of the test and test2 directories. Next, use the following command:
tar -tf test.tar
lists the contents of the tar file test.tar (but does not extract the file). Finally, use the command:
tar -xvf test.tar4.4 Summary 85
1
2
3
4
5
8
10
14
9
6
7
12
11
13
Use this command to extract the content from the tar file test.tar. If the tar file was created from a directory structure, the entire directory
structure will be recreated in the current directory.
As you can see, the tar command is an easy way to create an archive of an entire directory structure.
This is the common method used to distribute the source code files of open source programs in Linux .
Tip After downloading open source software, you will often see filenames ending in .tgz. These are gzipped tar files that can be
decompressed with the command tar -zxvf filename.tgz
The grep command looks for lines in the input or specified files that contain characters matching the specified pattern. The output of grep is
the lines that contain the matching pattern.
The following two simple examples demonstrate the use of the grep command to search the file file1 used in Section 4.3.1.
$ grep three file1
three
$ grep t file1
two
three
$
The first example searches the file file1 for text that matches the pattern three. The grep command outputs the lines matching the pattern
. The second example searches the file file1 for text that matches the pattern t. In this example, two lines in file1 match
the specified pattern, and both lines are output.
Due to the popularity of the grep command, it has undergone a lot of updates. A lot of functionality has been added to the grep command. If you take a look
at its man page, you can see how omnipotent it is. 82 Chapter 4 More bash shell commands
If you want to perform a reverse search (output lines that do not match the pattern), you can add the -v parameter.
$ grep -vt file1
one
four
five
$
If you want to display the line number of the line matching the pattern, you can add the -n parameter.
$ grep -nt file1
2:two
3:three
$
If you only need to know how many lines contain matching patterns, you can use the -c parameter.
$ grep -ct file1
2
$
If you want to specify multiple matching patterns, you can use the -e parameter to specify each pattern.
$ grep -et -ef file1
two
three
four
five
$
This example outputs all lines containing the character t or the character f.
By default, the grep command uses basic Unix-style regular expressions to match patterns. Unix-style regular expressions use
special characters to define how to find matching patterns.
For more details on regular expressions, refer to Chapter 20.
Following is a simple example of using regular expressions in grep search.
$ grep [tf] file1
two
three
four
five
$
The square brackets in the regular expression indicate that grep should search for a match containing the t or f characters. Without regular expressions,
grep searches for text that matches the string tf.
The egrep command is a derivative of grep that supports POSIX extended regular expressions. POSIX extended regular expressions contain more
characters that can be used to specify matching patterns (see Chapter 20). Fgrep is another version that supports specifying the matching pattern
as a list of fixed-length strings separated by newlines. In this way, this list of strings can be put into a file, and then
used in the fgrep command to search for strings in a large file