Liunx 用户及组,文件相关命令

目录

【 用户及组命令】

1.添加账号:adduser 用户名(用户名不可以大写、部分版本可大写) 亦或者useradd 用户名

2.查看账号:cat /etc/passwd

3.查看组名:cat /etc/group

4.创建文件(初始创建文件权限默认是644):touch 文件名

5.修改文件权限:chmod u+x,g+x,o+x tt.c    亦或者 chmod 644 tt.c

6.修改文件所属的用户名:chown  想改的文件所属名  文件名

 7.修改文件所属组:chgrp 需要的文件所属组  文件名

8.修改文件所属的用户名和组名:chown 所属用户名:所属组名 文件名

9.查看当前登陆的用户名:whoami

10.删除账号(若组中只有该账号,组也将自动删除):deluser 用户名

11.删除组(若组中只有该所属用户,将不能删掉):delgroup 组名

 12.为用户添加附属组:usermod -G 用户名 附属组名

13.更改用户的主要组: usermod -g 组名 用户名

14.开关机

15.重启

【文件相关命令】

1.显示文件内容:cat 文件名

2.显示文件内容(前面加上行数):cat -n 文件名

3.cat结合重定向(>):cat 文件名 > 新文件名(不存在则创建)(新文件内容被覆盖)

4.从头查看文件内容

 5.从尾查看文件内容

6.动态显示文件内容

7.查找文件字符串

8.查找文件:find 路径(不写的话表示默认当前路径)  (-name可写可不写,跨路径不能写) 文件名

9. 比较文件的差异:diff

10.查询文件位置并截取

11. 文件掩码:umask        默认的是0002


 

【 用户及组命令】

1.添加账号:adduser 用户名(用户名不可以大写、部分版本可大写) 亦或者useradd 用户名

root@ubuntu:/Liunx# adduser liu
Adding user `liu' ...
Adding new group `liu' (1001) ...
Adding new user `liu' (1001) with group `liu' ...
The home directory `/home/liu' already exists.  Not copying from `/etc/skel'.
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for liu
Enter the new value, or press ENTER for the default
	Full Name []: liu
	Room Number []: 1
	Work Phone []: 1
	Home Phone []: 1
	Other []: 1
Is the information correct? [Y/n] y

设置(useradd直接创建用户成功,初始无密码,需手动设置)或修改密码:passwd 用户名

root@ubuntu:/Liunx# passwd l
New password: 
Retype new password: 
passwd: password updated successfully

2.查看账号:cat /etc/passwd

root@ubuntu:/Liunx# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
liu:x:1001:1001:liu,1,1,1,1:/home/liu:/bin/bash

 查看指定用户:cat  /etc/passwd | grep 用户名

3.查看组名:cat /etc/group

root@ubuntu:/Liunx# cat /etc/group
root:x:0:
daemon:x:1:
bin:x:2:

 查看指定组名:cat /etc/group|grep 组名

root@ubuntu:/Liunx# cat /etc/group|grep liu
liu:x:1001:

查看所有组(第一个为主要组):groups 

4.创建文件(初始创建文件权限默认是644):touch 文件名

root@ubuntu:/Liunx# touch tt.c
root@ubuntu:/Liunx# ll
total 20
drwxr-xr-x  3 root root 4096 4月  12 20:01 ./
drwxr-xr-x 22 root root 4096 4月   9 23:40 ../
-rw-r--r--  1 root root  905 4月   9 22:31 str_replace.c
drwxr-xr-x  3 root root 4096 4月   9 22:12 t1/
-rw-r--r--  1 liu  root  290 4月   9 22:26 t.c
-rw-r--r--  1 root root    0 4月  12 20:01 tt.c

5.修改文件权限:chmod u+x,g+x,o+x tt.c    亦或者 chmod 644 tt.c

root@ubuntu:/Liunx# chmod u+x,g+x,o+x tt.c
root@ubuntu:/Liunx# ll
total 20
drwxr-xr-x  3 root root 4096 4月  12 20:01 ./
drwxr-xr-x 22 root root 4096 4月   9 23:40 ../
-rw-r--r--  1 root root  905 4月   9 22:31 str_replace.c
drwxr-xr-x  3 root root 4096 4月   9 22:12 t1/
-rw-r--r--  1 liu  root  290 4月   9 22:26 t.c
-rwxr-xr-x  1 root root    0 4月  12 20:01 tt.c*
root@ubuntu:/Liunx# chmod 644 tt.c
root@ubuntu:/Liunx# ll
total 20
drwxr-xr-x  3 root root 4096 4月  12 20:01 ./
drwxr-xr-x 22 root root 4096 4月   9 23:40 ../
-rw-r--r--  1 root root  905 4月   9 22:31 str_replace.c
drwxr-xr-x  3 root root 4096 4月   9 22:12 t1/
-rw-r--r--  1 liu  root  290 4月   9 22:26 t.c
-rw-r--r--  1 root root    0 4月  12 20:01 tt.c

6.修改文件所属的用户名:chown  想改的文件所属名  文件名

root@ubuntu:/Liunx# chown liu tt.c
root@ubuntu:/Liunx# ll
total 20
drwxr-xr-x  3 root root 4096 4月  12 20:01 ./
drwxr-xr-x 22 root root 4096 4月   9 23:40 ../
-rw-r--r--  1 root root  905 4月   9 22:31 str_replace.c
drwxr-xr-x  3 root root 4096 4月   9 22:12 t1/
-rw-r--r--  1 liu  root  290 4月   9 22:26 t.c
-rw-r--r--  1 liu  root    0 4月  12 20:01 tt.c
root@ubuntu:/Liunx# chown root tt.c
root@ubuntu:/Liunx# ll
total 20
drwxr-xr-x  3 root root 4096 4月  12 20:01 ./
drwxr-xr-x 22 root root 4096 4月   9 23:40 ../
-rw-r--r--  1 root root  905 4月   9 22:31 str_replace.c
drwxr-xr-x  3 root root 4096 4月   9 22:12 t1/
-rw-r--r--  1 liu  root  290 4月   9 22:26 t.c
-rw-r--r--  1 root root    0 4月  12 20:01 tt.c

 7.修改文件所属组:chgrp 需要的文件所属组  文件名

-rw-r--r--  1 liu  liu     0 4月  12 20:01 tt.c
root@ubuntu:/Liunx# chgrp root tt.c
root@ubuntu:/Liunx# ll
total 20
drwxr-xr-x  3 root root 4096 4月  12 20:01 ./
drwxr-xr-x 22 root root 4096 4月   9 23:40 ../
-rw-r--r--  1 root root  905 4月   9 22:31 str_replace.c
drwxr-xr-x  3 root root 4096 4月   9 22:12 t1/
-rw-r--r--  1 liu  root  290 4月   9 22:26 t.c
-rw-r--r--  1 liu  root    0 4月  12 20:01 tt.c

8.修改文件所属的用户名和组名:chown 所属用户名:所属组名 文件名

-rw-r--r--  1 root root    0 4月  12 20:01 tt.c
root@ubuntu:/Liunx# chown liu:liu tt.c
root@ubuntu:/Liunx# ll
total 20
drwxr-xr-x  3 root root 4096 4月  12 20:01 ./
drwxr-xr-x 22 root root 4096 4月   9 23:40 ../
-rw-r--r--  1 root root  905 4月   9 22:31 str_replace.c
drwxr-xr-x  3 root root 4096 4月   9 22:12 t1/
-rw-r--r--  1 liu  root  290 4月   9 22:26 t.c
-rw-r--r--  1 liu  liu     0 4月  12 20:01 tt.c

9.查看当前登陆的用户名:whoami

root@ubuntu:/# whoami
root

10.删除账号(若组中只有该账号,组也将自动删除):deluser 用户名

liu:x:1001:1001:liu,1,1,1,1:/home/liu:/bin/bash
l:x:1002:1002::/home/l:/bin/sh
root@ubuntu:/Liunx# deluser l
Removing user `l' ...
Warning: group `l' has no more members.
Done.

11.删除组(若组中只有该所属用户,将不能删掉):delgroup 组名

root@ubuntu:/Liunx# delgroup liu
/usr/sbin/delgroup: `liu' still has `liu' as their primary group!

 12.为用户添加附属组:usermod -G 用户名 附属组名

root@ubuntu:/Liunx# usermod -G liu group
usermod: user 'group' does not exist
root@ubuntu:/Liunx# usermod -G group liu
root@ubuntu:/Liunx# su liu
liu@ubuntu:/Liunx$ groups
liu group

13.更改用户的主要组: usermod -g 组名 用户名

root@ubuntu:/# su l
$ groups              
group
$ exit
root@ubuntu:/# usermod -g liu l
root@ubuntu:/# su l
$ groups  
liu

14.开关机

sudo shutdown -h now 立即关机

sudo shutdown -h 19:30 表示在19:30的时候关机

sudo shutdown -h +10 表示在10分钟后关机

15.重启

sudo shutdown -r now 立即重启

sudo shutdown -r 17:30 表示在17:30的时候重启

sudo shutdown -r +10 表示在10分钟后重启

【文件相关命令】

1.显示文件内容:cat 文件名

root@ubuntu:/Liunx# cat t.c
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int a=0;

    return 0;
} 

2.显示文件内容(前面加上行数):cat -n 文件名

root@ubuntu:/Liunx# cat -n t.c
     1	/*===============================================
     2	*   文件名称:t.c
     3	*   创 建 者:     
     4	*   创建日期:2022年04月09日
     5	*   描    述:
     6	================================================*/
     7	#include <stdio.h>
     8	
     9	int main(int argc, char *argv[])
    10	{ 
    11	    int a=0;
    12	
    13	    return 0;
    14	} 

3.cat结合重定向(>):cat 文件名 > 新文件名(不存在则创建)(新文件内容被覆盖)

root@ubuntu:/Liunx# cat tt.c
khsdjjk
root@ubuntu:/Liunx# cat t.c > tt.c
root@ubuntu:/Liunx# cat tt.c
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int a=0;

    return 0;
} 

cat 文件名 >> 新文件名(不存在则创建)(在新文件内容后面追加文件内容)

root@ubuntu:/Liunx# cat tt.c
sdgdgujs
root@ubuntu:/Liunx# cat t.c >> tt.c
root@ubuntu:/Liunx# cat tt.c
sdgdgujs
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int a=0;

    return 0;
} 

4.从头查看文件内容

head:表示查看文件的内容。默认查看文件的前10行代码

查看文件的前10行代码:head 文件名 

查看文件的前num行代码:head -num 文件名

root@ubuntu:/Liunx# head t.c
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{ 
root@ubuntu:/Liunx# head -13 t.c
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int a=0;

    return 0;

 5.从尾查看文件内容

tail:表示查看文件的内容,默认查看文件的后10行代码

查看文件的后10行:tail 文件名

查看文件的后n行代码:tail -n 文件名

root@ubuntu:/Liunx# tail t.c
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int a=0;

    return 0;
} 
root@ubuntu:/Liunx# tail -12 t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int a=0;

    return 0;
} 

6.动态显示文件内容

more 从头不完全显示文件内容,enter,s,d可以向下翻页,不能向上翻页(若文件内容足够多).

less  从尾不完全显示文件内容,可以上下翻页(若文件内容足够多).

7.查找文件字符串

grep "字符串" 文件名

-n:表示显示行号                                -w:精确查找

-i:忽略大小写                                     -R:递归查找

root@ubuntu:/Liunx# grep "i" t.c
#include <stdio.h>
int main(int argc, char *argv[])
    int a=0;

root@ubuntu:/Liunx# grep -n "i" t.c
7:#include <stdio.h>
9:int main(int argc, char *argv[])
11:    int a=0;

root@ubuntu:/Liunx# grep -w "i" t.c
root@ubuntu:/Liunx# grep -w "int" t.c
int main(int argc, char *argv[])
    int a=0;

root@ubuntu:/Liunx# grep -nw "int" t.c
9:int main(int argc, char *argv[])
11:    int a=0;

root@ubuntu:/Liunx# grep -i "int" t.c
int main(int argc, char *argv[])
    int a=0;
INT INT;
    InT;

root@ubuntu:/Liunx# grep -in "int" t.c
9:int main(int argc, char *argv[])
11:    int a=0;
12:INT INT;
14:    InT;

root@ubuntu:/Liunx# vim tt.c
root@ubuntu:/Liunx# grep -inR "int" t.c
9:int main(int argc, char *argv[])
11:    int a=0;
12:INT INT;
14:    InT;

root@ubuntu:/Liunx# grep -inR "int" t.c tt.c
t.c:9:int main(int argc, char *argv[])
t.c:11:    int a=0;
t.c:12:INT INT;
t.c:14:    InT;
tt.c:10:int main(int argc, char *argv[])
tt.c:12:    int a=0;

查找以该字符串开头的行:grep "^字符串" 文件名

查找以该字符串结尾的行:grep "字符串$" 文件名

表查找只有该字符串的行:grep "^字符串$" 文件名

用转义字符查询数组(特殊字符): grep   "a\[N\]"

root@ubuntu:/Liunx# cat t.c
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int a=0;
    INT int
    INI
INT
    InT;
    int a[10]={0};
    return 0;
} 
root@ubuntu:/Liunx# grep "^int" t.c
int main(int argc, char *argv[])
root@ubuntu:/Liunx# grep "int$" t.c
    INT int
root@ubuntu:/Liunx# grep "^int$" t.c
root@ubuntu:/Liunx# grep "^INT$" t.c
INT
root@ubuntu:/Liunx# grep "a\[10]" t.c
    int a[10]={0};

8.查找文件:find 路径(不写的话表示默认当前路径)  (-name可写可不写,跨路径不能写) 文件名

root@ubuntu:/Liunx# ls
str_replace.c  t1  t.c  tt.c
root@ubuntu:/Liunx# find t.c
t.c
root@ubuntu:/Liunx# find -name t.c
./t.c
root@ubuntu:/Liunx# cd ..
root@ubuntu:/# find /Liunx/t.c
/Liunx/t.c
root@ubuntu:/# find -name /Liunx/t.c
find: warning: ‘-name’ matches against basenames only, but the given pattern contains a directory separator (‘/’), thus the expression will evaluate to false all the time.  Did you mean ‘-wholename’?
find: ‘./run/user/1000/doc’: Permission denied
find: ‘./run/user/1000/gvfs’: Permission denied

9. 比较文件的差异:diff

diff 文件名1 文件名2

root@ubuntu:/Liunx# cat -n tt.c
     1	sdgdgujs
     2	/*===============================================
     3	*   文件名称:t.c
     4	*   创 建 者:     
     5	*   创建日期:2022年04月09日
     6	*   描    述:
     7	================================================*/
     8	#include <stdio.h>
     9	
    10	int main(int argc, char *argv[])
    11	{ 
    12	    int a=0;
    13	    int b,c,d;
    14	    return 0;
    15	} 
root@ubuntu:/Liunx# cat -n t.c
     1	/*===============================================
     2	*   文件名称:t.c
     3	*   创 建 者:     
     4	*   创建日期:2022年04月09日
     5	*   描    述:
     6	================================================*/
     7	#include <stdio.h>
     8	
     9	int main(int argc, char *argv[])
    10	{ 
    11	    int a=0;
    12	    INT int
    13	    INI
    14	INT
    15	    InT;
    16	    int a[10]={0};
    17	    return 0;
    18	} 
root@ubuntu:/Liunx# diff t.c tt.c
0a1
> sdgdgujs
12,16c13
<     INT int
<     INI
< INT
<     InT;
<     int a[10]={0};
---
>     int b,c,d;
  • "|"表示前后2个文件内容有不同
  • "<"表示后面文件比前面文件少了1行内容
  • ">"表示后面文件比前面文件多了1行内容
  • "12,16c13"表示文件2比文件1多了12~16行的内容,少了13行的内容
root@ubuntu:/Liunx# diff t.c tt.c -y -w
							              >	sdgdgujs
/*===========================		    /*=========================
*   文件名称:t.c					    	*   文件名称:t.c
*   创 建 者:     					    	*   创 建 者:     
*   创建日期:2022年04月09日		       	*   创建日期:2022年04月09日
*   描    述:						    	*   描    述:
============================*/		    ===========================*/
#include <stdio.h>						#include <stdio.h>

int main(int argc, char *argv[])		int main(int argc, char *argv[])
{ 								        { 
    int a=0;						   	       int a=0;
    INT int						          |	   int b,c,d;
    INI							          <
INT							              <
    InT;						          <
    int a[10]={0};					      <
    return 0;							    return 0;
} 							        	} 

10.查询文件位置并截取

cut -d(查找的分隔符)   ,(表示以‘,’作为分隔符)  -f(表示查找的位置)   1,4(分割的位置) 文件名

    int b,c,d;
    return 0;
} 
root@ubuntu:/Liunx# cut -d , -f 1,3 tt.c
sdgdgujs
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc
{ 
    int a=0;
    int b,d;
    return 0;
} 
root@ubuntu:/Liunx# cat tt.c
sdgdgujs
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int a=0;
    int b,c,d,e,f,g;
    return 0;
} 
root@ubuntu:/Liunx# cut -d , -f 2,5 tt.c
sdgdgujs
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

 char *argv[])
{ 
    int a=0;
c,f
    return 0;
} 
root@ubuntu:/Liunx# cut -d , -f 1,5 tt.c
sdgdgujs
/*===============================================
*   文件名称:t.c
*   创 建 者:     
*   创建日期:2022年04月09日
*   描    述:
================================================*/
#include <stdio.h>

int main(int argc
{ 
    int a=0;
    int b,f
    return 0;
} 

11. 文件掩码:umask        默认的是0002

因为Liunx系统默认的权限是0022,因此我们无论创建目录(0777),还是创建文件(0666),其权限都不是最大值,目录的权限为755,文件的权限是644(已创建的文件和目录除外)。

那么问题来了,怎么让初始的文件或者目录权限不是这样呢?

LIunx有两种修改方法:①临时修改umask(系统重启失效)②永久修改(配置文件/etc/profile)

//未用    umask
root@ubuntu:/Liunx/liunx# touch t1.c
root@ubuntu:/Liunx/liunx# ll
total 8
drwxr-xr-x 2 root root 4096 4月  14 22:31 ./
drwxr-xr-x 4 root root 4096 4月  14 22:31 ../
-rw-r--r-- 1 root root    0 4月  14 22:31 t1.c
root@ubuntu:/Liunx/liunx# mkdir t1
root@ubuntu:/Liunx/liunx# ll
total 12
drwxr-xr-x 3 root root 4096 4月  14 22:31 ./
drwxr-xr-x 4 root root 4096 4月  14 22:31 ../
drwxr-xr-x 2 root root 4096 4月  14 22:31 t1/
-rw-r--r-- 1 root root    0 4月  14 22:31 t1.c
//用    umask
root@ubuntu:/Liunx/liunx# umask 0044
root@ubuntu:/Liunx/liunx# touch t2.c
root@ubuntu:/Liunx/liunx# ll t2.c
-rw--w--w- 1 root root 0 4月  14 22:33 t2.c
root@ubuntu:/Liunx/liunx# mkdir t2
root@ubuntu:/Liunx/liunx# ll
total 16
drwxr-xr-x 4 root root 4096 4月  14 22:33 ./
drwxr-xr-x 4 root root 4096 4月  14 22:31 ../
drwxr-xr-x 2 root root 4096 4月  14 22:31 t1/
-rw-r--r-- 1 root root    0 4月  14 22:31 t1.c
drwx-wx-wx 2 root root 4096 4月  14 22:33 t2/
-rw--w--w- 1 root root    0 4月  14 22:33 t2.c
//    /etc/profile文件中
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi
··· ···
··· ···
umask 027

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值