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!
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 (
* means everything) without searching in sub directories (
-maxdepth 1, change
1 to search deeper) only files (
-type f) and will remove all the results (
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.
You can also do this using shell globs and a for loop:
for file in .[^.]*; do rm "$file"; done
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
done on them.
.[^.]* will match all files beginning with a
. and followed by a non-dot character (
[^.]). The latter is needed to avoid matching
So, for example:
$ ls -1A .dot and spaces .dotfile1 internal.dot nodot $ for file in .[^.]*; do rm -v "$file"; done removed ‘.dot and spaces’ removed ‘.dotfile1’ $ ls -1A internal.dot nodot
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