Show only hidden files (dot files) in ls alias

I’m using the command

ls -a | grep '^.'

for showing only the hidden files.
I added the line

alias hidden='ls -a | grep '^.'' # show only hidden files

to .bash_aliases file

but this does not work. It’s probably the problem with ' character.

Could you please help me write the correct alias?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Have the shell list the dot files, and tell ls not to see through directories:

ls -d .*

Method 2

Either make the inner pair of quotes double quotes:

alias hidden='ls -a | grep "^."'

Or make the outer pair of quotes double quotes:

alias hidden="ls -a | grep '^.'"

Or make all quotes double quotes and escape the inner pair:

alias hidden="ls -a | grep "^.""

Or make it a function, so you can pass some arguments when calling:

hidden() { ls -a "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="416501">[email protected]</a>" | grep '^.'; }

Method 3

ls -Ad .* #This will list all the hidden files & directories while retaining the color & formatting

OR

To create an alias of the same:

alias lh='ls -Ad .*'

OR

Same thing could be done via grep command and pipe operator; however it would loose the color and formatting:

ls -a|grep "^."

OR

Via alias:

alias lh='ls -a|grep "^."'

Method 4

You can use double quotes:

alias hidden="ls -a | grep '^.'"

or concatenate more single quoted strings

alias hidden='ls -a | grep '''^.''

or remove at all internal quotes

alias hidden='ls -a | grep ^\.'

Method 5

For the record this doesn’t seem to work with me, since ls -a prints two (sometimes more columns). I would recommend using the -1 option to make sure every file is in its own line. Something like this:

alias hidden='ls -a1 | grep "^."'

Method 6

First answer which shows an alias allowing to add a directory path as usual

Try

alias lsh='ls -al --ignore="[^.]*"'

where ls ignores all files and directories which NOT start with a .

Or

alias lsh='ls -Al --ignore="[^.]*"'

to avoid . and .. entries

Then just call

lsh /path/directory/

Method 7

Making it slightly more complicated, but avoiding parsing ls.

llsh () { find "${@:-.}/" -maxdepth 1 -type f -name ".*" -ls; }

lsh () { find "${@:-.}/" -maxdepth 1 -type f -name ".*" -print; }

The two shell function will use find to generate a list of all hidden regular files in the current directory, or in the directory given on the command line.

The llsh function will generate a “long listing” which will be only slightly more verbose than ls -l, while lsh generates a single-column listing like ls -1.

Method 8

Does your ls support -A? From man ls:

    -a, --all
          do not ignore entries starting with .

   -A, --almost-all
          do not list implied . and ..

$ ls --version
ls (GNU coreutils) 8.5
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  
Written by Richard M. Stallman and David MacKenzie.

Method 9

find . -type f -name ".*"

OR

find . -type f -name ".*" | while read file; do basename $file; done

Method 10

# add it to ~/.bashrc

    hidden() {  # lists hidden files and directories
        find . -maxdepth 1 -name '.?*' -exec ls --color -d {} ;
        }

    hiddenfiles() { # lists hidden files
        find . -maxdepth 1 -type f -name '.?*' -exec ls --color -d {} ;
        }

    hiddendirs() { # lists directories
        find . -maxdepth 1 -type d -name '.?*' -exec ls --color -d {} ;
        }

Method 11

Just simply type the below command and dont confuse with above explained stuff.

ls -A -1 -d -F .* | egrep -v '/$'

Method 12

ls -lart | sed -n "/ .[A-Za-z0-9][A-Za-z0-9]*/p"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x