最近遇到了一两个Linux文件路径获取的命令, 感觉会比较有用. 简单记录下, 应该还不全面.
以后遇到了更多的会再继续添加. 都是Linux自带的工具命令.
1: readlink
主要用来找出符号链接所指向的位置. 打印出符号链接或者是权威文件名.
$ readlink --help / man readlink
Usage: readlink [OPTION]... FILE...
Print value of a symbolic link or canonical file name
-f, --canonicalize canonicalize by following every symlink in
every component of the given name recursively;
all but the last component must exist
递归跟随给出文件名的所有符号链接以标准化,递归跟随给出文件名的所有符号链接以标准化,
-e, --canonicalize-existing canonicalize by following every symlink in
every component of the given name recursively,
all components must exist
递归跟随给出文件名的所有符号链接以标准化,递归跟随给出文件名的所有符号链接以标准化,
-m, --canonicalize-missing canonicalize by following every symlink in
every component of the given name recursively,
without requirements on components existence
递归跟随给出文件名的所有符号链接以标准化,递归跟随给出文件名的所有符号链接以标准化,
-n, --no-newline do not output the trailing delimiter
递归跟随给出文件名的所有符号链接以标准化,
-q, --quiet,
-s, --silent suppress most error messages 缩减大多数的错误消息
-v, --verbose report error messages 缩减大多数的错误消息
-z, --zero separate output with NUL rather than newline
--help display this help and exit 显示此帮助信息并退出
--version output version information and exit 显示此帮助信息并退出
常用的是readlink -f
可以递归跟随给出文件名的所有符号链接以标准化,除最后一个外所有组件必须存在。就是一直跟随符号链接,直到直到非符号链接的文件位置,限制是最后必须存在一个非符号链接的文件。
实例:
$ readlink /usr/bin/awk
/etc/alternatives/awk
$ ll /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 Sep 12 2013 /etc/alternatives/awk -> /usr/bin/mawk*
$ readlink -f /usr/bin/awk
/usr/bin/mawk
2: dirname
取一个文件存储路径
$ dirname --help
Usage: dirname [OPTION] NAME...
Output each NAME with its last non-slash component and trailing slashes
removed; if NAME contains no /'s, output '.' (meaning the current directory).
-z, --zero separate output with NUL rather than newline
--help display this help and exit
--version output version information and exit
实例:
dirname /usr/bin/ -> "/usr"
dirname dir1/str dir2/str -> "dir1" followed by "dir2"
dirname stdio.h -> "."
3: basename
取一个文件名(去掉后缀名),可以用basename。这个命令使用起来貌似有些死板。你要完整地写出 后缀名(想要剔除的部分)。
$ basename --help
Usage: basename NAME [SUFFIX]
or: basename OPTION... NAME...
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.
Mandatory arguments to long options are mandatory for short options too.
-a, --multiple support multiple arguments and treat each as a NAME
-s, --suffix=SUFFIX remove a trailing SUFFIX
-z, --zero separate output with NUL rather than newline
--help display this help and exit
--version output version information and exit
实例:
basename /usr/bin/sort -> "sort"
basename include/stdio.h .h -> "stdio"
basename -s .h include/stdio.h -> "stdio"
basename -a any/str1 any/str2 -> "str1" followed by "str2"
目测相关的命令还有realpath/readlinat/symlink/symlinkat等,不过我目前没有用到过,暂时不做介绍了.
以后了解点再补上.