• linux查看列出目录及子目录下的所有软链接 狼人:


        在服务器上有时需要将某个目录(包括子目录)下的所的软链接列出来,方法当然是有,但有什么好的方法没?来看看下面的方法:

    1,使用ls -lR递归显示目录下的所有方便,然后过滤出以l开头的文件(以d形状是目录,以-开头是文件),此时便列出来了所有的软链接文件,效果如下:

    user@test:/opt/www/04007.cn/now_ver$ ls -lR | grep ^l
    lrwxrwxrwx 1 www www    26 Dec 30 11:21 cache -> /home/file_dir/cache
    lrwxrwxrwx 1 www www    25 Dec 30 11:21 data -> /home/file_dir/data
    lrwxrwxrwx 1 www www    25 Dec 30 11:21 logs -> /home/file_dir/logs

    2,使用find命令通过-type选项指定l参数(只显示link),效果如下:

    user@test:/opt/www/04007.cn/now_ver$ find ./ -type l
    ./app/logs
    ./app/data
    ./app/cache

        从上面的效果来看,都能把链接显示出来。但是也有点不够友好,ls方法显示的软链文件未把文件的全路径显示出来。而find虽然把文件路径显示出来了,但未显示出足够多的信息。改进一下,在find后使用-ls命令列出文件或者使用xargs ls列出文件能得到比较友好的显示结果。

    user@test:/opt/www/04007.cn/now_ver$ find . -type l -ls
     26744362      0 lrwxrwxrwx   1 deployer deployer       25 Jan  2 17:15 ./app/logs -> /home/file_dir/logs
     26744360      0 lrwxrwxrwx   1 deployer deployer       25 Jan  2 17:15 ./app/data -> /home/file_dir/data
     26744359      0 lrwxrwxrwx   1 deployer deployer       26 Jan  2 17:15 ./app/cache -> /home/file_dir/cache
    user@test:/opt/www/04007.cn/now_ver$ find ./ -type l | xargs ls -l
    lrwxrwxrwx 1 www www 26 Dec 30 11:21 ./app/cache -> /home/file_dir/cache
    lrwxrwxrwx 1 www www 25 Dec 30 11:21 ./app/data -> /home/file_dir/data
    lrwxrwxrwx 1 www www 25 Dec 30 11:21 ./app/logs -> /home/file_dir/logs

        另外如果要显示一个本身就是软链接的目录下的软链接的话,使用上面的方法还会有些问题,如下的软链接直接find显示-type l的时候就不会列出其下面的软链。而针对其它的目录这样是能有效的。这时就需要使用-L选项,但是在使用-L选项之后发现查找出来的链接不完全。这就是文档上说的如果-L生效的话,那么l过滤可能不准确,需要结合使用-xtype选项。如下:

    user@test:/opt/www/04007.cn$ file now_ver
    now_ver: symbolic link to ./version_30001
    user@test:/opt/www/04007.cn$ find now_ver -type l   
    now_ver
    user@test:/opt/www/04007.cn$ find -L now_ver -type l
    now_ver/app/logs
    user@test:/opt/www/04007.cn$ find -L now_ver -xtype l
    now_ver
    now_ver/app/logs
    now_ver/app/data
    now_ver/app/cache
    user@test:/opt/www/04007.cn$ find -L now_ver -xtype l -ls
     24381761      4 drwxrwxr-x  20 www    www        4096 Dec 30 11:21 now_ver
     24514414      0 lrwxrwxrwx   1 www    www          25 Dec 30 11:21 now_ver/app/logs -> /home/file_dir/logs
     24514416      0 lrwxrwxrwx   1 www    www          25 Dec 30 11:21 now_ver/app/data -> /home/file_dir/data
     24514412      0 lrwxrwxrwx   1 www    www          26 Dec 30 11:21 now_ver/app/cache -> /home/file_dir/cache

        find文档:symbolic link l; this is never true if the -L option or the -follow option is in effect, unless the symbolic link  is broken.  If you want to search for symbolic links when -L is in effect, use -xtype。find命令查找时,l选项有时不一定正确,比如-L选项或者-follow选项生效的时候,这时就需要使用-xtype l来过滤。不过可以注意看下我上面的软链接目录后面未带上/号,如果带上/的话也就不存在这个问题了。

  • 相关阅读:
    linux-vi/vim
    Linux性能监控-ss
    Linux性能监控-netstat
    Linux性能监控-sar
    Linux性能监控-htop
    Linux性能监控-pstree
    Linux性能监控-ps
    查看linux占用内存/CPU最多的进程
    Linux性能监控-top
    Linux-cpu-socket/core/processor
  • 原文地址:https://www.cnblogs.com/waw/p/16755140.html
Copyright © 2020-2023  润新知