2

In a Django project, I've run a python manage.py compilescss command which has generated a bunch of untracked CSS files:

enter image description here

I would like to delete all untracked files ending with *css with a single command. From Git: list only "untracked" files (also, custom commands), I've so far found that the command to list all untracked files is

git ls-files --others --exclude-standard

However, although it seems there is an -x (or --exclude) option to exclude files matching a certain pattern, there is no equivalent --include option to which I could pass *css.

Is there perhaps a generic Bash way to filter down these results to CSS files and then mass-delete them?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526

2 Answers2

1

I managed to do this by piping the result to grep (with a regular expression argument) and xargs rm:

git ls-files --others --exclude-standard | grep -E "\.css$" | xargs rm

After running this command, the untracked CSS files have been removed:

enter image description here

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
1

git clean is a very useful command to get rid of generated files. If the files are already ignored by Git, add the -x flag to include ignored files. Typically CSS files are contained in a very specific sub-tree of the project, and you can run git clean -x path/to/Django/root/*/static.

The --dry-run flag lists files to be deleted, and --force actually deletes them.

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • In my case, the CSS files are not contained in a single directory but are in the `static` directories of various Django apps, so I do need to match a pattern. It occurred to me that I could use the `--exclude` option if I can pass in a pattern meaning 'not ending with .css'; I tried `git clean --exclude '(?!.+\.css)'` but this didn't work. – Kurt Peek Apr 12 '18 at 17:50
  • Following up on my previous comment, I needed to pass in either `--dry-run` or `--force` as an additional argument to make it run. It seems, though, like the `pattern` expected needs to be a POSIX regular expression, which does not support lookaheads or lookbehinds (according to https://stackoverflow.com/questions/15377469/posix-regular-expressions-excluding-a-word-in-an-expression). – Kurt Peek Apr 12 '18 at 17:57
  • You can specify several directories - I've amended to show this. – l0b0 Apr 12 '18 at 20:42
  • According to https://docs.djangoproject.com/en/2.0/howto/static-files/, "static files should be stored in a folder called `static` in your app". So for a project with multiple apps, CSS files are in multiple folders. (After running the [`collectstatic`](https://docs.djangoproject.com/en/2.0/ref/contrib/staticfiles/#collectstatic) command, they are also collected to the `STATIC_ROOT`, but they still remain in their app directories as well). – Kurt Peek Apr 16 '18 at 17:58