Data mining engineers do not always work in a graphical interface, and with the development of big data open source technology, there will be more tasks to be done in the command-line environment, such as in the Hadoop environment for the import and export of data files, in the Ubuntu environment for the installation and configuration of the Python library, Therefore, you need to master some basic, commonly used Linux commands. This paper summarizes the topic.
1. Directory operations (1) absolute and relative paths
Absolute Path : The path must be written by the root directory "/", for example:/usr/local/lib
relative path : The path is not written by the root directory "/", for example: Local/lib. At this point, this directory is equivalent to the current directory as the "root directory", if the current directory changes, then the relative path will need to be modified.
(2) Print current directory: PWD
命令格式: pwd
Returns the absolute path of the current directory.
(3) Switch directory: CD
命令格式: cd 目录名cd 绝对路径cd 相对路径cd . --切换到当前目录cd .. --切换到上一层目录cd / --切换到根目录cd ~ --切换到home目录
(4) Create directory: mkdir
命令格式: mkdir 新目录名(既可以是绝对路径,也可以是相对路径)
-P parameter:
mkdir -p 新目录名
The effect of-p is that when the-p parameter is not used, an error is made if the upper-level directory where the directory is to be created does not exist, and when the-p parameter is used, the top-level directory is created directly at once. As follows: Create Tmp4 in Tmp3, where Tmp3 was not present before. When-P is not used, the Tmp3 is not present; When using-P, the Tmp3 and Tmp4 are created once.
(5) View contents: ls
命令格式: ls <目录>
use the LS command directly to view the contents of the current directory.
use the LS command + path to view the contents of the specified directory.
Different colors, meaning that the meanings are not the same:
White: Normal file
Blue: Directory
Green: Executable file
Red: Compress files
Light blue: Link file
Yellow: Device File
Grey: Other Files
(6) Copy directory: Cp-r
The CP command can be used both to copy files and to copy directories, but you must use the-r parameter when copying a directory .
命令格式: cp -r 源目录 目标目录
Copy the TMP2 directory under TMP1 to the Tmp3 directory:
Here, first of the/usr 6.csv file is copied to/USR/TMP1/TMP2, and then try to copy the TMP2 directory to Tmp3, without the-r parameter, error, cannot be copied. After adding the-R parameter, the copy succeeds. And check that the discovery is copied together with the files in the directory.
(7) Mobile directory: MV
命令格式: mv 源目录 目标目录
When you move a directory, the files in the directory are moved together.
When the target directory does not exist, the rename operation is performed while the directory is being moved.
Here,/USR/TMP9 does not exist, so the Tmp4 will be moved in the past, will be renamed at the same time.
When the source directory and the target directory are in the same parent directory, the original directory is actually renamed.
(8) Delete directory: RmDir, RM
命令格式: rmdir 目录名
Create a new directory in/usr TMP5, you can delete it directly using RmDir.
rmdir -p 目录名
Recursively create a new directory in/usr TMP6/TMP7, when you use RmDir to delete TMP7, only the TMP7 is deleted, its parent directory is TMP6, and if the-p parameter is used, its parent directory tmp6 is deleted, as in the example when TMP8 is deleted.
However, when you want to delete the directory is not empty, such as TMP2 in the 6.csv file, and then use rmdir Delete it will be an error. This is the time to use the RM command.
The RM command can be used either to delete files or to delete directories, but you must increase the-R parameter at this point.
The rmdir command can only be used to delete directories.
2. File operations (1) Copying files: CP
命令格式: cp 文件名 目录</文件名>
With the CP command, a file is copied to the specified location, and the file name can be set after the location directory is specified, which is equivalent to renaming the file after copying it.
(2) Moving files: MV
命令格式: mv 文件名 目录</文件名>
(3) Delete file: RM
命令格式:rm 文件名
(4) Read and print the contents of the file: Cat
命令格式: cat 文件名
(5) See more file contents: more, less
When the file content is too many, one screen can not be displayed, if you use cat can only show the last part, this time need to use more or less.
More after reading a screen, you can pass the space (print the next screen), or enter (print the next line), you can at any time through the Q end browsing mode.
命令格式: more 文件名
Less and more function the same: Split screen to browse long text content, but more than more convenient, you can "back" printing, that is, looking forward. You can use Q to exit the browsing mode at any time by using the top and bottom arrow keys, and looking backwards, with the space and enter key acting like the more command.
命令格式: less 文件名
(6) Read the first n rows: Head
命令格式: head <-n> 文件名
The first 10 lines are printed by default.
With the parameter n=3, only the first three lines are printed.
(7) After reading N rows: Tail
命令格式: tail <-n> 文件名
The default is 10 lines after printing.
Using the parameter n=3, only the following three lines are printed.
(8) Search for files: find
命令格式: find -name filename --按文件名搜索
You can use a command or a type of fuzzy matching search.
With the-iname parameter instead of the-name, the case can be ignored.
You can use the-o parameter to concatenate multiple search conditions, which are "or" relationships between the conditions.
You can also set the search directory to complete the search in the specified directory.
3. File attributes (1) Introduction
Use the Ls-l command to view the property information for a file under a specified directory. Here the "file" is a generalized file, including files, directories, devices, partitions and so on.
命令格式: ls -l <目录>
Meaning explanation:
Total: The size of all files in this directory (in kilobytes) is the fifth column in the list of file attributes, the sum of the file size, and the unit is B.
First column: File type and file permissions, total 10 bits.
which
1th bit: File type (d: directory,-: Normal file ...) )
The last 9 bits, each 3 bits of a group, are a combination of rwx three parameters. r--readable, w--writable, x--executable.
The first three-bit, which represents the permissions of the primary (user) of the file, the second three-bit, represents the permissions of the group to which the file belongs, and the third three-bit represents the permissions of other non-owning group users (others).
(2) Change belongs to group: CHGRP
命令格式: chgrp 组名 文件名
First use the Groupadd command to add a group TESTGRP, and then use CHGRP to change the owning group of file 5.txt to this group.
(3) Change the owner: Chown
命令格式: chown <-R> 用户名 文件名
First use the Useradd command to add a user testusr, and then use Chown to change the owner of the file 5.txt to this user.
When modifying a directory, it is important to note that the-R parameter is used to implement cascading changes that not only modify the directory, but also modify all subdirectories or files underneath the directory. -R is only valid for modifying the directory.
(4) Change user operation rights: chmod
命令格式: chmod <-R> 权限值 文件名
In order to facilitate the modification of permissions, the permissions are represented by numbers, r:4,w:2,x:1,-:0, the values of three permission groups are stitched together, for example: The above rw-r--r--, the first permission group: rw-equals 4+2+=6, the second permission group: r--equals 4+0+0= 4, third permission group: r--equals 4+0+0=4, so its permission value is 644.
Similarly, use the-R parameter to implement cascading changes .
On a Linux system, the default permission for a directory is 755, and the default permission for a file is 644.
-RWXRWX---The number is 770, below we use the chmod command to modify the 5.txt file permissions.
4. Shortcut keys (1) to terminate the current command: CTRL + C
Used to terminate the current command.
(2) Auto Completion: TAB
Help you automatically complete the command or directory, if there is no response, indicating that the system can not be judged, need to enter more information.
(3) Exit the current terminal: Ctrl+d, exit
Exit the current terminal and close the window.
(4) Pause the current process: Ctrl + Z
Pauses the current process, using FG recovery.
(5) Clear screen: ctrl+l
5. Other commands (1) View command Help information: Man
命令格式: man 命令名称
The help document used to see a command.
(2) Packaging and compression of files:
The most common compressed files under Linux usually end in. tar.gz, in addition to. tar,.gz,. Bz2,.zip and so on.
Files compressed by the. Gz:gzip Compression Tool
Files compressed by the. BZ2:BZIP2 Compression Tool
. Tar:tar packaged files (tar does not compress function, just merge a directory into one file)
. tar.gz: It can be understood that the tar is packaged first and then gzip compressed
. tar.bz2: Ditto, first use tar to pack, then bzip2 compress
压缩: gzip 文件名解压缩: gzip -d 压缩包.gz
Note: gzip does not compress the directory.
压缩: bzip2 文件名解压缩: bzip2 -d 压缩包.bz2
打包: tar -cvf 文件名.tar 文件名解包tar: tar -xvf 压缩包.tar
打包后gzip压缩: tar -czf 文件名.tar.gz 文件名解压缩tar.gz: tar -xzvf 压缩包.tar.gz
打包后bzip2压缩: tar -cjf 文件名.tar.bz2 文件名解压缩tar.bz2:
(3) system shutdown and restart:
halt 立刻关机poweroff 立刻关机shutdown -h now 立刻关机(root用户使用)shutdown -h 10 10分钟后自动关机reboot 立刻重启shutdown -r now 立刻重启(root用户使用) shutdown -r 10 10分钟后自动重启(root用户使用)
Linux Basic Command Summary