Common Linux operations

Common Linux operations

 

1. Run sh file

The first method:

First you have to open a terminal.
Then enter sudo su
 Then enter the password. This has achieved root User permissions.
Then find the file
First, check whether the file has execution permission. If not, change the execution permission of the file, and then carry out the remaining operations implement./sh File name such.sh It runs.

The second method:

sh xx.sh

2. Check the file status, such as modification time, creation time, file size, etc

For example, I have a file named test Log, use stat to view the status of the file

stat test.log

3. View the size of a folder

du -h --max-depth=1

To view the size of a file:

ubuntu@ip-10-0-0-59:~/data/logs/java/tc-spider$ du -sh *
1.1M    error_20180208.log
23M    error_20180209.log
4.0K    error_20180211.log
12K    error_20180212.log
20K    error_20180214.log
12K    error.log
384M    info_20180208.log
1012M    info_20180209.log
1.1G    info_20180210.log
2.0G    info_20180211.log
2.1G    info_20180212.log
2.0G    info_20180213.log
1.9G    info_20180214.log
2.0G    info_20180215.log
2.0G    info_20180216.log
46M    info.log

 

df is used to view the space utilization of the device

$ df -lh

#View device usage

Reference: http://blog.csdn.net/wangjunjun2008/article/details/19840671

4. Linux looks for a string in a folder

grep -rn "6402105992922202358" *         //  The command 6402105992922202358 executed is the string to find

For example:

5. Use SSH to link remote ip

Under Windows, I'm used to ssh login with Xshell, and Mac can use Terminal directly.

Some common commands of ssh:

ssh root@ip

Log in to the server with the specified ip using the root account. Next, you need to replace the ip with the ip of your own server. If the server does not use a standard port, such as port 2345, it is:

ssh root@ip -p 2345

Exit the currently logged in server:

exit

6. Use of SCP command

Here are some simple examples:

The command to copy local files to the server is as follows:

scp <local file> <remote user>@<remote machine>:<remote path>

 

Upload file:

[root@test test]# scp ./mytest/password.php 172.30.4.42:/tmp/test2

Set the password under the mytest directory in the current directory Upload PHP to 172.30.4.42 server / tmp/test2 directory.

 

Upload directory:

[root@test test]# scp -r ./mytest 172.30.4.42:/tmp/test2

Upload the mytest directory in the current directory to the 172.30.4.42 server / tmp/test2 directory.

 

If you want to Copy a remote file to a local location:

scp <remote user>@<remote machine>:<remote path> <local file>

Download File

[root@test test]# scp 172.30.4.42:/tmp/test2/aaa.php ./

Add / TMP / test2 / AAA. In 172.30.4.42 Linux system copy the PHP file to the current directory

Download directory

[root@test test]# scp -r root@172.30.4.42:/tmp/test2 ./

copy the / tmp/test2 directory in the 172.30.4.42 Linux system to the current directory. Add root @ in front of 172.30.4.42 and prompt for the password. If not, you will be prompted for the user name and password

See scp command for details: scp --help

7. zip compression and unzip decompression

Compress a directory with the following command:

zip -r  file.zip FolderName

Where - r means to compress the folder (i.e. recycle the file), file Zip represents the file name generated after compression, and FolderName represents the directory or folder name to be compressed

For example:

zip -r Projects.zip Projects/

If you are compressing a file, remove the - r parameter.

Decompression:

unzip file.zip

For example:

unzip Projects.zip

8. Server and local file transfer

 

For those who often use Linux system, it is necessary to upload local files to the server or download files from the server to the local. rz / sz command is very convenient to help us realize this function, but there are many problems Linux system These two commands are not available initially. Today, we will briefly explain how to install and use rz and sz commands. yum install:
yum install -y lrzsz 
Use the following:
 
 
 
sz command sends files to local:
 
 
# sz filename

 

rz command to upload files locally to the server:
# rz

 

After executing the command, select the file to upload in the pop-up box.
Note: open SecureCRT software - > Options - > session options - > x / Y / zmodem to set the upload and download directories.
 

9. Creating and switching users

First, add an ordinary user with the adduser command. The command is as follows:

#adduser tommy / / add a user named tommy
#passwd tommy / / modify the password
Changing password for user tommy.
New UNIX password:     //Enter the new password here
Retype new UNIX password:  //Enter the new password again
passwd: all authentication tokens updated successfully.

Delete user:

userdel test

Delete test user

Switch users:

have access to su Command to switch users, su yes switch user Toggle user abbreviations. You can switch from ordinary users to root User, or from root Users switch to normal users. Switch from normal user to root The user needs to enter a password from root Users do not need to enter a password to switch to ordinary users.
Command format: su [parameter] [-] [user name]
The default value of user name is root. 
Usage example:
su zhidao #Switch to zhidao user
su #Switch to root

10. View the command of process occupied port and port number occupied process

View the port occupied by the process under linux:
(1) Check the process number corresponding to the program: ps -ef | grep process name

(2) View the port number occupied by the process number: netstat -nltp | grep | process number

ubuntu: view the port number occupied by the process: netstat -anp | grep pid

The process number used to view the port number under linux:
(1) Use lsof command: lsof -i: port number

 

Tags: Linux

Posted by olko on Fri, 20 May 2022 08:50:27 +0300