2018-10-10

2018-10-16  本文已影响0人  LLaw熊猫

Linux文件管理命令分别有cd,mkdir,rmdir,ls,tree,pwd命令等


cd:change directory 改变目录

  1. cd:切换家目录
  2. cd ~:切换回家目录
  3. cd ~USERNAME:切换至用户家目录
  4. cd -:在上一次所在目录来回切换
[root@centos-7 ~]# cd /etc
[root@centos-7 etc]# cd /mnt
[root@centos-7 mnt]# 

mkdir:make directory 创建目录

mkdir   [OPTION]... DIRECTORY...
    -p:递归创建目录
    -v:显示详细过程

[root@centos-7 ~]# mkdir -pv /test/adc

mkdir: created directory ‘/test’

mkdir: created directory ‘/test/adc’

[root@centos-7 ~]#

rmdir:删除空目录

rmdir  [OPTION]... DIRECTORY...
   -p:递归删除目录后,如果父目录为空一并删除
   -v:显示过程

[root@centos-7 ~]# rmdir -pv test/adc
rmdir: removing directory, ‘test/adc’
rmdir: removing directory, ‘test’

ls:list 列出指定目录下内容

list  [OPTION]... DIRECTORY...
    -a:显示所有文件,包括隐藏文件
    -A:显示除了.和..以外的所有文件
    -l:显示文件的长格式及详细信息
    -d:只显示所输入目录的信息
    -h:对文件进行单位换算,便于阅读
    -r:逆序排序
    -R:顺序排序

[root@centos-7 ~]# ls -al /tmp
total 3316
drwxrwxrwt. 13 root      root        4096 Oct 11 12:48 .
dr-xr-xr-x. 22 root      root        4096 Oct 10 17:51 ..
drwx------.  2 parallels parallels     20 Aug 15 16:12 .esd-1000
drwxrwxrwt.  2 root      root           6 Aug 10  2017 .font-unix
drwxrwxrwt.  2 root      root          19 Aug 15 16:12 .ICE-unix
drwx------.  2 parallels parallels     25 Aug 15 16:12 ssh-rVNvQRdodWcE
drwx------.  3 root      root          17 Aug 15 15:02 systemd-private-28849c5ee0754865bc31081fbfeaa277-colord.service-qPaXEk
drwx------.  3 root      root          17 Aug 15 15:02 systemd-private-28849c5ee0754865bc31081fbfeaa277-cups.service-INfa1j
drwx------.  3 root      root          17 Aug 22 11:39 systemd-private-28849c5ee0754865bc31081fbfeaa277-mariadb.service-pX1r5g
drwx------.  3 root      root          17 Aug 15 15:02 systemd-private-28849c5ee0754865bc31081fbfeaa277-rtkit-daemon.service-SEwPQJ
drwxrwxrwt.  2 root      root           6 Aug 10  2017 .Test-unix
-r--r--r--.  1 root      root          11 Aug 15 15:02 .X0-lock
drwxrwxrwt.  2 root      root          16 Aug 15 15:02 .X11-unix
drwxrwxrwt.  2 root      root           6 Aug 10  2017 .XIM-unix
-rw-------.  1 root      root      480849 Oct  1 17:22 yum_save_tx.2018-10-01.17-22.aT7rtE.yumtx
-rw-------.  1 root      root      480849 Oct  2 10:28 yum_save_tx.2018-10-02.10-28.rFIIvH.yumtx
-rw-------.  1 root      root      480849 Oct  5 09:54 yum_save_tx.2018-10-05.09-54.BbLaQV.yumtx
-rw-------.  1 root      root      480849 Oct  6 11:08 yum_save_tx.2018-10-06.11-08.KL5C88.yumtx
-rw-------.  1 root      root      480849 Oct  7 12:03 yum_save_tx.2018-10-07.12-03.pi_qvg.yumtx
-rw-------.  1 root      root      480849 Oct 10 15:59 yum_save_tx.2018-10-10.15-59.XZGosY.yumtx
-rw-------.  1 root      root      480849 Oct 11 06:51 yum_save_tx.2018-10-11.06-51._fwJex.yumtx

tree:树状显示目录结构,需提前安装

tree...DIRECTORY...
    -L:显示层数

[root@centos-7 ~]# tree -L 1 /tmp
/tmp
├── ssh-rVNvQRdodWcE
├── systemd-private-28849c5ee0754865bc31081fbfeaa277-colord.service-qPaXEk
├── systemd-private-28849c5ee0754865bc31081fbfeaa277-cups.service-INfa1j
├── systemd-private-28849c5ee0754865bc31081fbfeaa277-mariadb.service-pX1r5g
├── systemd-private-28849c5ee0754865bc31081fbfeaa277-rtkit-daemon.service-SEwPQJ
├── yum_save_tx.2018-10-01.17-22.aT7rtE.yumtx
├── yum_save_tx.2018-10-02.10-28.rFIIvH.yumtx
├── yum_save_tx.2018-10-05.09-54.BbLaQV.yumtx
├── yum_save_tx.2018-10-06.11-08.KL5C88.yumtx
├── yum_save_tx.2018-10-07.12-03.pi_qvg.yumtx
├── yum_save_tx.2018-10-10.15-59.XZGosY.yumtx
└── yum_save_tx.2018-10-11.06-51._fwJex.yumtx

pwd:显示当前文件位置

[root@centos-7 ~]# pwd
/root

文件查看类命令:cat,tac,head,tail,more,less

cat:文本文件查看工具

cat [OPTION]...[FIEL]...
     -n:显示行号

[root@centos-7 ~]# cat -n /etc/passwd
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync

tac:和cat相反倒叙显示


head:查看文件前n行

head [OPTION]... FILE
    -n:查看前n行信息

[root@centos-7 ~]#  head -n 5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

tail:查看文件后n行

tail [OPTION]...FILE
    -n:查看文件后n行

[root@centos-7 ~]#  tail -n 3 /etc/passwd
nologin:x:1007:1011::/sbin/nologin:/sbin/nologin
123:x:1008:1012::/home/123:/bin/bash
1234:x:1009:1013::/home/1234:/bin/bash

more:查看文件翻屏后退出

h:获取帮助信息
space:向下翻
b:想上翻
q:退出


less:和more功能类似

但是不会自动退出其余和more一样

文件管理工具:mv,cp,rm

cp:COPY 复制文件

copy [OPTION] ...SOURCE...DEST
     -i:交互式复制
     -r:递归复制
     -f:强制复制
     -d:保留符号链接文件,并指向源文件
如果SOURCE是目录,则需要添加-r,否侧报错


mv:剪切或重命名

mv [OPTION] ...SOURCE...DEST
     -i:交互式复制
     -r:递归式复制
     -f:强制复制
如果SOURCE是目录,则DEST对应为目录


rm:删除 使用命令和cp mv 类似


bash执行命令执行状态返回值演示

[root@centos-7 a]# ls -al >/dev/null
[root@centos-7 a]# echo $?
0
[root@centos-7 a]# ls -al a>/dev/null
ls: cannot access a: No such file or directory
[root@centos-7 a]# echo $?           
2

命令执行成功则为0 不成功为1-255


创建/tmp下创建a_c,a_d,b_c,b_d文件

[root@centos-7 tmp]# mkdir -pv {a,b}_{c,d}
mkdir: created directory ‘a_c’
mkdir: created directory ‘a_d’
mkdir: created directory ‘b_c’
mkdir: created directory ‘b_d’

(2)、创建/tmp/mylinux目录下的:
mylinux/
├── bin
├── boot
│ └── grub
├── dev
├── etc
│ ├── rc.d
│ │ └── init.d
│ └── sysconfig
│ └── network-scripts
├── lib
│ └── modules
├── lib64
├── proc
├── sbin
├── sys

[root@centos-7 ~]# mkdir -pv  /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig,network-scripts},lib/modules,lib64,proc,sbin,sys}
mkdir: created directory ‘/tmp/mylinux’
mkdir: created directory ‘/tmp/mylinux/bin’
mkdir: created directory ‘/tmp/mylinux/boot’
mkdir: created directory ‘/tmp/mylinux/boot/grub’
mkdir: created directory ‘/tmp/mylinux/dev’
mkdir: created directory ‘/tmp/mylinux/etc’
mkdir: created directory ‘/tmp/mylinux/etc/rc.d’
mkdir: created directory ‘/tmp/mylinux/etc/rc.d/init.d’
mkdir: created directory ‘/tmp/mylinux/etc/sysconfig’
mkdir: created directory ‘/tmp/mylinux/etc/network-scripts’
mkdir: created directory ‘/tmp/mylinux/lib’
mkdir: created directory ‘/tmp/mylinux/lib/modules’
mkdir: created directory ‘/tmp/mylinux/lib64’
mkdir: created directory ‘/tmp/mylinux/proc’
mkdir: created directory ‘/tmp/mylinux/sbin’
mkdir: created directory ‘/tmp/mylinux/sys’

查看文件的元数据信息及修改文件的时间戳

[root@centos-7 ~]# stat /etc/passwd
  File: ‘/etc/passwd’
  Size: 2663            Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 69050443    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:passwd_file_t:s0
Access: 2018-10-12 15:54:03.889000000 +0800
Modify: 2018-09-28 16:54:11.362000000 +0800
Change: 2018-09-28 16:54:11.362000000 +0800
 Birth: -

file:文件名
size:文件大小
blocks:所占用的扇区个数
IO block:每个数据块的大小
regular file:常规文件
indoe:文件indoe号
links:硬链接数
access:登录权限
UID:属主
GID:属组
access:最近访问时间
modify:最近修改时间
change:元数据修改时间

touch:创建文件或修改时间戳

touch...[OPTION]...file
     -a:修改atime
     -c:不创建新文件
     -m:修改mitme

[root@centos-7 test]# stat ll    
  File: ‘ll’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 34543470    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2018-10-16 16:42:27.968000000 +0800
Modify: 2018-10-16 16:42:05.761000000 +0800
Change: 2018-10-16 16:42:27.968000000 +0800
 Birth: -
[root@centos-7 test]# touch -a ll 
[root@centos-7 test]# stat ll     
  File: ‘ll’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 34543470    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2018-10-16 16:47:29.406000000 +0800
Modify: 2018-10-16 16:42:05.761000000 +0800
Change: 2018-10-16 16:47:29.406000000 +0800
 Birth: -

如何定义别名

[root@centos-7 test]# alias ifconfig='ifconfig eth0'
[root@centos-7 test]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.211.55.3  netmask 255.255.255.0  broadcast 10.211.55.255
        inet6 fdb2:2c26:f4e4:0:d5a2:332c:f355:9ca0  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::d41a:a6de:73c6:70fd  prefixlen 64  scopeid 0x20<link>
        ether 00:1c:42:c0:f8:2a  txqueuelen 1000  (Ethernet)
        RX packets 953926  bytes 1156754137 (1.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 399165  bytes 36309979 (34.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
alias NAME=' '  定义别名
unalias NAME=' ' 去除别名
需要永久修改需要在用户目录 .bashrc 或/etc/bashrc
--------------------------------------------
[root@centos-7 test]# ls 
ll
[root@centos-7 test]# echo $(ls)
ll
[root@centos-7 test]# mkdir $(ls)y
[root@centos-7 test]# ls
ll  lly
需要引用其他命令执行结果为$(COMMAND)或者反引号

练习

1.显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数字(可以有其他字符)的文件或目录

[root@centos-7 var]# ls -d /var/l*[0-9]*[a-z]
/var/l123a  /var/l234a

2.显示/etc目录下,以任意一个数字开头且非数字结尾的文件和目录

[root@centos-7 var]# ls -d /etc/[0-9]*[^0-9]
ls: cannot access /etc/[0-9]*[^0-9]: No such file or directory

3.显示/etc目录下,以非字母开头,后面跟了一个字母及其他任意长度任意字符额文件或目录

[root@centos-7 etc]# ls -d /etc/[^[:alpha:]]*[[:alpha:]]
ls: cannot access /etc/[^[:alpha:]]*[[:alpha:]]: No such file or directory

4.在/tmp目录下创建以tfile开头,后面跟当前日期和时间的文件

[root@centos-7 tmp]# mkdir -pv tfile-$(date +%Y-%M-%d-%H-%M-%S)
mkdir: created directory ‘tfile-2018-05-16-18-05-56’

5.复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中

[root@centos-7 ~]# mkdir /tmp/mytest1
[root@centos-7 ~]# cp -r /etc/p*[^0-9] /tmp/mytest1/

6.复制/etc下所有以l或m或n开头,以.conf结尾的文件/tmp/mytest3目录中

[root@centos-7 ~]# mkdir /tmp/mytest3
[root@centos-7 ~]# cp -r /etc/{l,m,n}*.conf /tmp/mytest3
上一篇下一篇

猜你喜欢

热点阅读