如何与find /tmp -type l -exec ls -l输出中的 $TARGET_NAME 值完全匹配?
$ find /tmp -type l -exec ls -l 2>/dev/null {} +
lrwxrwxrwx 1 root root 24 Mar 18 12:41 /tmp/test/link -> /usr/admin/Collect_tests
lrwxrwxrwx 1 root root 43 Mar 18 12:41 /tmp/test/link1 -> /usr/admin/Collect_tests/Upload.CM@.www.com
lrwxrwxrwx 1 root root 68 Mar 18 12:41 /tmp/test/link2 -> /usr/admin/Collect_tests/Upload.CM@.www.com/Upload_Shema@@@.DATA.com
lrwxrwxrwx 1 root root 100 Mar 18 12:42 /tmp/test/link3 -> /usr/admin/Collect_tests/Upload.CM@.www.com/Upload_Shema@@@.DATA.com/List.files.emails.dummy*Printed
lrwxrwxrwx 1 root root 92 Mar 18 12:42 /tmp/test/link4 -> /usr/admin/Collect_tests/Upload.CM@.www.com/Upload_Shema@@@.DATA.com/List.files@emails.dummy
值的例子
TARGET_NAME=Upload.CM@.www.com
TARGET_NAME=Upload_Shema@@@.DATA.com
TARGET_NAME=List.files.emails.dummy*Printed
目标:仅当 $TARGET_NAME 与最后一个字段中的单词完全匹配时才打印:“链接名称”和“PATH”(最后一个字段)。
示例(当我们想要精确匹配时 - 而TARGET_NAME =Upload_Shema@@@.DATA.com 然后):
结果将显示如下
/tmp/test/link2 /usr/admin/Collect_tests/Upload.CM@.www.com/Upload_Shema@@@.DATA.com
/tmp/test/link3 /usr/admin/Collect_tests/Upload.CM@.www.com/Upload_Shema@@@.DATA.com/List.files.emails.dummy*Printed
/tmp/test/link4 /usr/admin/Collect_tests/Upload.CM@.www.com/Upload_Shema@@@.DATA.com/List.files@emails.dummy
有几个条件:
1)只需要匹配最后一个字段(来自ls -l
输出)
例子
/usr/admin/Collect_tests/Upload.CM@.www.com
2) $TARGET_NAME 值应该匹配整个单词
完全匹配示例(同时TARGET_NAME=Upload.CM@.www.com):
/usr/admin/Collect_tests/Upload.CM@.www.com
非完全匹配示例:
/usr/admin/Collect_tests/Upload.CM@.www.c
3) $TARGET_NAME 左侧必须存在反斜杠(“/”),$TARGET_NAME 右侧必须找到反斜杠或字符串结尾。
4) 需要将特殊字符转义为:“/”、“ @ ”。“ * ”等
5) 代码将成为 ksh 脚本的一部分(并且可以由 Perl oneliner 或AWK或 ksh 等实现。)
例子
find /tmp -type l -exec ls -l 2>/dev/null {} + | < Perl one liner .............. >