Linux command to delete all files except .git folder?

11,368

Solution 1

Resetting the index is cheap, so

git rm -rf .
git clean -fxd

Then you can reset the index (with git reset) or go straight on to checking out a new branch.

Solution 2

With find and prune option.

find . -path ./.git -prune -o -exec rm -rf {} \; 2> /dev/null


Edit: For two directories .git and dist

find . -path ./.git -prune -o \( \! -path ./dist \) -exec rm -rf {} \; 2> /dev/null

Solution 3

As Crayon mentioned in the comments, the easy solution would be to just move .git out of the directory, delete everything, and then move it back in. But if you want to do it the fancy way, find has got your back:

find -not -path "./.git/*" -not -name ".git" | grep git
find -not -path "./.git/*" -not -name ".git" -delete

The first line I put in there because with find, I always want to double-check to make sure it's finding what I think it is, before running the -delete.

Edit: Added -not -name ".git", which keeps it from trying to delete the .git directory, and suppresses the errors. Depending on the order find tries to delete things, it may fail on non-empty directories.

Solution 4

One way is to use rm -rf *, which will delete all files from the folder except the dotfiles and dotfolders like .git. You can then delete the dotfiles and dotfolders one by one, so that you don't miss out on important dotfiles like .gitignore, .gitattributes later.

Another approach would be to move your .git folder out of the directory and then going back and deleting all the contents of the folder and moving the .git folder back.

mv .git/ ../
cd ..
rm -rf folder/*
mv .git/ folder/
cd folder

Solution 5

for i in `ls | grep -v ".git"` ; do rm -rf $i; done; rm .gitignore;

the additional rm at the end will remove the special .gitignore. Take that off if you do need the file.

Share:
11,368
Eric Steinborn
Author by

Eric Steinborn

I'm a web designer. I like the internet and I make things that work really well.

Updated on June 13, 2022

Comments

  • Eric Steinborn
    Eric Steinborn 4 months

    I want to delete all of the current directory's content except for the .git/ folder before I copy the new files into the branch.

    What's the linux command for that?

  • Anshul Goyal
    Anshul Goyal over 8 years
    -1 The user wants to delete all files except the .git files, while those are the same ones you seem to be deleting.
  • cincodenada
    cincodenada over 8 years
    @mu無 They used grep -v, which inverts the match. But it's still a very awkward solution, when there are much better options.
  • Anshul Goyal
    Anshul Goyal over 8 years
    @cincodenada The other problem was that this doesn't delete remaining dotfiles within the folder. Also, initially there was some error with the code which has been corrected now which was giving me -bash: syntax error near unexpected token |'`
  • cincodenada
    cincodenada over 8 years
    Try running find . -not -name .git | grep git - you'll see that you're trying to delete all the files in the .git directory, becuase they don't have ".git" in their filenames. You're looking for -path and some wildcards.
  • DannyK
    DannyK over 8 years
    that is why the command i gave to delete the files has the '-not' in front of it. i'll review my answer.
  • cincodenada
    cincodenada over 8 years
    My original comment had a typo where I omitted the -not, which I've corrected. See my corrected comment as to why you need to learn more about how the find command works.
  • Eric Steinborn
    Eric Steinborn over 8 years
    doesn't remove any folders
  • Eric Steinborn
    Eric Steinborn over 8 years
    now it works, but it gives me errors saying find: './scss': no such file or directory but still deletes everything. weird
  • Eric Steinborn
    Eric Steinborn over 8 years
    Does this require the first line to run?
  • Eric Steinborn
    Eric Steinborn over 8 years
    Answered my own question, no.
  • Eric Steinborn
    Eric Steinborn over 8 years
    PxL, If you can suppress errors that I'm getting about no such file or directory This is my answer of choice.
  • Eric Steinborn
    Eric Steinborn over 8 years
    yeah, luckily It's a git repo and I could reclone after testing this out.
  • P̲̳x͓L̳
    P̲̳x͓L̳ over 8 years
    @EricSteinborn: added stderr redirection to a null file
  • Eric Steinborn
    Eric Steinborn over 8 years
    So this works just fine, I feel that the non quoted version was more widely accepted as the correct answer.
  • Eric Steinborn
    Eric Steinborn over 8 years
    I know its a different question than the initial question, but how would you add another ignored folder? Say "dist/" and ".git"?
  • P̲̳x͓L̳
    P̲̳x͓L̳ over 8 years
    @EricSteinborn: added to the answer
  • Antonio
    Antonio over 8 years
    Hmmm... I don't think redirecting all possible errors to null is a good idea in general. Why not using the find option -delete instead of executing rm?
  • P̲̳x͓L̳
    P̲̳x͓L̳ over 8 years
    @Antonio: I agree with you regarding redirecting errors. From the docs for -delete option: Because -delete implies -depth, you cannot usefully use -prune and -delete together.
  • Antonio
    Antonio over 8 years
    @TimWolla You're right, but life would be better if those special characters could not/were not used in file names
  • cincodenada
    cincodenada over 8 years
    Nice! This is a good git-specific solution I wasn't aware of, which may well be a better one in this case.
  • cincodenada
    cincodenada over 8 years
    Detailing the difference between this answer and mine: -prune effectively limits find to the current directory level, so instead of going through the tree and deleting each file, it uses rm -r to delete each file or folder in the root directory. And as @P̲̳x͓L̳ noted, -prune precludes -delete, since -delete isn't recursive.
  • cincodenada
    cincodenada over 8 years
    The major difference between this and the accepted answer is not the quotes (I have those to protect from shells trying to prematurely expand the glob), but the -prune. It's a different way of accomplishing the same task. And as for the first line: any time you are running a find -delete, you should run it without the -delete first, to make sure you're not doing something unintended.
  • Matthew Mitchell
    Matthew Mitchell over 7 years
    I do think rm -r * is the best solution. Keeping .gitignore and other files like that can be important.
  • xRahul
    xRahul over 5 years
    This is just so convinient!
  • Daniel F
    Daniel F about 5 years
    Good answer. When replacing -path "./.git/*" with -path "*/.git/*" then it works for excluding multiple git repositories which are under one common directory. Like projects/project1/.git and projects/project2/.git then this can be run directly inside the projects/ directory.
  • Trần Việt Hoàng
    Trần Việt Hoàng over 4 years
    To reset the index, neither git reset nor git checkout worked for me. I have to use git reset --hard.
  • Andry
    Andry over 3 years
    Neat use of the -x option to bypass the gitignore
  • Derek Wright
    Derek Wright over 2 years
    Adding -d to the command will depth sort so dirs are deleted last, then you no longer need the redirect.
  • Devin Rhode
    Devin Rhode over 1 year
    Would adding git reset --hard as first command here have any benefit?