Linux command to delete all files except .git folder?

后端 未结 6 624
别跟我提以往
别跟我提以往 2020-12-15 03:46

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.

6条回答
  •  醉梦人生
    2020-12-15 04:22

    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: Had a braindead moment, didn't just copy from my terminal.

    Edit2: 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.

提交回复
热议问题