【问题标题】:How to subtract folders and files list from another one?如何从另一个中减去文件夹和文件列表?
【发布时间】:2019-01-04 14:24:50
【问题描述】:

我有一个如下的 git repo:

[root@localhost www]# tree
.
├── admin.php
├── api
    ├── index.htm
    └── remote
        └── mod
            ├── index.htm
            ├── mod_cron.php
            └── mod_index.php


3 directories, 5 files

获取 git 跟踪的文件夹和文件列表很容易。

[root@localhost www]# git ls-files >a.txt
[root@localhost www]# cat a.txt
.gitattributes
.gitignore
admin.php
api/index.htm
api/remote/mod/index.htm
api/remote/mod/mod_cron.php
api/remote/mod/mod_index.php

但是在工作目录中,有一些文件夹和文件没有被git跟踪。整个工作目录如下:

    [root@localhost www]# tree
    .
    ├── admin.php
    ├── api
    │   ├── index.htm
    │   └── remote
    │       ├── index.htm
    │       ├── index.php
    │       └── mod
    │           ├── index.htm
    │           ├── mod_cron.php
    │           └── mod_index.php
    ├── archiver
       └── index.php

4 directories, 8 files

如何获取git未跟踪的文件夹和文件列表如下:

archiver/
archiver/index.php
api/remote/index.htm
api/remote/index.php

我想在rsync -av --files-from=/path/to/not_tracked_files_lsit 中使用此列表。
提前致谢!

【问题讨论】:

  • a find 会给你一个完整的文件列表。 grep -vf a.txt 应该消除被跟踪的文件。
  • 另一种可能的选择:git status -s|sed -n '/^[^?]/d; s/^?? //; p;'
  • @PaulHodges,为什么不添加答案
  • 只是觉得有人可能会提供更好的,但会做到的。
  • git ls-files -o?

标签: bash git


【解决方案1】:

基本上是从上面的cmets中拷贝进来的……

find 将为您提供当前文件的完整列表。

find | grep -v '[.]git/' | grep -vf a.txt

应该消除回购本身和所有跟踪文件中的废话。

另一种选择,可能更好:

git status -s | sed -n '/^[^?]/d; s/^?? //; p;'

这应该做同样的事情,但希望更简洁,更切题,流水线中的进程更少。

【讨论】:

  • 如果有一个空文件夹没有被 git 跟踪,将被你的脚本忽略。
  • 仅当为空时 - 但您是对的,未跟踪的空文件夹将被忽略。
猜你喜欢
  • 2018-01-25
  • 2019-07-26
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
相关资源
最近更新 更多