用于递归地将命令应用于文件系统树的命令行实用程序

fmap的Python项目详细描述


https://travis-ci.org/mbodenhamer/fmap.svg?branch=masterhttps://img.shields.io/coveralls/mbodenhamer/fmap.svghttps://readthedocs.org/projects/fmap/badge/?version=latest

用于递归地将命令应用于文件系统树的python命令行实用程序。

程序通过使用python的os.walk遍历文件系统树(如果提供了-r的值或当前工作目录)来工作。调用该程序时使用一个字符串,该字符串指定要在树中的每个目录执行的命令。在每个目录中,每个文件(和子目录,如果指定了-d)都与在命令行中指定的包含和排除模式列表相匹配。如果文件(和/或目录)与包含模式匹配,并且与任何排除模式不匹配,则将使用该文件(或目录)名称作为参数执行该命令。如果未指定包含模式,则程序将该命令应用于与任何排除模式不匹配的所有文件(或目录)。

安装

$ pip install -U fmap

用法

Usage: fmap [-h] [-p] [-v] [-d] [-l] [-b] [-z <depth>] [-x <pattern>] [-r <dir>]
     <cmd> [<pattern> [<pattern> ...]]

Recursively apply a command to a filesystem tree.

positional arguments:
  <cmd>                 The command to apply. The file to be applied may be
                        optionally specified by '{}'. If '{}' is not
                        supplied, the file will be passed in as the last
                        argument.
  <pattern>             Unix filename pattern that specifies which files to
                        apply the command to.

optional arguments:
  -h, --help            Show this help message and exit
  -p, --preview         Doesn't apply the command. Instead, prints command
                        invocations that would be performed.
  -v, --verbose         Print command invocations as they are performed.
  -d, --apply-dirs      Apply the command to directories after it is applied
                        to files at each level of the tree.
  -l, --follow-links    Follow symbolic links.
  -b, --bottom-up       Walk the tree from the bottom up. By default, the tree
                        is traversed from the top down.
  -z <depth>, --max-depth <depth>
                        Maximum recursion depth. Any negative number results
                        in unlimited recursion. Default is -1.
  -x <pattern>, --exclude <pattern>
                        Unix pattern that specifies which files to exclude
                        applying the command to.
  -r <dir>, --root <dir> Directory in which to begin the traversal. Is the
                        current directory by default.

示例

假设在/root下有一个目录结构,如下所示:

.profile
file1
file2
dir1/
    file3
    dir3/
        file4
dir2/
    file5

作为一个简单的例子,列出当前目录下的所有文件路径:

$ pwd
/root

$ fmap echo
/root/file1
/root/file2
/root/.profile
/root/dir2/file5
/root/dir1/file3
/root/dir1/dir3/file4

要排除某些模式,请使用-x选项:

$ fmap -x .profile echo
/root/file1
/root/file2
/root/dir2/file5
/root/dir1/file3
/root/dir1/dir3/file4

$ fmap -x '.*' -x file[35] echo
/root/file1
/root/file2
/root/dir1/dir3/file4

记住要引用任何通配符模式,以防止它们被shell扩展。

默认情况下,该命令应用于所有文件。但是,可以在命令之后指定包含模式:

$ fmap echo file1 file[35]
/root/file1
/root/dir2/file5
/root/dir1/file3

包含和排除模式可以组合:

$ fmap -x file[35] echo 'file*'
/root/file1
/root/file2
/root/dir1/dir3/file4

默认情况下,该命令不应用于目录。但是,通过提供-d

$ fmap -d echo
/root/file1
/root/file2
/root/.profile
/root/dir2
/root/dir1
/root/dir2/file5
/root/dir1/file3
/root/dir1/dir3
/root/dir1/dir3/file4

该命令在应用于该级别的所有适用文件之后应用于目录。

默认情况下,文件树是自上而下遍历的。要自下而上遍历树,请提供-b选项:

$ fmap -b echo
/root/dir2/file5
/root/dir1/dir3/file4
/root/dir1/file3
/root/file1
/root/file2
/root/.profile

要在执行时打印命令调用,请提供-v

$ fmap -v echo
echo /root/file1
/root/file1
echo /root/file2
/root/file2
echo /root/.profile
/root/.profile
echo /root/dir2/file5
/root/dir2/file5
echo /root/dir1/file3
/root/dir1/file3
echo /root/dir1/dir3/file4
/root/dir1/dir3/file4

要预览哪些命令调用将在不实际调用它们的情况下进行,请使用-p选项:

$ fmap -p 'rm -f' 'file*'
rm -f /root/file1
rm -f /root/file2
rm -f /root/dir2/file5
rm -f /root/dir1/file3
rm -f /root/dir1/dir3/file4

但是,使用上述命令不会实际删除任何文件。

如果命令调用包含参数或子命令,请记住引用它。您还可以使用{}指定文件路径应插入命令调用的位置:

$ fmap -v 'echo {} >> out'
echo /root/file1 >> /root/out
echo /root/file2 >> /root/out
echo /root/.profile >> /root/out
echo /root/dir2/file5 >> /root/out
echo /root/dir1/file3 >> /root/out
echo /root/dir1/dir3/file4 >> /root/out

$ cat out
/root/file1
/root/file2
/root/.profile
/root/dir2/file5
/root/dir1/file3
/root/dir1/dir3/file4

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将jaxb插件扩展与gradlejaxbplugin一起使用   java Hibernate列表<Object[]>到特定对象   java使用多态性显示arraylist的输出   java水平堆叠卡,带有一定偏移量   java错误:找不到符号方法liesInt()   java客户机/服务器文件收发中的多线程流管理   在java中可以基于访问重载方法吗?   包含空元素的java排序数组   swing Java按钮/网格布局   java BottomNavigationView getmaxitemcount   java空指针异常字符串生成器   java Xamarin升级导致“类文件版本错误52.0,应为50.0”错误   java我正在尝试打印它,而不只是对每一行进行println   Tomcat7中的java是否需要复制上下文。将xml转换为conf/Catalina/locahost以使其生效   带有注入服务的java REST端点在何处引发自定义WebServiceException?   在Java中使用GPS数据   java如何将JFreeChart ChartPanel导出到包含添加的CrosshairOverlay的图像对象?   内置Eclipse期间的Java 8堆栈溢出   java在GWT编译的JavaScript中如何表示BigDecimal