Shell script (i)
1. Common Commands
(1) System Management
#useradd, a user group with the same user name is created by default when a new user is added
#创建用户user1 and specify a UID of 1111, the owning group is 2222 (test), and the home directory is not established
Groupadd test-g 2222
grep test/etc/group
Useradd user1-g 2222-u 1111-m
ID User1
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/12/wKiom1VyudrjuwgvAADH3my4lO4986.jpg "/>
#创建用户user2 and specify a UID of 1112, the owning group is 2222 (test), and the home directory is established
Useradd user2-g 2222-u 1112
Ls/home
ID User2
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0E/wKioL1Vyu3rjSZC8AAFidnKEZB0473.jpg "/>
#userdel, home directories are not deleted by default
#删除用户user1和user3 (but does not delete home directories), delete user2 and delete home directories
Userdel user1
Userdel User3
Userdel User2-r
Ls/home
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/12/wKiom1Vyudui426RAAB0wBXTqD4683.jpg "/>
#usermod, change user4 (1114) ID to 2224, group test to root, home directory to/home/test
Useradd user4-u 1114-g Test
Usermod-g root-u 2224-d/home/test user4
ID user4
grep user4/etc/passwd
Ll/home
Chown-r User4:root/home/test
Ll/home
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0E/wKioL1Vyu3vRN7_DAANToK76oVo088.jpg "/>
#groupadd, add group test1, specify id=2223
Groupadd test1-g 2223
grep test1/etc/group
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/12/wKiom1VyudyStq_6AABrAjZK-Ik564.jpg "/>
#groupmod, rename the group Test1 to test2,id=3333
Groupmod test1-n test2-g 3333
grep test/etc/group
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0E/wKioL1Vyu3ui2Jg7AAC5Rs9CAQ8049.jpg "/>
#groupdel, deleting a group requires confirming that the group has no other members
#删除包含成员的群组将会出错
#删除群组test和test2, the test group has a member delete error
Groupdel test2
Groupdel Test
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/12/wKiom1VyudzSaXndAABzGgK-ZPs681.jpg "/>
#passwd, change password, lock/unlock user, specify minimum/maximum password/warning change time
#更改user4密码为love, only the root user can passwd the user name
passwd User4
Enter Password two times
#锁定用户user4和user5
Passwd-l User4
#在/etc/shadow the 2nd column before adding "! "Indicates that the user is locked
grep User4/etc/shadow
#解锁用户user4
Passwd-u User4
grep User4/etc/shadow
#指定user4密码最短更改时间2天, up to 7 days, 3 days before expiry warning
passwd user4-n 2-x 7-w 3
grep User4/etc/shadow
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0E/wKioL1Vyu3zxey_sAAJo-o0w3gk003.jpg "/>
#chgrp, change the file or directory group, changing the mount.sh group from root to test
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/12/wKiom1Vyud3DD8CLAAGh6na92rY690.jpg "/>
(2) file directory management
#ls,-L is displayed in long format and 1-7 columns are:
#文件类型和权限, number of links, file owner, file group, file size, last modified time, file name
#ls, displays all files in the specified directory, contains hidden files, size in M, and sorted by modified time
LS ALC block-size=m/home
#不足1M以1M算, less than 1G is calculated in 1G
The difference between #与-A and-A is that the former does not show "." (current directory) and ".." (Top level directory)
LS ALC block-size=g/home
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0E/wKioL1Vyu33zEIrkAAHY2OuKKh0650.jpg "/>
#ls默认以1行显示所有文件和目录, but does not contain subdirectories
Ls/home
#前者1行显示完, which takes 1 lines per file or directory
Ls-1/Home
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/12/wKiom1Vyud7Shj6sAAB5ONVETjw163.jpg "/>
#在目录后加反斜杠
Ls-p/Home
Ls-lp/home
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0E/wKioL1Vyu32RtegDAAEQCBCYtdk457.jpg "/>
#递归式显示, the UID and GID are replaced by numeric values and sorted alphabetically
#-r: Recursive display,-n:uid,gid value display,-R: Alphabetical order
Ls-r-n-r-al/home
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/12/wKiom1Vyud7RzEO9AAL9w_GzLsQ774.jpg "/>
#cp, the directory cannot be copied by default without parameters
#-a: Preserve links, file attributes, and recursively replicate directories
#-d: Reserved link,-f: Overwrite destination file and do not prompt,-P: With copy modification time and access time
#-r: Recursive copy directory, target must be directory,-L: Do not copy file, just link file
#将/Home Copy to/tmp with all attributes, and force overwrite of destination file
Cp-apf/home/tmp
Ls/tmp
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/12/wKiom1Vyud6iYjxWAADp0_5mfsk362.jpg "/>
#mv,-F: Force overwrite of target file and do not prompt, new version has default reserved permission
#将/home/aaa file is moved to/TMP and retains permissions, such as the destination file exists with the same overwrite
Ls-kl/home/aaa
Mv-f/home/aaa/tmp
Ll/tmp/aaa
#两者比较, the target file is overwritten and permissions are reserved
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0E/wKioL1Vyu36xdsjxAAEBvs8XiMA610.jpg "/>
#rm, delete files or directories
#强制删除文件或者目录, if the directory has subdirectories deleted
Rm-rf/tmp/home
Ll/tmp
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/12/wKiom1Vyud6yBjp8AADZU9EmIz8091.jpg "/>
#mkdir, set up directory,-P: If the path does not exist for some directories,-m: Set permissions
#建立目录/HOME/BBB/CCC, and specify a permission of 777
Mkdir-p-M 777/HOME/BBB/CCC
#-m Specifies the deepest directory permission, and the missing directory permission is the current user's default permission
#此处需要注意的是root的umask为0022, so the BBB permission is 755 instead of 777
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0E/wKioL1Vyu36Rk9ATAAEc6t5LAEQ148.jpg "/>
#rmdir, you can only delete empty directories,-p: Recursively delete empty directories if the parent directory is empty and delete
#加上-P, if the parent directory is not empty, prompt for errors, note the difference
Mkdir-p BBB/CCC
Mkdir-p bbb/ddd
Rmdir-p BBB/CCC
LS BBB
Rmdir-p bbb/ddd
Ls
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0E/wKioL1Vyu37BbLfCAADQp-1npFg462.jpg "/>
#stat, view file directories access, modify, and change time
#access为访问时
#modify为修改过内容时间,
#change为修改过状态时间, such as chmod, chown command changed permissions, etc.
Stat mount.sh
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/12/wKiom1Vyud-C3ENzAAC3B7ky0TM813.jpg "/>
#touch, create an empty file,-A: Modify access time only,-M: modify only the modified time
#-t: Using the specified date,-r: modifies the specified file date to the reference file date
#将访问时间改为20140102030405, modify the time to 20150102030405
Touch-a-T 201401020304.05 BBB
Touch-m-T 201501020304.05 BBB
Stat BBB
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0E/wKioL1Vyu3-RXY26AADJHz6XBHw731.jpg "/>
#再将bbb改为参考文件ccc时间
Stat CCC
Touch-r CCC BBB
Stat BBB
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/12/wKiom1VyueDjxu8eAAFELA0NCr4486.jpg "/>
#file, test file type,-B: Do not display file or directory names
File mount.sh
File-b mount.sh
File test
File-b Test
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0E/wKioL1Vyu3_zItgbAADiBJ0Y_Ek877.jpg "/>
#ln,-B: Back up before overwriting the target file, append string to backup file,-S: Specify backup string
#-s: Creating a soft link,-D: Creating a hard link
#软链接: Equivalent to shortcut, delete does not affect the original file
#硬链接: Equivalent to the original file of a "special" copy, delete the original file does not affect
#但修改内容同样指向原文件
#为bbb建立软链接, and overwrite the destination file, specify the backup string as ". BK"
Ln-s-b-s BK BBB LNB
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/13/wKiom1VyueDSnVkDAAI4ShMPmfw435.jpg "/>
#删除软链接: The original file is not affected, but writes to the original file if the content is written
Cat BBB
echo "Test" >LNB
Cat BBB
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0E/wKioL1Vyu4DDam90AADDTiaHb8k720.jpg "/>
#为bbb建立硬链接, you can see the CCC link value changed from 1 to 2
ln-d CCC Ccchard
LL CCC
#删除硬链接: After the deletion of the original file does not affect, but the original file link value minus 1
LL CCC
echo "Ccchard" >ccchard
Rm-f Ccchard
ll ccc*
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/13/wKiom1VyueDCwlq1AAEJ-1-1LxQ572.jpg "/>
#删除原文件, hard links can also be used, HARDCCC link value changed from 2 to 1
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0E/wKioL1Vyu4DRSsnVAAEsI8_mLvg289.jpg "/>
This article is from the "Love On Action" blog, please be sure to keep this source http://1055745601.blog.51cto.com/5003160/1659230
Shell script (i)