Welcome to Raspbot V2 repository
当製品の日本語版マニュアル資料は、こちらからご取得ください。
6. Common Linux commands and use of vim editor
6. Common Linux commands and use of vim editor
6. Common Linux commands and use of vim editor6.1. Common Linux commands6.1.1. Linux file and directory management6.1.2.Linux View file contents6.2. Use of Vim editor6.2.1. Install vim6.2.2. Usage of vim
6.1. Common Linux commands
- Check the remaining space of the SD memory card
df -h
- Check the IP address
ifconfig
- Compression: tar –zcvf filename.tar.gz dirname
- Decompression: tar –zxvf filename.tar.gz
- Linux system often uses apt (Advanced Package Tool) advanced software tool to install software
sudo apt-get install xxx to install software.
sudo apt-get update to update the software list.
sudo apt-get upgrade to update installed software.
sudo apt-get remove xxx to delete software.
6.1.1. Linux file and directory management
We know that the directory structure of Linux is a tree structure, and the top directory is the root directory /.
Other directories can be added to the tree by mounting them, and they can be removed by unmounting them.
Before we start this tutorial, we need to know what absolute and relative paths are.
· Absolute path: The path is written from the root directory /, for example: /usr/share/doc.
· Relative path: The path is not written from /, for example, from /usr/share/doc to /usr/share/man, it can be written as: cd ../man This is the relative path!
Common commands for processing directories
Next, let's take a look at some common commands for processing directories:
· ls: List directories
· cd: Switch directories
· pwd: Display the current directory
· mkdir: Create a new directory
· rmdir: Delete an empty directory
· cp: Copy files or directories
· rm: Remove files or directories
You can use man [command] to view the usage documentation of each command, such as: man cp.
ls (list directory)
In Linux systems, the ls command may be the most frequently run.
Syntax:
[root@www ~]# ls [-aAdfFhilnrRSt] directory name
[root@www ~]# ls [--color={never,auto,always}] directory name
[root@www ~]# ls [--full-time] directory name
Options and parameters:
· -a: All files, including hidden files (files starting with .) are listed together (commonly used)
· -d: Only the directory itself is listed, not the file data in the directory (commonly used)
· -l: Long data string listing, including file attributes and permissions, etc. (commonly used)
List all files in the home directory (including attributes and hidden files)
[root@www ~]# ls -al ~
cd (change directory)
cd is the abbreviation of Change Directory, which is a command used to change the working directory.
Syntax:
cd [relative path or absolute path]
#Use the mkdir command to create the runoob directory
[root@www ~]# mkdir runoob
#Use the absolute path to switch to the runoob directory
[root@www ~]# cd /root/runoob/
#Use the relative path to switch to the runoob directory
[root@www ~]# cd ./runoob/
# Indicates returning to your home directory, which is the /root directory
[root@www runoob]# cd ~
# Indicates going to the current parent directory, which means the parent directory of /root;
[root@www ~]# cd ..
After a few more operations, you should be able to understand the cd command well.
pwd (display current directory)
pwd is the abbreviation of Print Working Directory, which is the command to display the current directory.
[root@www ~]# pwd [-P]
Options and parameters:
· -P: Display the actual path instead of using the link path.
Example: Simply display the current working directory:
[root@www ~]# pwd
/root <== Display the directory~
The example displays the actual working directory instead of the directory name of the link file itself.
[root@www ~]# cd /var/mail <==Note that /var/mail is a link file
[root@www mail]# pwd
/var/mail <==List the current working directory
[root@www mail]# pwd -P
/var/spool/mail <==What's going on? There is a big difference between adding -P and not adding -P~
[root@www mail]# ls -ld /var/mail
lrwxrwxrwx 1 root root 10 Sep 4 17:54 /var/mail -> spool/mail
# You should know why after reading this, right? Because /var/mail is a link file, linking to /var/spool/mail
# So, after adding the pwd -P option, it will not display the data of the link file, but the correct full path!
mkdir (create a new directory)
If you want to create a new directory, then use mkdir (make directory).
Syntax:
mkdir [-mp] directory name
Options and parameters:
· -m: Configure the file permissions! Direct configuration, no need to look at the default permissions (umask)~
· -p: Helps you directly create the required directory (including the parent directory) recursively!
Example: Please try to create several new directories under /tmp:
[root@www ~]# cd /tmp
[root@www tmp]# mkdir test <==Create a new directory named test
[root@www tmp]# mkdir test1/test2/test3/test4
mkdir: cannot create directory `test1/test2/test3/test4':
No such file or directory <== There is no way to create this directory directly!
[root@www tmp]# mkdir -p test1/test2/test3/test4
Adding this -p option can help you create multiple directories by yourself!
Example: Create a directory with permissions of rwx--x--x.
[root@www tmp]# mkdir -m 711 test2
[root@www tmp]# ls -l
drwxr-xr-x 3 root root 4096 Jul 18 12:50 test
drwxr-xr-x 3 root root 4096 Jul 18 12:53 test1
drwx--x--x 2 root root 4096 Jul 18 12:54 test2
In the permission part above, if -m is not added to force the configuration of attributes, the system will use the default attributes.
If we use -m, as in the above example, we give -m 711 to give the new directory drwx--x--x permissions.
rmdir (delete empty directory)
Syntax:
rmdir [-p] directory name
Options and parameters:
· -p : Delete the "empty" directory at the previous level
Delete the runoob directory
[root@www tmp]# rmdir runoob/
Delete the directory created in the mkdir example (under /tmp)!
[root@www tmp]# ls -l <==See how many directories exist?
drwxr-xr-x 3 root root 4096 Jul 18 12:50 test
drwxr-xr-x 3 root root 4096 Jul 18 12:53 test1
drwx--x--x 2 root root 4096 Jul 18 12:54 test2
[root@www tmp]# rmdir test <==You can delete it directly, no problem
[root@www tmp]# rmdir test1 <==Because there is still content, it cannot be deleted!
rmdir: `test1': Directory not empty
[root@www tmp]# rmdir -p test1/test2/test3/test4
[root@www tmp]# ls -l <==Look, test and test1 are gone in the output below!
drwx--x--x 2 root root 4096 Jul 18 12:54 test2
Using the -p option, you can immediately delete test1/test2/test3/test4 at once.
However, please note that this rmdir command can only delete empty directories. You can use the rm command to delete non-empty directories.
cp (copy files or directories)
cp means copying files and directories.
Syntax:
[root@www ~]# cp [-adfilprsu] source file (source) destination file (destination)
[root@www ~]# cp [options] source1 source2 source3 .... directory
Options and parameters:
· -a**:** Equivalent to -pdr, as for pdr, please refer to the following description; (commonly used)
· -d**:** If the source file is a link file, copy the link file attributes instead of the file itself;
· -f**:** For force, if the destination file already exists and cannot be opened, remove it and try again;
· -i**:** If the destination file (destination) already exists, it will ask for the action before overwriting (commonly used)
· -l**:** Create a hard link file instead of copying the file itself;
· -p**: **Copy the file attributes together with the file instead of using the default attributes (commonly used for backup);
· -r**: **Recursive and continuous copying, used for directory copying; (commonly used)
· -s**: **Copy as a symbolic link file, that is, a "shortcut" file;
· -u**: **Upgrade the destination only if the destination is older than the source!
As root, copy .bashrc in the root directory to /tmp and name it bashrc
[root@www ~]# cp ~/.bashrc /tmp/bashrc
[root@www ~]# cp -i ~/.bashrc /tmp/bashrc
cp: overwrite `/tmp/bashrc'? n <==n does not overwrite, y means overwrite
rm (remove files or directories)
Syntax:
rm [-fir] file or directory
Options and parameters:
· -f: It means force, ignore non-existent files, and no warning message will appear;
· -i: Interactive mode, the user will be asked whether to act before deleting
· -r: Recursive deletion! Most commonly used for directory deletion! This is a very dangerous option! ! !
·
Delete the bashrc just created in the cp example!
[root@www tmp]# rm -i bashrc
rm: remove regular file `bashrc'? y
If you add the -i option, it will automatically ask you to avoid deleting the wrong file name!
mv (Move files and directories, or modify names)
Syntax:
[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory
Options and parameters:
· -f: force means if the target file already exists, it will not be asked and will be directly overwritten;
· -i: If the target file (destination) already exists, it will ask whether to overwrite!
· -u: If the target file already exists and source is newer, it will be upgraded (update)
Copy a file, create a directory, and move the file to the directory
[root@www ~]# cd /tmp
[root@www tmp]# cp ~/.bashrc bashrc
[root@www tmp]# mkdir mvtest
[root@www tmp]# mv bashrc mvtest
Move a file to a directory, that's it!
Rename the directory to mvtest2
[root@www tmp]# mv mvtest mvtest2
6.1.2.Linux View file contents
Use the following commands in Linux to view the contents of a file:
· cat displays the contents of a file starting from the first line
· tac displays the contents of a file starting from the last line. You can see that tac is cat written backwards!
· nl outputs the line number when displaying!
· more displays the contents of a file page by page
· less is similar to more, but better than more, it can turn pages forward!
· head only reads the first few lines
· tail only reads the last few lines
You can use man [command] to view the usage documentation of each command, such as: man cp.
cat
Display the file contents starting from the first line
Syntax:
cat [-AbEnTv]
Options and parameters:
· -A: Equivalent to the integrated option of -vET, which can list some special characters instead of blanks;
· -b: List line numbers, only display line numbers for non-blank lines, blank lines are not marked with line numbers!
· -E: Display the trailing line break byte $;
· -n: Print line numbers, including blank lines, which is different from the -b option;
· -T: Display the [tab] key as ^I;
· -v: List some invisible special characters
Check the contents of the /etc/issue file:
[root@www ~]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel run an m
tac
tac is the opposite of the cat command. The file content is displayed starting from the last line. It can be seen that tac is cat written backwards! Such as:
[root@www ~]# tac /etc/issue
Kernel r on an m
CentOS release 6.4 (Final)
nl
Display line numbers
Syntax:
nl [-bnw] file
Options and parameters:
· -b: Specify the line number specification method, there are two main ways:
-b a: Indicates whether it is a blank line or not, the line number is also listed (similar to cat -n);
-b t: If there is a blank line, the blank line does not list the line number (default value);
· -n: List the line number representation method, there are three main ways:
-n ln: The line number is displayed at the leftmost side of the screen;
-n rn: The line number is displayed at the rightmost side of its own column, and no 0 is added;
-n rz: The line number is displayed at the rightmost side of its own column, and 0 is added;
· -w: The number of digits occupied by the line number column.
Example 1: Use nl to list the contents of /etc/issue
[root@www ~]# nl /etc/issue
1 CentOS release 6.4 (Final)
2 Kernel run an m
more
Flip pages one by one
[root@www ~]# more /etc/man.config
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6d
....(omitted in the middle)....
--More--(28%) <== Focus on this line! Your cursor will also wait for your command here
During the running of the more program, you can press several keys:
· Space: represents flipping down one page;
· Enter: represents flipping down "one line";
· / string: represents searching for the keyword "string" in the displayed content;
· :f: immediately display the file name and the number of lines currently displayed;
· q: represents leaving more immediately and no longer displaying the contents of the file.
· b or [ctrl]-b: represents flipping back pages, but this action is only useful for files, not for pipelines.
less
Turn page by page, the following example outputs the contents of the /etc/man.config file:
[root@www ~]# less /etc/man.config
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6d
....(omitted in the middle)....
: <== Here you can wait for your input command!
The commands that can be entered when running less are:
· Spacebar: scroll down one page;
· [pagedown]: scroll down one page;
· [pageup]: scroll up one page;
· / string: search down for "string";
· ? string: search up for "string";
· n: repeat the previous search (related to / or ?!)
· N: repeat the previous search in reverse (related to / or ?!)
· q: quit the less program;
head
Take out the first few lines of the file
Syntax:
head [-n number] file
Options and parameters:
· -n: followed by a number, indicating the number of lines to be displayed
[root@www ~]# head /etc/man.config
By default, the first 10 lines are displayed! To display the first 20 lines, you need to do this:
[root@www ~]# head -n 20 /etc/man.config
tail
Take out the last few lines of the file
Syntax:
tail [-n number] file
Options and parameters:
· -n: followed by a number, indicating the number of lines to be displayed
· -f: Indicates continuous detection of the following file name, and tail detection will not end until [ctrl]-c is pressed
[root@www ~]# tail /etc/man.config
# By default, the last ten lines are displayed! To display the last 20 lines, you need to do this:
[root@www ~]# tail -n 20 /etc/man.config
6.2. Use of Vim editor
6.2.1. Install vim
First update the index source:
sudo apt-get update
Install the vim editor:
sudo apt-get install vim
6.2.2. Usage of vim
vim has 3 modes: insert mode, command mode, and low line mode.
Insert mode: You can enter characters in this mode, and press ESC to return to command mode.
Command mode: You can move the cursor, delete characters, etc.
Low line mode: you can save files, exit vim, set vim, search and other functions (low line mode can also be regarded as command mode)
Open files, save, close files (used in vi command mode)
vim filename //Open filename
:w //Save file
:q //Exit the editor. If the file has been modified, please use the following command
:q! //Exit the editor without saving
:wq //Exit the editor and save the file
Insert text or line (used in vi command mode, after executing the following command, you will enter insert mode, press ESC to exit insert mode)
a //Add text to the right of the current cursor position
i //Add text to the left of the current cursor position
A //Add text to the end of the current line
I //Add text at the beginning of the current line (the beginning of the line with non-blank characters)
O //Create a new line above the current line
o //Create a new line below the current line
R //Replace (overwrite) the current cursor position and some text after it
J //Merge the cursor line and the next line into one line (still in command mode)
Delete, restore characters or lines (used in vim command mode)
x //Delete the current character
nx //Delete n characters starting from the cursor
dd //Delete the current line
ndd //Delete n lines including the current line
u //Undo the last step
U //Undo all operations on the current line
Copy, paste (used in vi command mode)
yy //Copy the current line to the buffer
nyy //Copy the current line to the buffer n lines below
yw //Copy the characters from the cursor to the end of the word
nyw //Copy n words starting from the cursor
y^ //Copy the content from the cursor to the beginning of the line
y$ //Copy the content from the cursor to the end of the line
p //Paste the content in the clipboard after the cursor
P //Paste the content in the clipboard before the cursor
Set the line number (used in vi command mode)
:set nu //Display the line number
:set nonu //Cancel displaying line numbers