用户及文件系统管理命令

1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。
  Linux上的文件管理类命令都有增,删,查看,复制,移动等命令
新增文件最常见的是用touch命令
用法 : touch [OPTION]… FILE…
如果文件不存在,且没有用 -c 或-h 选项,则创建一个空文件,
[root@chiji51 ~]# ls
anaconda-ks.cfg Desktop Downloads Music Public Videos
a.txt Documents initial-setup-ks.cfg Pictures Templates
[root@chiji51 ~]# touch b.txt
[root@chiji51 ~]# ls -l b.txt
-rw-r–r--. 1 root root 0 May 12 19:43 b.txt
删除文件最常见的是用rm rm是remove的简写
用法 : rm [OPTION]… FILE…
-f, --force 强制删除文件和文件夹 [root@chiji51 ~]# rm -f desktop
-i 在删除之前提示是否删除 [root@chiji51 ~]# rm -i a.txt
rm: remove regular empty file ‘a.txt’? y
-r, -R 递归删除目录和目录里面的内容
[root@chiji51 ~]# mkdir -p a/c
[root@chiji51 ~]# rm -r a
rm: descend into directory ‘a’? y
rm: remove directory ‘a/c’? y
rm: remove directory ‘a’? y
-d, --dir 删除空目录
[root@chiji51 ~]# mkdir a
[root@chiji51 ~]# rm -d a
rm: remove directory ‘a’? y
[root@chiji51 ~]#
-v, --verbose 显示删除详细信息
[root@chiji51 ~]# rm -v b.txt
rm: remove regular empty file ‘b.txt’? y
removed ‘b.txt’
查看文件有两种
1 查看文件属性详细信息有 ls
用法 :ls [OPTION]… FILE…
-a, --all 显示所有文件包含隐藏文件,.和…文件
[root@chiji51 ~]# ls -a
. … .bashrc Desktop initial-setup-ks.cfg .tcshrc
anaconda-ks.cfg .cache Documents .local Templates
.bash_history .config Downloads Music Videos
.bash_logout .cshrc .esd_auth Pictures .viminfo
-A, --almost-all 显示所有文件包含隐藏文件,除了.和…文件
[root@chiji51 ~]# ls -A
anaconda-ks.cfg .bashrc .dbus .esd_auth Music Templates
.bash_history .cache Desktop .ICEauthority Pictures Videos
.bash_logout .config Documents initial-setup-ks.cfg Public .viminfo
.bash_profile .cshrc Downloads .local .tcshrc
-d, --directory 显示目录不显示内容
[root@chiji51 ~]# ls -dl Desktop/
drwxr-xr-x. 2 root root 14 May 12 21:23 Desktop/
[root@chiji51 ~]# ls -l Desktop/
total 0
-rw-r–r--. 1 root root 0 May 12 21:23 a
-h, --human-readable 人们可以读的文件大小通常和-l一起使用
[root@chiji51 ~]# ls -hl
-rw-------. 1 root root 1.2K May 12 10:22 anaconda-ks.cfg
-l 长格式显示
[root@chiji51 ~]# ls -l
-rw-------. 1 root root 1216 May 12 10:22 anaconda-ks.cfg
-r, --reverse 反向排序文件
[root@chiji51 ~]# ls -lr
drwxr-xr-x. 2 root root 6 Nov 6 2018 Videos
drwxr-xr-x. 2 root root 6 Nov 6 2018 Templates
drwxr-xr-x. 2 root root 6 Nov 6 2018 Public
-R, --recursive 递归显示目录和文件
[root@chiji51 ~]# ls -R
.:
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
./Desktop:
a
-s, --size 显示文件的块数
[root@chiji51 ~]# ls -sl
4 -rw-------. 1 root root 1216 May 12 10:22 anaconda-ks.cfg
-S 按文件大小排序
[root@chiji51 ~]# ls -Sl
-rw-r–r--. 1 root root 1266 Nov 6 2018 initial-setup-ks.cfg
-rw-------. 1 root root 1216 May 12 10:22 anaconda-ks.cfg
drwxr-xr-x. 2 root root 14 May 12 21:23 Desktop
-t 按修改时间排序
[root@chiji51 ~]# ls -tl
drwxr-xr-x. 2 root root 14 May 12 21:23 Desktop
-rw-------. 1 root root 1216 May 12 10:22 anaconda-ks.cfg
2查看文件内容的有cat 、tac、more、less、head、tail
[root@chiji51 ~]# cat --help
用法cat [OPTION]… [FILE]…
-A, --show-all 显示所有结束符
[root@chiji51 ~]# cat -A anaconda-ks.cfg
#version=RHEL7$
# System authorization information$
auth --enableshadow --passalgo=sha512$
-b, --number-nonblank 给所有非空白字符行编号
[root@chiji51 ~]# cat -b anaconda-ks.cfg
1 #version=RHEL7
2 # System authorization information
3 auth --enableshadow --passalgo=sha512

                  4	# Use CDROM installation media
          -n, --number          给所有行编号
                  root@chiji51 ~]# cat -n anaconda-ks.cfg 
                      1	#version=RHEL7
                      2	# System authorization information
                      3	auth --enableshadow --passalgo=sha512
                      4	
                      5	# Use CDROM installation media
           more 可以翻页查看文件,能向前后翻,一旦到底部就自动退出
                    用法more [options] file [...]
            tac   可以反向显示文件
                     tac [OPTION]... [FILE]...
          less    可以翻页查看文件,能向前后翻,到底部不会自动退出
          head   默认可以看文件前10行
               head -n  数字   [FILE]...
               head -数字   [FILE]...
             [root@chiji51 ~]# head -n 3 /etc/init.d/functions 
                # -*-Shell-script-*-
                #
                # functions	This file contains functions to be used by most or all
               [root@chiji51 ~]# head -3 /etc/init.d/functions 
               # -*-Shell-script-*-
                 #
                     # functions	This file contains functions to be used by most or all
          tail      默认可以看文件后10行
               tail  -n   数字   [FILE]...
              tail  -数字   [FILE]...
               [root@chiji51 ~]# tail -3 /etc/init.d/functions 
	                             exit $?
                             fi
                            fi
              [root@chiji51 ~]# tail -n 3 /etc/init.d/functions 
	               exit $?
                  fi
                 fi
    cp(copy)命令是用来复制文件及目录的
    用法  cp [OPTION]... [-T] SOURCE DEST
     -a  相当于-pdr参数,
     [root@chiji51 ~]# cp -a anaconda-ks.cfg ana.cfg
     [ root@chiji51 ~]# ls -l anaconda-ks.cfg ana.cfg
        -rw-------. 1 root root 1216 May 12 10:22 ana.cfg
       -rw-------. 1 root root 1216 May 12 10:22 anaconda-ks.cfg
     -d  若源文件是符号链接文件,则复制文件的属性而不是文件本身
       [root@chiji51 ~]# cp -d anaa.cfg  anc.cfg
        [root@chiji51 ~]# ls -l anaa.cfg  anc.cfg
        lrwxrwxrwx. 1 root root 15 May 12 22:18 anaa.cfg -> anaconda-ks.cfg
       lrwxrwxrwx. 1 root root 15 May 12 22:19 anc.cfg -> anaconda-ks.cfg
      -i  复制文件已经存在时,提示是否覆盖
        [root@chiji51 ~]# ls
        anaa.cfg         ana.kfg   Desktop    initial-setup-ks.cfg  Public
       ana.cfg          anca.cfg  Documents  Music                 Templates
        anaconda-ks.cfg  anc.cfg   Downloads  Pictures              Videos
        [root@chiji51 ~]# cp -i initial-setup-ks.cfg anc.cfg 
        cp: overwrite ‘anc.cfg’? y
         [root@chiji51 ~]#    
     -p 连同文件的属性一起复制过去,而非使用默认属性
      [root@chiji51 ~]# cp -p initial-setup-ks.cfg ana.cfg 
       cp: overwrite ‘ana.cfg’? y
        [root@chiji51 ~]# ls -l initial-setup-ks.cfg ana.cfg 
             -rw-r--r--. 1 root root 1266 Nov  6  2018 ana.cfg
             -rw-r--r--. 1 root root 1266 Nov  6  2018 initial-setup-ks.cfg
     -r  复制目录时,将目录及里面的内容一起复制过去
     [root@chiji51 ~]# cp -r a Desktop/
     [root@chiji51 ~]# ls -lR a Desktop/
       a:
        total 0
        drwxr-xr-x. 2 root root 6 May 12 22:32 c
      Desktop/:
       total 0
       drwxr-xr-x. 3 root root 14 May 12 22:34 a
       Desktop/a:
           total 0
        drwxr-xr-x. 2 root root 6 May 12 22:34 c
   mv(move)移动文件    
      用法     mv [OPTION]... [-T] SOURCE DEST
      -f   强制移动并覆盖文件
      [root@chiji51 ~]# mv -f aaa Downloads/aaa
      -i    若目标存在时,是否覆盖文件
       [root@chiji51 ~]# mv -i aaa Downloads/aaa
       mv: overwrite ‘Downloads/aaa’? y

2、使用命令行展开功能,创建/tmp/a1, /tmp/a2, /tmp/a1/a, /tmp/a1/b,在/tmp目录下创建目录:x_y, x_z, q_y, q_z
 mkdir -p /tmp/a{1/{a,b},2}
mkdir {x,q}_{y,z}
3、文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。
  文件的数据分两种:
一种元数据,既属性数据:metadata
一种就是数据本身:data
stat
stat命令用于显示文件的状态信息。stat命令的输出信息比ls命令的输出信息要更详细。
语法 stat(选项)(参数)
选项
-L:支持符号连接;
-f:显示文件系统状态而非文件状态;
-t:以简洁方式输出信息;
4、在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22。
 touch /tmp/tfile-date +%Y-%m-%d-%H-%M-%S
5、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。
  cp -r /etc/p*[^0-9] /tmp/mytest1/
6、创建用户tom,指定UID为5001,指定家目录为/tmp/tom, 指定shell为/bin/zsh, 指定基本组为tom,附加组为jack
 useradd jack
useradd -u 5001 -d /tmp/tom -s /bin/zsh -G jack tom
7、常用的用户以及文件管理命令有哪些,并演示命令以及用法。
groupadd、groupmod、groupdel、useradd、usermod、userdel、passwd、gpasswd、id、su等
groupadd添加用户组
[root@chiji51 ~]# groupadd funguy
[root@chiji51 ~]# tail -1 /etc/group
funguy❌1001:
groupmod修改用户组权限和信息
[root@chiji51 ~]# groupmod -g 666 funguy
[root@chiji51 ~]# tail -1 /etc/group
funguy❌666:
groupdel删除用户组
[root@chiji51 ~]# groupdel funguy
[root@chiji51 ~]# cat /etc/group |grep “funguy”
useradd添加用户
[root@chiji51 ~]# id funguy
id: funguy: no such user
[root@chiji51 ~]# useradd funguy
[root@chiji51 ~]# id funguy
uid=1001(funguy) gid=1001(funguy) groups=1001(funguy)
usermod修改用户权限和信息
[root@chiji51 ~]# id funguy
uid=1001(funguy) gid=1001(funguy) groups=1001(funguy)
[root@chiji51 ~]# usermod -u 666 funguy
[root@chiji51 ~]# id funguy
uid=666(funguy) gid=1001(funguy) groups=1001(funguy)
userdel删除用户
[root@chiji51 ~]# id funguy
uid=666(funguy) gid=1001(funguy) groups=1001(funguy)
[root@chiji51 ~]# userdel -r funguy
[root@chiji51 ~]# id funguy
id: funguy: no such user
passwd 给用户添加密码或改密码
[root@chiji51 ~]# passwd funguy
Changing password for user funguy.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
--------------------------------------------------------------------
[root@chiji51 ~]# passwd
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
gpasswd 给用户添加密码或改密码
[root@chiji51 ~]# gpasswd funguy
Changing password for group funguy.
New password:
Retype new password:
--------------------------------------------------------------------
[root@chiji51 ~]# gpasswd root
Changing password for grouproot.
New password:
Retype new password:
id查看用户信息
[root@chiji51 ~]# id root
uid=0(root) gid=0(root) groups=0(root)
su切换用户
[root@chiji51 ~]# su funguy
[funguy@chiji51 root]$
-----------------------------------------------------------------------------------------
[root@chiji51 ~]# su - funguy
Last login: Sun May 12 23:28:44 CST 2019 on pts/0
[funguy@chiji51 ~]$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值