I'm trying to find and delete all files that begin with . within a directory, and I know that I could do this via the command terminal but I'm VERY new to Ubuntu and don't quite know my way around commands yet. Any help would be appreciated!

  • What do you mean exactly by binary files? – Louis Matthijssen Apr 26 '14 at 23:05
  • Hidden files. Really, anything that begins with a . – JonesCode Apr 26 '14 at 23:09

Hidden files don't have to be binary files.

You can use the following command to do this:

find /path/to/start/ -maxdepth 1 -name ".*" -type f -delete

This will search for files in /path/to/start/ starting with a dot (-name ".*", * means everything) without searching in sub directories (-maxdepth 1, change 1 to search deeper) only files (-type f) and will remove all the results (-delete).

It's not always a good idea to delete these files! Please be sure that you know what you're doing before executing this. Some files are hidden for a reason.

  • For more control, put argument i to rm command. This will require confirmation for each file to be deleted. – girardengo Apr 26 '14 at 23:30
  • 2
    With GNU find, you can also use -delete. – terdon Apr 27 '14 at 11:21
  • Don't you mean find /path/to/start/dir/ -maxdepth 1 -name ".*" -type f -exec rm {} + instead? with -maxdepth 0 you won't find anything... – gniourf_gniourf Apr 27 '14 at 15:25
  • @gniourf_gniourf You're right, my bad. I`ve updated my answer with both of your suggestions and verified that it's working as it should. – Louis Matthijssen Apr 27 '14 at 15:51

You can also do this using shell globs and a for loop:

for file in .[^.]*; do rm "$file"; done

The for file in GLOB; do ... done will expand GLOB to all file names that match it, iterate through these files, sequentially saving each as $file and then run the commands between the do and done on them.

The glob .[^.]* will match all files beginning with a . and followed by a non-dot character ([^.]). The latter is needed to avoid matching . and ../.

So, for example:

$ ls -1A
.dot and spaces
$ for file in .[^.]*; do rm -v "$file"; done
removed ‘.dot and spaces’
removed ‘.dotfile1’
$ ls -1A

If you want to run this for a directory other than the one you are currently in, just add the path to the glob. For example:

for file in /home/terdon/foobar/.[^.]*; do rm "$file"; done

Of course, the loop itself is not needed as @gniourf_gniourf pointed out in the comments, and you can simply delete all the files with

rm .[^.]*
  • You're also (trying) to delete folders that start with a ., whereas the OP mentions files. (Oh, then you will argue that everything is a file in Unix...). – gniourf_gniourf Apr 27 '14 at 15:21
  • @gniourf_gniourf no, I will argue that rm has no effect on directories so whether the glob matches any or not is irrelevant. – terdon Apr 27 '14 at 16:27
  • That's why I specified (though parenthesized) trying :D. You'll get some junk on stderr though. Also, why not, simply rm /home/terdon/foobar/.[^.]*? (oh, you will argue that this might potentially exceed the max number of arguments). – gniourf_gniourf Apr 27 '14 at 16:30
  • 1
    @gniourf_gniourf yes, it might but that's indeed a good solution. I did not use it 'cause I thought that showing the loop would be useful. Added now, thanks. – terdon Apr 27 '14 at 16:36
  • Your glob .[^.]* is wrong. It won't match files/directories starting with two dots, e.g. ..hello (yes, they're possible). – Sasha Oct 12 '16 at 17:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.