The main contents of this section
- How to Get Help Documents
- Introduction to Linux File System
- Directory operation
1. How to get help documents
In the actual working process, we often forget how to use commands, such as what parameters can be followed by ls command. At this time, we can use man command to see how to use commands, such as
//man command acquisition command help manual xtwy@ubuntu:~$ man ls
You can use the keyboard's
To display the next or previous line of commands, you can also use
View the previous or next page (screen) commands, and
The space bar can also be used to display commands on the next screen. To view the exit command, just press the q key to exit, or the h key to display the list of less commands (the man command outputs the results through the less command)
2. Introduction to Linux File System
(1) Documents and catalogues
This section introduces the Linux file system from the user's point of view. Linux divides files into directories and ordinary files according to the form of files, as follows:
The name of a directory or file shall not exceed 255 characters in length. The name of a file or directory may consist of the following characters:
- Uppercase letters (A–Z)
- Lowercase letters (a–z)
- Numbers (0–9)
- Underscore ( _ )
- Period(.)
- Comma(,)
File or directory names are case-sensitive and belong to different files or directories
(2) File extensions and invisible filenames
Unlike Window s, Linux files do not require file extensions. For example, if you write a source file in C language, you can name it complier.c, or other file names such as complier, complier.ccc. This is not recommended, because if you can write a file extension with Linux file extensions, which are commonly used at present, are shown in the following table:
File name with extension | The Meaning of Extensions |
---|---|
max.c | C language source file |
max.o | Coded target code file |
max | Executable file corresponding to max.c |
memo.txt | text file |
memo.pdf | pdf files, you must use xpdf or kpdf on the GUI interface to view |
memo.ps | PostScript files must be viewed using ghostscript or kpdf on the GUI interface |
memo.z | Compressed files can be decompressed by uncompress or gunzip |
memo.gz | The compressed files can be decompressed by gunzip |
memo.tar.gz or memo.tgz | The tar archive file compressed by gzip can be decompressed by gunzip |
memo.bz2 | The file compressed by bzip2 can be decompressed by bunzip2 |
memo.html | html file, view using firefox in GUI environment |
memo.jpg, etc. | Image file, open with photo viewer in GUI environment |
In the previous lecture, we saw that there are still a large number of hidden files in linux, which can be displayed by ls-a command. If you want to define hidden files, just start with the file name or directory.
(3) Absolute path and relative path
Absolute path and relative path are very important concepts in Linux. The following figure shows what absolute path is.
All starting with the root directory "/" are absolute paths, and the rest are relative paths.
//Absolute path access xtwy@ubuntu:~/Public$ cd /home/ xtwy@ubuntu:/home$ ls xtwy //Relative path access xtwy@ubuntu:/home$ cd xtwy/
3. Directory operation
(1) Create directory mkdir
For the convenience of demonstration, the following directory structure is used for demonstration:
1 Absolute Path Creation Method
//Create with absolute paths root@ubuntu:/home# mkdir /home/max root@ubuntu:/home# ls max xtwy root@ubuntu:/home#
2 Relative Path Creation
//Create using relative paths root@ubuntu:/home# mkdir max/names root@ubuntu:/home# mkdir max/temp root@ubuntu:/home# mkdir max/literature root@ubuntu:/home# cd max root@ubuntu:/home/max# mkdir demo root@ubuntu:/home/max# ls demo literature names temp
Sometimes you don't want to create hierarchical directories. At this point, you can add the parameter - p (parents) after mkdir to create parent-child directories together.
root@ubuntu:/home/max# mkdir -p literature/promo root@ubuntu:/home/max# ls demo literature names temp root@ubuntu:/home/max# cd literature/ root@ubuntu:/home/max/literature# ls promo
(2) Change directory cd
The difference between working directory and home directory
The default directory after each login is the home directory, which remains unchanged during the session with the system. The home directory is represented by ~
xtwy@ubuntu:/root$ cd ~ xtwy@ubuntu:~$ pwd /home/xtwy
The working directory is also called the current directory. The directory after the execution of the cd command is the working directory. It can be changed at will.
//Represents the current directory, that is, the working directory. //... Represents the upper directory of the current directory xtwy@ubuntu:~$ cd . xtwy@ubuntu:~$ cd .. xtwy@ubuntu:/home$
(3) Delete directory rmdir
rmdir, short for remove directory, is used to delete a directory. It deletes all files in the directory first, and then deletes the directory. However, when there are subdirectories in the directory, the command cannot be executed. It needs to use rm commands, such as
//Delete temp directory, first delete files under the directory //Remove temp directory itself root@ubuntu:/home/max# rmdir temp/ root@ubuntu:/home/max# rmdir literature/ rmdir: failed to remove `literature/': Directory not empty root@ubuntu:/home/max# rm -r literature/ root@ubuntu:/home/max# ls demo names
Among them, r in rm-r refers to the recursive deletion of directories and files in directories, so it has a strong destructive power, and should be used cautiously.
(4) Mobile directory mv
//Move the directory demo to / home/xtwy / directory root@ubuntu:/home/max# mv demo/ /home/xtwy/ root@ubuntu:/home/max# cd /home/xtwy/ root@ubuntu:/home/xtwy# ls demo Documents examples.desktop Pictures Templates Desktop Downloads Music Public Videos root@ubuntu:/home/xtwy# rmdir demo //The demo directory of the original directory no longer exists root@ubuntu:/home/xtwy# cd /home/max/ root@ubuntu:/home/max# ls names
(5) Copy directory cp
Moving directories with the mv command, sometimes you need to copy directories. The following is how to use them:
//First create a demo directory, using - p, the parent directory will be created if it does not exist root@ubuntu:/home/max# mkdir -p literature/demo //Because literature also includes subdirectories, the copy is unsuccessful at this time root@ubuntu:/home/max# cp literature/ /home/xtwy/ cp: omitting directory `literature/' //If subdirectories are included, the - r parameter is added to indicate recursive copy. root@ubuntu:/home/max# cp -r literature/ /home/xtwy/ root@ubuntu:/home/max# cd /homt bash: cd: /homt: No such file or directory root@ubuntu:/home/max# cd /home/xtwy/ root@ubuntu:/home/xtwy# ls Desktop Downloads literature Pictures Templates Documents examples.desktop Music Public Videos root@ubuntu:/home/xtwy# cd literature/ root@ubuntu:/home/xtwy/literature# ls demo
4. File operation
(1) Create files
There are many ways to create files directly from the command line. The common ways are as follows:
//Redirect the output command to the file through the echo command root@ubuntu:/home/xtwy# echo "hello linux" > hello.txt root@ubuntu:/home/xtwy# ls Desktop Downloads hello.txt Music Public Videos Documents examples.desktop literature Pictures Templates //touch command, how the file does not exist, will create the file root@ubuntu:/home/xtwy# touch hell1.txt root@ubuntu:/home/xtwy# ls Desktop Downloads hell1.txt literature Pictures Templates Documents examples.desktop hello.txt Music Public Videos
(2) Displaying the contents of documents
The cate command displays the contents of the file. Its full name is catenate, which means to connect words one by one.
root@ubuntu:/home/xtwy# cat hello.txt hello linux
The cat command displays everything in the file at once, such as
root@ubuntu:/home/xtwy# cat /etc/profile # /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i ......
Sometimes we want to be able to view the contents of a file on a split screen. At this time we can use less or more paging programs. Less and more are used in a similar way. The next screen information is displayed by a space key. The difference between them is that less displays END messages at the end of the file, while more returns directly to the shell terminal, for example:
less command
more command
(3) Copy files by cp command
root@ubuntu:/home/xtwy# ls Desktop Downloads hell1.txt literature Pictures Templates Documents examples.desktop hello.txt Music Public Videos //Copy files root@ubuntu:/home/xtwy# cp hell1.txt literature/demo root@ubuntu:/home/xtwy# cd literature/demo //cd - Return to the last working directory executed root@ubuntu:/home/xtwy/literature/demo# cd - /home/xtwy
It should be noted that when the cp command is copied, if the file already exists in the target directory, the system will not give a warning, but directly overwrite it. Therefore, it may have the risk of destroying the file. To solve this problem, we can use the - i parameter to let the system give a warning, for example:
root@ubuntu:/home/xtwy# cp -i hell1.txt literature/demo cp: overwrite `literature/demo/hell1.txt'?
(3) mv command to move or rename files
//In the same directory, it is equivalent to file renaming, and hell1.txt does not exist after execution. root@ubuntu:/home/xtwy# mv hell1.txt hell2.txt root@ubuntu:/home/xtwy# ls Desktop Downloads hell2.txt literature Pictures Templates Documents examples.desktop hello.txt Music Public Videos //Move hell2.txt to literature/demo root@ubuntu:/home/xtwy# mv hell2.txt literature/demo root@ubuntu:/home/xtwy# cd literature/demo/ root@ubuntu:/home/xtwy/literature/demo# ls hell1.txt hell2.txt root@ubuntu:/home/xtwy/literature/demo# cd - /home/xtwy //Source directory hell2.txt no longer exists root@ubuntu:/home/xtwy# ls Desktop Downloads hello.txt Music Public Videos Documents examples.desktop literature Pictures Templates
(4) Display the head or tail of a document
The header command is used to display the contents of the file header, tail command is used at the end, and the default number of lines is 10.
root@ubuntu:/home/xtwy# head ~/.bashrc # ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples # If not running interactively, don't do anything [ -z "$PS1" ] && return # don't put duplicate lines in the history. See bash(1) for more options # ... or force ignoredups and ignorespace HISTCONTROL=ignoredups:ignorespace root@ubuntu:/home/xtwy# tail ~/.bashrc if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi # enable programmable completion features (you don't need to enable # this, if it's already enabled in /etc/bash.bashrc and /etc/profile # sources /etc/bash.bashrc). #if [ -f /etc/bash_completion ] && ! shopt -oq posix; then # . /etc/bash_completion #fi
The default number of rows for head and tail can be modified, for example:
//Show only the first two lines root@ubuntu:/home/xtwy# head -2 ~/.bashrc # ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
Tail command may often be used when viewing the growth of log file content. For example, after hadoop is started, many logs will be generated, but when problems occur, tail command can be used to dynamically monitor the growth of log file content and see where the problem is.
//Initial display root@ubuntu:/home/xtwy# tail -f hello.txt hello linux //Adding content to a document root@ubuntu:/home/xtwy# echo "hello linux linux" >> hello.txt //Additional output root@ubuntu:/home/xtwy# tail -f hello.txt hello linux hello linux linux
(5) Other Common Document Operating Commands
Neither of the following commands will change the contents of the file
root@ubuntu:/home/xtwy# cp hello.txt hello1.txt root@ubuntu:/home/xtwy# ls Desktop Downloads hello1.txt literature Pictures Templates Documents examples.desktop hello.txt Music Public Videos //Sort by file content root@ubuntu:/home/xtwy# sort hello1.txt hello linux hello linux linux //Inverse Sequence Output root@ubuntu:/home/xtwy# sort -r hello1.txt hello linux linux hello linux //diff for content comparison root@ubuntu:/home/xtwy# diff hello1.txt hello.txt //Adding content to a document root@ubuntu:/home/xtwy# echo "hello linux linux" >> hello.txt //Content comparison root@ubuntu:/home/xtwy# diff hello1.txt hello.txt 2a3 > hello linux linux //Format output //- The u parameter divides the file into blocks //The two files for comparison are represented by -, +, respectively. //In this case - for Hello 1. txt, +for hello.txt root@ubuntu:/home/xtwy# diff -u hello1.txt hello.txt --- hello1.txt 2015-08-22 17:28:44.071202558 -0700 +++ hello.txt 2015-08-22 17:29:49.131181281 -0700 //@@ xxx @ is used to identify the start number and number of rows //- 1,2 means hello 1.txt file start number is 1, line number is 2 //+ 1,3 means the hello.txt file has a starting number of 1 and a line number of 3. @@ -1,2 +1,3 @@ hello linux hello linux linux +hello linux linux
Author: foreknow
Links: https://www.jianshu.com/p/d3c673b3f905
Source: Brief Book
The copyright of the brief book belongs to the author. For any form of reprinting, please contact the author for authorization and indicate the source.