Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-31-2005, 01:03 PM   #1
LQ Newbie
Registered: May 2005
Location: sWeDen
Posts: 2

Rep: Reputation: 0
ls command .. hidden files only

Hi everyone First post here!!

Im driving myself nuts here. Im trying to list all hidden files in a directory without showing any directories. So only files. I could use ls -a but its an assignment so im supposed to use use ls .*

ive come up with

ls .* -d

but it still shows hidden directories. How can i make it show only hidden files?

Another assignement we have is to find similar commands. Ive come up with

find -maxdepth 1 -name ".*"

dir .*

echo .*

but they all show directories.. Is it even possible to list only files?
Old 05-31-2005, 01:44 PM   #2
LQ Newbie
Registered: May 2005
Location: Calgary, Alberta, Canada
Distribution: Debian (plan to use Gentoo and Slackware as well)
Posts: 5

Rep: Reputation: 0
Re: ls command .. hidden files only

I've gone through everything I've got (UNIX in a Nutshell; Linux Command Instant Ref.; scroogle search for "man ls" and "info ls") but have been unable to find any option of the ls command that will not display directories in the listing.

However, I do notice the -p option, which marks directories by appending a "/" to them. You might try the -A option to exclude the "." and ".." (current and parent directories respectively) from the listing, but I'm not sure this works on all systems

Just in case, though, if you want to count the number of files in a directory, for some reason or other, you can use the command
ls | wc -l
Good luck with the assignment! Sorry I can't be of more help
Old 05-31-2005, 03:01 PM   #3
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
Are you allowed to pipe? if so ...

ls -aAF | grep -v "/"
might be what you are looking for. However, if you only want files, then you might have to add additional filters to the end to prevent links from displaying as well ...

ls -aAF | grep -v "/" | grep -v "@"
there are also sockets and pipes and all that can be shown, but unfortunately, that is not something that I deal with, and don't really know how they are represented, though

If you aren't allowed to pipe though, I must admit that I am not able to figure it out. I was trying to use the --ignore option, but it appears that it is applied before the -F option is, so it doesn't filter out based on the / that is appended to a directory. Specifically I was trying...

ls -aAF --ignore"/"
Old 06-02-2005, 05:11 PM   #4
LQ Newbie
Registered: May 2005
Location: sWeDen
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for your help guys. Unfortunatelly non of them really works since were only allowed to use ls .* However we are allowed to pipe as much as we need.
I gave my teacher ls .* -d as well as Hobbletoe's ls -a solution as an answer. And as you say servantofgandalf its really difficult to find. Its going to be interesting to see what the answer is if there is one
Old 04-11-2010, 09:36 AM   #5
LQ Newbie
Registered: Apr 2010
Posts: 1

Rep: Reputation: 0
Smile Command for the hidden files only

Please check the following command:

ls -aF|grep "^\.$*"
Old 04-11-2010, 09:56 AM   #6
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,579

Rep: Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914Reputation: 2914
How about:
ls -al | awk '!/^d/ && $NF ~ /^\./'

Last edited by grail; 04-11-2010 at 10:00 AM.
Old 04-11-2010, 10:03 AM   #7
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Originally Posted by kkgrg View Post
Please check the following command:

ls -aF|grep "^\.$*"
What is the intended logic of this command? I read the regex as "literal . at the beginning of the line, followed by zero or more instances of end of line". This means that it simply matches every line that begins with "." (The $* does nothing.)
Old 04-12-2010, 02:30 AM   #8
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
It should work
ls -aF1 | grep '^\..\+[^\/]$'
Old 04-12-2010, 02:49 AM   #9
Senior Member
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
What about the -hide and the -I (--ignore) option of ls? Also we better go with -I cause -hide is overridden by -a and -A

Use -F to have the type of file marked through either */=>@|. Then ignore or hide everything with a / at the end. pipe this through ls again and ignore everything with a =. Pipe till satisfied.

I could not get one -I pattern to be recognised but the OP sure has some time to achive this
Old 04-12-2010, 03:06 AM   #10
Senior Member
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
If you wanna be a smartass you just give the output of ls -A and say that everything in linux is a file....
Old 04-12-2010, 04:32 AM   #11
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
This is not as easy as it should be!

'ls' -ld .[!.*]* | egrep -v "^d"

working from the command line:

ls in quotes to disable any alias in place
-l for long listing to show the permissions and directory flag
-d to disable recursive directory listing
glob expression to allow dot files but disallow . and .. entries
reverse egrep to remove all lines beginning with "d"

'ls' -pA --ignore='[^.]*'
This almost works, but still shows the directories as name/

find . -type f -name ".*" -maxdepth 1
find in the current directory
use -type f for files only
use maxdepth to restrict recursing to lower directories

Last edited by bakdong; 04-12-2010 at 04:53 AM. Reason: Added --ignore example


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
hidden files Jeebizz Slackware 4 04-22-2005 08:18 PM
home directory files gone, hidden files remain Grasshopper Linux - Security 12 04-10-2005 09:23 PM
hidden files Alwyn Linux - Newbie 2 01-27-2005 01:20 AM
Hidden Files Alien18 Linux - Newbie 4 08-12-2004 10:38 PM
M$ CD w/ Hidden files???? trey85stang Linux - Newbie 3 05-04-2004 10:00 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:58 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration