I have a directory that contains thousands of files, some of them are hidden.
The command ls -a
list all files, including hidden ones, but I need just to list hidden files.
What command should I use?
I have a directory that contains thousands of files, some of them are hidden.
The command ls -a
list all files, including hidden ones, but I need just to list hidden files.
What command should I use?
The command :
Will only list hidden files .
Explain :
Does exactly what OP is looking for .
If you just want the files in your current directory (no recursion), you could do
That will print the names of all files whose name starts with a
.
and is followed by one or more non-dot characters. Note that this will fail for files whose name starts with consecutive dots, so for example....foo
will not be shown.You could also use
find
:The
-mindepth
ensures we don't match.
and the-prune
means thatfind
won't descend into subdirectories.works for me in Bash.
Using
find
andawk
,Explanation:
find . -type f
--> List all the files in the current directory along with it's path like,awk -F"/" '$NF ~ /^\..*$/ {print $NF}'
/
as field separator awk checks for the last field staring with a dot or not. If it starts with a dot, then it prints the last field of that corresponding line.find
is usually a better option for complicated searches than using name globbing.or
find .
searches current directory-mindepth 1
excludes . and .. from the list-maxdepth 1
limits the search to the current directory-name '.*'
find file names that start with a dot-o
or-name '*~'
find file names that end with a tilde (usually, these are backup files from text editing programs)However, this and all of the other answers miss files that are in the current directory's
.hidden
file. If you are writing a script, then these lines will read the.hidden
file and display the file names of those that exist.I think that you can do it with following command.
ls -a
command you entered, that shows all files and directories in current working directory.grep "^\."
command I appended, that filters output to shows only hidden files(It's name starts with"."
).grep -v "^\.$" | grep -v "^\..$"
command I appended, that filters output to exclude ., ..(They are current and parent directory).If some filenames can have more than a line with
"\n"
, above example could be incorrect.So I suggest following command to solve it issue.
Approach 1:
ls -d .{[!.],.?}*
Explain: I want to exclude
.
and..
but include file such as..i_am_also_a_hidden_file.txt
ls -d .*
undesirably shows.
and..
ls -d .?*
(the current accepted answer) undesirably shows..
ls -d .[!.]*
undesirably will not show..i_am_also_a_hidden_file.txt
ls -d .{[!.],.?}*
is the answerApproach 2:
ls -A | grep "\\."
I personally like this way better
What else you could have done,
is ls .?*
Orls .!(|)
that will show you everything in the current dir hidden files/dirs on the top and other files/dirs belowe.g: from my terminal
Now notice in the above results, it shows you every file/dir with its subdir and any hidden files right below.
Sorry, I cannot comment. to explain the difference here between
ls .?*
and @cioby23 answerls -d .[!.]* .??*
And why it is actually printing hidden files twice is because literally you're asking twice.??*
,.?*
,.[!.]*
they're the same thing, so adding any of them with different command characters will print twice.With bash, setting the
GLOBIGNORE
special variable is some non-empty value is enough to make it ignore.
and..
when expanding globs. From the Bash docs:If we set it to
.:..
, both.
and..
will be ignored. Since setting it to anything non-null will also get this behaviour, we might as well set it to just.
So: