Linux developers are essential to our system, but also often come into contact. However, Linux command more, some less common it is difficult to remember. So how do we more efficient use of Linux commands, without having to fully learn it? Today I gave you share in the development process, the more commonly used, but also more practical Linux commands.
01 view files or logs
Look at the file or log file, I believe that almost everyone will come into contact with. Among them, the most used is the head, tail and the cat command.
1、head
He represents the head, to see the first few rows of data.
command | Explanation |
---|---|
head test.log | View first few lines of the log file contents test.log |
head -n 20 test.log | View first 20 lines of the log file contents test.log |
head -n -20 test.log | View test.log except for the last 20 lines of the log content |
2、tail
He represents the tail, to see the last few lines or real-time log.
command | Explanation |
---|---|
tail -f test.log | Real-time view test.log last few lines of the log file contents |
tail -f -n 20 test.log | Real-time view test.log last 20 lines of log file contents |
tail -n 20 test.log | View test.log last 20 lines of log file contents |
tail -n +20 test.log | All contents of the log file to see test.log 20 lines |
3、cat
To view the file contents.
command | Explanation |
---|---|
cat test.log | View all test.log file contents |
02 Local Copy
The same server between different directory copy files or directories are also frequently used.
1, copy the files
command | Explanation |
---|---|
cp test.log /home/geshan/logs | Test.log copy files to / home under / geshan / logs / directory |
cp test.log /home/geshan/logs/cptest.log | Test.log copy files to / home / under geshan / logs / directory and rename cptest.log |
2, copy the folder
command | Explanation |
---|---|
cp -r logs/ /home/geshan/ | Under the logs directory and copy all files to the directory / home / geshan / directory, then the directory will have logs directory |
cp -r logs/ /home/geshan/testlogs/ | Under the logs directory and copy all files to the directory / home / geshan / testlogs / directory and rename testlogs logs directory, provided that / home / geshan / directory directory is not testlogs |
Note: -r represents -R or cycle, all files and directories under the directory cycle. All of the following commands as well.
03 Remote Copy
When we need to copy a file or directory to a local login server from a remote server, most people think of first login to the remote server to download the file, and then log on the local server and upload files. This is too much trouble, efficiency is not high.
1, the local server replicated to a remote server
(1) copy files
Command Format | |
---|---|
Command 1 | scp local_file remote_username@remote_ip:remote_folder |
Command 2 | scp local_file remote_username@remote_ip:remote_file |
Command 3 | scp local_file remote_ip:remote_folder |
Command 4 | scp local_file remote_ip:remote_file |
1st, 2nd specifies the user name, enter your user code after the command is executed, the first one specifies only a remote directory, file name unchanged;
the second specifies the file name;
the first three or four do not specify a username after the command is executed with the current server default user name and enter your user code, only the first three specified remote directory, file name unchanged, the fourth specifies the file name.
Example 1: Copy test.log to 192.168.130.130 server from a local server / home / geshan / directory and rename 20190125.log
command:
scp test.log [email protected]:/home/geshan/20190125.log
Example 2: Copy test.log to 192.168.130.130 server from a local server / home / geshan / directory
command:
scp test.log [email protected]:/home/geshan/
(2) Copy directory
Command Format | |
---|---|
Command 1 | scp -r local_folder remote_username@remote_ip:remote_folder |
Command 2 | scp -r local_folder remote_ip:remote_folder |
The first one specifies the user name and password you need to enter a user command;
the first two do not specify a user name, you need to enter a user name and password command.
Example: copy from the local server tomcat directory under / home / appuser / directory to 192.168.130.130 server / home / geshan / directory
command:
scp -r /home/appuser/tomcat [email protected]:/home/geshan/
2, copied from the remote server to a local server
From the remote to the local copy command scp command similar to the above, as long as the copy from the local to the remote command sequence behind two parameters interchanged on the line.
(1) copy files
Examples: Copy from / home / geshan / test.log path 192.168.130.130 remote server to the local file server / home / appuser / Path
command:
SCP [email protected]: /home/geshan/test.log / Home / appuser /
(2) Copy directory
Examples: Tomcat folder from the / home / geshan / path 192.168.130.130 remote server to a local server / home / appuser / path
command:
scp -r [email protected]:/home/geshan/tomcat/ /home/appuser/
Note: ip address and the path between (ie, between the colon) without any spaces, otherwise it will error. When the first communication service and another one (such as copying files), you will be prompted whether you trust, you can enter yes.
04 compression and decompression
Compression and decompression of files are also often encountered. If you download all files in a directory, you can download the catalog, but directly to the download directory, then a lot of files once the download process is very easy to lost files, download speed is relatively slow. Under normal circumstances is to compress the folder before downloading. Here to introduce four kinds of compression and decompression command.
1, tar command
Decompression:
tar zxvf fileName.tar
compression:
tar czvf fileName.tar folder/
Example:
unzip a file log.tar:
tar zxvf log.tar
Compressed log directory and name log.tar:
tar czvf log.tar log/
2, gz command
compression:
gzip FileName
Note: compressed .gz files in the source file will disappear
if you want to keep the source file, use the following command
gzip -c 源文件 > 压缩文件
Decompression:
gunzip FileName.gz
或者 gzip -d FileName.gz
.tar.gz and .tgz
unpack:
tar zxvf FileName.tar.gz
compression:
tar zcvf FileName.tar.gz DirName
Compress multiple files:
tar zcvf FileName.tar.gz DirName1 DirName2 DirName3 ...
Example:
unzip the file tomcat.tar.gz
tar zxvf tomcat.tar.gz
Compression tomcat directory and file name tomcat.tar.gz
tar zcvf tomcat.tar.gz tomcat/
3, zip command
Decompression:
the unzip filename.zip
Compression:
zip -r fileName.zip folder/
或者 zip -r fileName.zip folder1/ folder2/ .....
或者 zip -r fileNamezip file1 file2 ...
Note: Compression directory, need to add -r, represents the cycle all files and directories in the directory. When multiple compressed files or directories, you need a space between files or directories.
Example:
unzip a file log.zip:
unzip log.zip
Compressed log file directory and name log.zip:
zip -r log.zip log/
Log1 compression and log2 directory and name log.zip:
zip -r log.zip log1/ log2/
Compression test1.log and test2.log file and name it log.zip:
zip log.zip test1.log test2.log
4, rar command
Similarly, and the zip command similar, just the zip rar can be changed.
05 modify permissions
Linux file system and directory access permissions are different, can determine who can perform operations on the file and directory by some way.
Among them, access to the file or directory is divided into read-only, write-only, and executes three, representing readable, writable and executable.
1, chgrp command
Change group of files or directories belongs.
command:
chgrp groupName fileName
或者 chgrp -R groupName dirName/
Example:
the file belongs to the group test.log amended as set geshan
chgrp geshan test.log
The group logs directory and all files in that directory belongs to the group modified to geshan
chgrp -R geshan logs/
2, chown command
Change the user a file or directory belongs.
command:
chown userName fileName
或者 chown -R userName dirName/
Example:
the file belongs to the user to modify test.log user geshan
chown geshan test.log
The modification logs directory and all files in your user directory for this user geshan
chown -R geshan logs/
3, chmod command
For changing the file or directory access. Use it to control user access to files or directories.
command:
chmod mode fileName
或者 chmod -R mode dirName/
Which, mode there are many express meaning, only the following figure illustrates.
Most people see mode there are 777 or 776 and other privileges, what do these represent?
For convenience, the mode to ABC, wherein a, b, c are each a number, represent the permissions user, group and the other. r = 4, w = 2, x = 1, r represents Read Read, w denotes write Write, x represents eXecute.
- To the property rwx 4 + 2 + 1 = 7;
- To rw- property is 4 + 2 = 6;
- To the property rx 1 = 4 + 5.
Example:
the file test.log modifications may perform read and write (i.e., 777) operations.
chmod 777 test.log
The modification logs directory and all files in the directory is read-write (666) operation.
chmod -R 666 logs/
Input ll (two lowercase LL) in a Linux system, if files or directories to 777 (read and write execute) permission, you can see the front as -rwxrwxrwx. If 666 (read-write) rights, as can be seen in front -rw-rw-rw.
Note: General modify a file or directory permissions requires superuser (root) to modify, or it may be reported insufficient privileges.
These are commonly used Linux commands useful commands, there will certainly be a time to get, I remember collection and forward it to people in need, oh.