This is the 25th day of my participation in the August Genwen Challenge.More challenges in August

1. Perform operations on files and directories

1.1 Ls Command; Function: Displays information about files and directories

lsDisplays a list of files in the current directory by defaultls -aShow all files including hidden filesls -lDisplays file properties, including size, date, symbolic link, read-write, and executablels -lhDisplay file size, print file size in an easy-to-understand format (e.g1K 234M2G)
ls -ltDisplays files sorted by modification timeCopy the code

1.2 CD Command; Function: Rename directory

cd dirTo switch to the current directorydirdirectorycd/ Switch to the root directorycd. Switch to the previous directorycd. /.. Switch to the upper level directorycd~ Switch to the user directory. For example, if the user is root, switch to /rootCopy the code

The difference between the root directory and home directory:

The root directory is the primary file structure of the system, and the home directory is only the directory controlled by non-root users.

As with My Documents on Windows, non-root users can only control files in the home directory, not other files in the root directory.

The root directory is the topmost directory of the device and is represented by /

The home directory is the directory where each user logs in to the system. The home directory is usually under /home. The user name is used as the directory and can be expressed as ~.

CD/Go to the root directory

CD ~/ Go to the home directory

You can use /home/someone to access someone’s home directory

1.3 cp command; Function: Copy a file

cpSource target Copies the file source to targetcp/root/source. Copy the source file under /root to the current directory eg:# cp/home/open_038_dev/external_files/test/test.sh .
cp- av soure_dir target_dir Copies the entire directory. The two directories are exactly the samecp -rSource file destination file copy folder eg:Bak copy a views.bak file with views as the source file
Copy the code

1.4 Rm Command; Run the following command to delete files or directories

rmFile Deletes the file filerm -fFile deletion is not prompted. Can be inrParameter coordinationrm -rfDomed Deletes the Domed directory (folder) and all its contentsrm -iA * Deletes all files starting with the letter A from the current directory and prompts the user to confirm each deletionCopy the code

1.5 mv command; Function: to move the file away, or rename, there is no rename command in uinx, if you want to rename, you can use this command

Command parameters:-b: To overwrite a file, back up the file before overwriting it.-fIf the target file already exists, it will be overwritten without being asked.-iIf the destination file already exists, it will ask whether to overwrite it.-u: Update only if the target file already exists and the source is new-t: --target-directory=DIRECTORY moveAll SOURCE arguments into DIRECTORYmvTo move multiple source files to a directory. In this case, the destination directory is first and the source file is lastChange the name of file source to target
mvsource target# file log1. TXT, log2. TXT, log3. TXT test3 move to the directory
mv log1.txt log2.txt log3.txt test3   
TXT log2. TXT log3. TXT to /opt/soft/test/test4
mv -t /opt/soft/test/test4/ log1.txt log2.txt  log3.txt  
Move all files in the current folder to the upper directory
mv * ../                                                     
Copy the code

1.6 Diff command; Function: Compare file contents

# compare the contents of the files in directory 1 and directory 2
diffdir1 dir2File 1 and file 2 have the same contents. If the file is a text format, the different contents will be displayed. If the file is a binary format, only the two files are different
diff file1 file2
# compare files to show different contents of two filescomm file1 file2Copy the code

1.7 Run the ln command. Function: Create a link

Ln source_PATH Target_path Hard link ln-sSource_path target_path soft link ln is another very important command in Linux. It is used to establish a link for a file in another location. The most common parameter of this command is-s, the specific usage is: Ln -s Source file Destination file When we need to use the same file in different directories, we do not need to put the same file in every directory we need, we can just put the file in a fixed directory and use the ln command to link it in other directories. You don't have to use disk space repeatedly. Eg: ln -s /bin/less /usr/local/bin/less Delete the soft linkrm- rf /target Note: Do not add a slash (/) to the end of the file name. Otherwise, the contents of the folder will be deletedCopy the code

1.8 mkdir command; Create a directory

Mkdir [options] Directory name Directory where the option is located: /bin/mkdir Run permission: all users Run description: Create an empty directoryCopy the code

1.9 PWD command; Run the following command to query a directory

Path: /bin/pwdExecute permission: all users. Function Description: Query the working directoryCopy the code

1.10 Sz command; Function: Download files to local download directory

Sz file pathCopy the code

1.11 rz command; Function: Upload files locally to the server will pop out the file selection window, select the file, click ok

rz
rz -yIf you upload a file to Linux, the file with the same file name will be overwritten. rz-EWhen you upload a file to Linux, if there is a file with the same file name, it will not be overwritten, but will be appended to the uploaded file.0, both files will be stored in this directory, the file name will be added after the next upload.1, and so on;Copy the code

ps:

Check whether the command already exists

If the corresponding RPM package is not installed, the following message is displayed when you enter rz or sz

[root@vdedu test]# sz
-bash: sz: command not found
[root@vdedu test]# rz
-bash: /usr/bin/rz: No such file or directory
Copy the code

Install LRZSZ

LRZSZ is a Unix communication suite that provides X, Y, and ZModem file transfer protocols.

yum -y install lrzsz 
Copy the code

2. Decompress and compress (package) commands

The benefits of reducing file size are as follows: first, you can reduce storage space; Second, the transmission time can be reduced when the file is transmitted through the network.

2.1 Tar command; Function: archive, compression, etc

Tar unpack: tar XVF filename. tar Package: tar CVF filename. tar DirName (note: tar is packaged, not compressed!) -- -- -- -- -- -- -- -. Gz decompression1: gunzip filename. gz Decompress2: gzip-dGz compressed: gzip filename.tar. gz and.tgz decompressed: tar ZXVF filename.tar. gz Compressed: The tar ZCVF FileName. Tar. Gz DirName -- -- -- -- -- -- -- -..bz2 decompression1: bzip2-dFileName..bz2 decompression2: bunzip2 filename. bz2 Compression: bzip2-zBz2 Decompress: tar JXVF filename.tar. bz2 Decompress: tar JCVF filename.tar. bz2 DirName ———————————————1: bzip2-dFileName. Bz decompression2Bz: tar JXVF filename.tar. bz: unknown ———————————————.Z: uncompress filename. Z: uncompress Z compress: tar Zxvf filename.tar. Z compress: tar Zcvf filename.tar. Z DirName ———————————————. Unzip filename. zip: zip filename. zip DirName ———————————————. Rar: rar x filename. rar: Rar a FileName. Rar DirName -- -- -- -- -- -- -- -- -. Lha extract: lha-eFilename. lha Compression: lha-aFileName. Lha FileName -- -- -- -- -- -- -- -- -. The RPM unpacking: rpm2cpio FileName. The RPM | cpio-div-- -- -- -- -- -- -- -. Deb unpacking: ar p FileName. Deb data. The tar. Gz | tar ZXF -Copy the code

2.2 Gzip Command; Function: Compress files in GZ format

Gzip belongs to GNU software and is the preferred compression tool in Linux. The -z option of tar archive command also uses gzip/gunzip to compress and decompress files.

Gzip generates.gz files

Syntax: gzip [options] [file or directory...] Parameters:-cOr - stdout or - to-stdoutOutput the compressed (uncompressed) content to the standard output device, preserving the original file-dOr - decompress decompression-lOr -list If the target file is a compressed file, the compressed size, decompressed size, compression ratio, and decompressed file name are displayed.-rOr -- recursive compression-tOr - test Tests to check the integrity of compressed files-vOr - verbose displays filename and compression ratio for each file.-VOr -version Displays the version number-numSpecifies the speed of compression- 1Or -- fast means fast (low compression ratio)9 -Or -- best Slow (high compression ratio)Copy the code
eg:
1. Compress all files in the current directory linux@ubuntu:~/test$ lsA B Peach Blossom IslandThere are three files
linux@ubuntu:~/test$ gzip *  # compress all files
linux@ubuntu:~/test$ lsA.gz B.gz Peach Blossom Island.mp3.gzThe corresponding.gz files were generated separately, but the original files were deleted.

2. Unpack the cases1All files in and display the compression ratio linux@ubuntu:~/test$ gzip --decompress -v *
a.gz: 1.3% -- replaced with a
b.gz: 1.3% -- replaced with b Peachblossom island.mp3.gz:1.3% -- replaced with peach Blossom island.mp33. Display case1Information about all compressed files in linux@ubuntu:~/test$ gzip --list *
compressed        uncompressed  ratio uncompressed_name
5566197             5638272   1.3% a
5566197             5638272   1.3% b
5566209             5638272   1.3Peach Blossom Island mp316698603            16914816   1.3% (totals) can be seen after compression, uncompressed, compression rate, decompressed file name.4, compress files in the test directory (you can use recursive compression) linux@ubuntu:~$ lsTest a B Peach Blossom Island.mp3 linux@ubuntu:~$ gzip --recursiv test  Compress the files in the directory
linux@ubuntu:~$ lsTest A.gz B.gz Peach Blossom Island.mp3.gz#You can see that it does not generate a compressed file, but the files in the directory are compressed separately. This means that it does not generate a package, that is, the directory can not be compressed, you can use tar package, and then compress. tar-cf test.tar test/ tar -zcfTest.tar. gz Test/can also be used, use tar-zxfAfter decompressing, run gzip test.tar to compress itCopy the code

Note:

  1. Specifies that the compressed file must exist.
  2. Does not compress directories, but can compress files in recursive directories.

3. View the file content

3.1 Cat Command; Function: Display the contents of a file

catfileCopy the code

3.2 tail command; Function: Displays the last few lines of a file

eg:
tail -n 100Aaa. TXT Displays the end of the aaA. TXT file100lineCopy the code

3.3 Running the more command; Function: paging display command

Morefile more command can also command used together with others through the pipeline operator | eg:ps ux|more
ls|more
Copy the code

3.4 Vi Command;

How to enter a command (step) :1. Run vi FileName to go to the editor (default command mode).2. Tap a or I to go into edit mode and type in content such as Hello Linux World!3. Then press ESC on the keyboard to exit edit mode (to enter command mode).4.Finally, press the colon:5Knock wq again! Save and exit. ------- wqwriteQuite does not want to save, q forcibly quit Q!Copy the code

3.5 Touch command; Create an empty file

TXT Create an empty file named aaA.txtCopy the code

4. Basic system commands

4.1 Man Command; Run the following command to view the help information about a command

eg:
man lsAccording tolsHelp content of the commandCopy the code

4.2 w command; Function: Displays the details of the login user

w
eg:
22:06:51 up 43 min,  1 user,  load average: 0.00.0.00.0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
zhoulj   pts/0    10.140.0.109     21:24    0.00s  0.85s  0.09s sshd: zhoulj [priv]
Copy the code

4.3 Who Command; Function: Displays the login user

who
eg:
zhoulj   pts/0        Mar 13 21:24 (10.140.0.109)
Copy the code

4.4 Running the last command; Function: View the most recent login users

last
eg:
zhoulj   pts/0        10.140.0.109     Mon Mar 13 21:24   still logged in   
reboot   system boot  2.6.82 -- 386.      Mon Mar 13 21:23          (00:43)   
zhoulj   pts/0        10.140.0.105     Sun Mar 12 22:51 - down   (00:00)   
zhoulj   pts/0        10.140.0.105     Sun Mar 12 22:51 - 22:51  (00:00)   
root     tty1                          Sun Mar 12 22:50 - down   (00:01)   
root     tty1                          Sun Mar 12 22:46 - 22:48  (00:02)   
root     tty1                          Sun Mar 12 22:43 - 22:46  (00:02)   
reboot   system boot  2.6.82 -- 386.      Mon Mar 13 06:34          (7 -:- 41)   
wtmp begins Mon Mar 13 06:34:11 2006
Copy the code

4.5 Date Command; Function: System date setting

Date Displays the current date and time date-s 20:30:30Set the system time to20:30:30
date -s 2002- 3- 5Set the system period to2003- 3- 5
date -s "060520 06:00:00"Set the system period to2006years5month20day6O 'clockCopy the code

4.6 clock command; Function: Set the clock

Clock -rRead the time parameter clock -w in the system Bios. Write the system time (such as the time set by date) into the BiosCopy the code

4.7 Running the uname command; Run the following command to view the system version

uname -aThe operating system kernel version eg: Linux Sarge is displayed2.6.82 -- 386. #1 Tue Aug 16 12:46:35 UTC 2005 i686 GNU/Linux
Copy the code

4.8 Commands for Stopping and Restarting the System

Reboot Restarts the computer shutdown-rNow Restart the computer, stop the service and restart the computer shutdown-hNow Shut down the computer, stop service and then shut down the system halt Shut down the computerCopy the code

4.9 Su command; Function: Switch the current user identity to another user identity, you must enter the user account and password to change note: – critical, use – will use the user’s environment variables

Su - Switch to user root su - zhoulj Switch to user zhoulj,Copy the code

5. Monitor system status command

5.1 Running the top command; Run the following command to view the CPU and memory usage

top
Copy the code

5.2 Free Command; Run the following command to view memory and swap usage

free -tm
eg:
              total        used        free      shared  buff/cache   available
Mem:          31980        8478         288       11681       23212       11185
Swap:         20479         194       20285
Copy the code

5.3 Running the uptime Command; Functions: current time, elapsed time since system startup, number of connected users, system load of last 1 minute, 5 minutes and 15 minutes

uptime
eg:
23:27:33 up 123 days, 10:25.3 users,  load average: 1.13.1.10.1.19
Copy the code

5.4 Vmstat Command; Function: Monitor virtual memory usage

vmstat 
eg
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
13  0 204032 325884      0 23746456    0    0     0     1    0    0  1  1 98  0  0
Copy the code

5.5 Ps Command; Run the following command to display process information

psUx displays the processes of the current userpsUxwww displays detailed information about the current user's processpsAux displays processes for all userspsEf Displays information about all processes in the systemCopy the code

5.6 Kill Command; Run the ps command to kill a process

kill 9 - 1001Number the process as1001Program to killkill all 9 -Apache kills all programs named apapche,killIt's not a panacea, and it won't work against dead programs.Copy the code

6. Commands related to users and groups

6.1 Groupadd Command; Function: Add a group

Groupadd test1 Adds groupadd for group test1-g 1111Test2 Add group test2 whose ID is1111
Copy the code

6.2 Useradd Command; Function: Add a user

Useradd user1 Add user user1, home is /home/user1, and group is user1 useradd-g test1 -m -d/home/test1 test1 Add user test1, home is /home/test1, group is test1 user list Displays the list of logged in usersCopy the code

6.3 The passwd command; Run the following command to change a user password

Passwd user1 Changes the password of user1 passwd-dRoot Deletes the password of user rootCopy the code

6.4 The userdel command; Run the following command to delete a user

Userdel user1 Deletes user user1Copy the code

6.5 Chown Command; Change the owner of a file or directory

chown user1 /dirWill /dirThe directory is set to user1 for all chown-R user1.user1 /dirWill /dirSet all files and directories under the directory to user1 all and group to user1.-RRecurse to each of the following files and directoriesCopy the code

6.6 Running the CHGRP Command; Function: Change all groups of a file or directory

chgrp user1 /dirWill /dirThe directory is set to be owned by user1Copy the code

6.7 The chmod command; Function: Change the user’s permission

Chmod a+x file Sets file to executable. The script class file must be set in this way, otherwise you will need bash file to execute chmodAwesome!File Sets file file to read and write chmod750File Sets file file to: the owner has full permission and the group can read and execute the fileCopy the code

6.8 ID command; Run the following command to display user information, including uid and GID

 id zhoulj
 eg:
 uid=500(zhoulj) gid=500(zhoulj) groups=500(zhoulj)
Copy the code

6.9 Finger Command; Function: Display the information used

Note: Debian does not have this command

finger zhoulj
eg:
Login: zhoulj                           Name:
Directory: /home/zhoulj                 Shell: /bin/bash
On since Sun May 21 07:59 (CST) on pts/0 from 192.168.1.4
No mail.
No Plan.
Copy the code

7. Disk operation commands

7.1 The df command; Run the following command to check the disk space usage of the file system

parameter-aList all directories-TaLists all directories and displays file types-BDisplay block information-iList all directories as the I node-hDisplay according to daily habits (e.g.1K,100M,20G)-x [filesystype] Do not display [filesystype]

df -Th
eg:
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda3      xfs       259G   53G  206G  21% /
devtmpfs       devtmpfs   16G     0   16G   0% /dev
tmpfs          tmpfs      16G     0   16G   0% /dev/shm
tmpfs          tmpfs      16G  1.6G   15G  10% /run
tmpfs          tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/sda1      xfs       497M  169M  329M  34% /boot
tmpfs          tmpfs     3.2G   56K  3.2G   1% /run/user/1000
tmpfs          tmpfs     3.2G     0  3.2G   0% /run/user/1002
tmpfs          tmpfs     3.2G     0  3.2G   0% /run/user/0
Copy the code

7.2 DU Command; Function: Detects the disk space occupied by a directory and (recursively) all files in its subdirectories

-s [dirName] Displays the total space occupied by the directory-sk [dirName] Displays the total space occupied by a directory, in k-sb [dirName] Displays the total space occupied by a directory, in unit B-sm [dirName] Displays the total space occupied by a directory, expressed in M-sc [dirName] displays the total space occupied by directories, plus directory statistics-sh [dirName] Statistics only directory size eg: du-sh views
# 192K views
Copy the code

7.3 Mount Command; What it does: You can mount various file systems in Linux using the mount command

Format:mount -tDevice name Mount Point (1) mount /dev/sda1  /mnt/filetest
	mount -t vfat /dev/hda  /mnt/fatfile
	mount -t ntfs /dev/hda  /mnt/ntfsfile
	mount -t iso9660 /dev/cdrom  /mnt/cdrom
	mount -oDevice name Mount Point (2) Using a USB device modProbe USB-storage
	mkdir /mnt/usb
	mount -t auto /dev/sdx1 /mnt/usb
	umount /mnt/usb
Copy the code

7.4 mkswap command; Run the mkswap command to create a swap space

eg:
mkswap -c /dev/hda4
The swapoff command is used to disable the newly created swap space
swapon /dev/hda4 
Copy the code

7.5 Fdisk Command; Run the following command to partition a disk

Fdisk /dev/xxx Formats the XXX device (XXX indicates the name of the disk drive, for example, HDB or SDC) fdisk-lDisplays the partition table of the diskCopy the code

7.6 MKFS command; Run the following command to format a file system: Specify the type of a file system, such as ext2, ext3, FAT, or NTFS

-bThe block size-iNode uppercase-mReserved management space size format1: mkfs.ext3 options /dev/xxx format2: MKFS-t ext2 options /dev/xxx
eg:
mkfs.ext3 /dev/sdb1
Copy the code

7.7 The e2fsck command; Run: Disk detection

E2fsck /dev/hda1 Check whether /dev/hda1 has a file system error, prompting the repair method e2fsck-p/dev/hda1 Check whether /dev/hda1 has an error. If yes, e2fsck is automatically repaired-y/dev/hda1 check error, all questions are executed in yes mode e2fsck-c/dev/hda1 Check whether the disk has bad areasCopy the code

7.8 The tune2fs command; Run the following command to adjust ext2/ext3 file parameters

-lView file system information-cSet the mount times for the mandatory self-check-iSet the mandatory self-check interval (unit: day)-mPercentage of blocks reserved-jConvert an ext2 file system to the ext3 format eg: Tune2fs-l /dev/sda1
Copy the code

7.9 DD Commands; Function: Copy the specified input file to the specified output file, and format conversion can be performed during the copy process

dd if= /dev/fd0 of=floppy. Img Copies the contents of the floppy disk into a mirror DDif= floppy. Img of=/dev/fd0 Copies the contents of an image to a floppy disk.Copy the code

8. Network commands

8.1 The ifconfig Command; Run the following command to display nic information

Ifconfig Displays network information ifconfig eth0 Displays network information for eth0192.168.1.1 netmask 255.255.255.0Set the nic1The address of the192.168.1.1, mask255.255.255.0
ifconfig eth0:1 192.168.1.2Bundled nic1The second address of192.168.1.2
ifconfig eth0:x 192.168.1N Bind nics1The NTH address of is192.168.1.n
Copy the code

8.2 Route command; Run the following command to display the current route Settings

Route Displays the current route Settings. route add-net 10.0.0.0 netmask 255.255.0.0 gw 192.168.1.254Add a static route routedel -net 10.0.0.0 netmask 255.255.0.0 gw 192.168.1.254Add the static route route add default gw192.168.1.1Metric1 set192.168.1.1Is the default route routedelDefault Deletes the default route. Eg:# route add-net 10.0.0.0 gw 192.168.1.254

# netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
10.0.0.0        192.168.1.254   255.255.0.0     UG        0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
0.0.0.0         192.168.1.254   0.0.0.0         UG        0 0          0 eth0

# route del-net 10.0.0.0 gw 192.168.1.254

# netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
0.0.0.0         192.168.1.254   0.0.0.0         UG        0 0          0 eth0
Copy the code

8.3 Running the netstat command; Run the following command to display network status

netstat -anExample View the network port information netstat-nrView routing table information faster than route netstat-ntlp|grep 7007To view7007Port informationCopy the code

8.4 Commands for Starting the Network Run the following command to display network status

D /network Debian command: /etc/init.d/networking eg: /etc/init.d/network stop Stop the networkstartStart the networkCopy the code

8.5 Network Troubleshooting Errors

(1Ping command (2) traceroute command; Function: Traceroute Traceroute207.68.173.7
(3) the nslookup command; Function: Resolve domain name errorsCopy the code

9. Other commands

9.1 SSH Commands; Function: Remotely log in to another UNIX host

ssh -l user1 192.168.1.2Log in as user1192.168.1.2
Copy the code

9.2 SCP Command; Function: Secure copy

SCP abc.tar.gz Copy the local abc.tar.gz file to192.168.1.5Under the root of user user1 (/home/user1).Copy the code

9.3 Telnet Commands; Function: Log in to the remote host

eg:
telnet 192.168.1.5
Copy the code