Kurt Peek
Kurt Peek

Reputation: 57421

How to list all untracked git files matching a certain pattern (and delete them)?

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?

Upvotes: 2

Views: 1151

Answers (2)

l0b0
l0b0

Reputation: 58788

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.

Upvotes: 1

Kurt Peek
Kurt Peek

Reputation: 57421

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

Upvotes: 1

Related Questions