LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-30-2017, 12:01 PM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 342

Rep: Reputation: 54
Listing files in terminal


Hello

Please help me to list files in following way. Some I know, but others I don't know how to achieve.
  1. List all files and folders including hidden
  2. List only hidden files and folders
  3. List only folders (not hidden)
  4. List only folders (hidden only)
  5. List only files (not hidden) -- Don't know
  6. List only files (hidden) -- Don't know
  7. List all files (hidden and not-hidden) -- Don't know

Please help me for last three options in listing. I want to do this only through ls command. No other command or through grep or any other pipes.

1.List all files and folders including hidden
Code:
$ ls -lA
total 46856
-rw-------.  1 2dd 2dd     2707 Mar 30 20:55 .bash_history
-rw-r--r--.  1 2dd 2dd       18 Aug  2  2016 .bash_logout
-rw-r--r--.  1 2dd 2dd      193 Aug  2  2016 .bash_profile
-rw-r--r--.  1 2dd 2dd      274 Mar 30 14:05 .bashrc
drwx------. 21 2dd 2dd     4096 Mar 30 21:23 .cache
drwxr-xr-x. 25 2dd 2dd     4096 Mar 30 20:57 .config
drwxr-xr-x.  2 2dd 2dd        6 Mar 30 13:08 Desktop
-rw-r--r--.  1 2dd 2dd  1542196 Feb 24 15:20 DM Multipath.pdf
drwxr-xr-x.  2 2dd 2dd        6 Mar 30 20:12 Documents
drwxr-xr-x.  2 2dd 2dd      238 Mar 30 21:28 Downloads
drwx------.  5 2dd 2dd      164 Mar 30 20:57 .dropbox
drwx------.  8 2dd 2dd      168 Mar 30 20:57 Dropbox
drwxr-xr-x.  3 2dd 2dd       71 Mar 30 20:49 .dropbox-dist
-rw-------.  1 2dd 2dd       16 Mar 30 13:08 .esd_auth
drwxr-xr-x.  4 2dd 2dd     4096 Feb 24 18:55 .FRD
drwxr-xr-x. 24 2dd 2dd     4096 Mar 30 19:41 .gimp-2.8
drwx------.  3 2dd 2dd       18 Mar 30 15:43 .gnome
drwx------.  2 2dd 2dd       98 Mar 30 21:32 .gnupg
-rw-------.  1 2dd 2dd     2802 Mar 30 18:12 .ICEauthority
drwx------.  3 2dd 2dd       19 Mar 30 13:08 .local
-rw-r--r--.  1 2dd 2dd 13197684 Feb 24 15:20 Logical Volume Manager Administration.pdf
drwxr-xr-x.  5 2dd 2dd       54 Mar 30 13:49 .mozilla
drwxr-xr-x.  2 2dd 2dd        6 Mar 30 13:08 Music
drwxr-xr-x.  2 2dd 2dd        6 Mar 30 13:08 Pictures
drwx------.  3 2dd 2dd       19 Mar 30 15:43 .pki
drwxr-xr-x.  2 2dd 2dd        6 Mar 30 13:08 Public
-rw-r--r--.  1 2dd 2dd  8304590 Feb 24 15:20 Red_Hat_Enterprise_Linux-7-Networking_Guide-en-US.pdf
-rw-r--r--.  1 2dd 2dd 24871779 Feb 24 15:20 Red_Hat_Enterprise_Linux-7-System_Administrators_Guide-en-US.pdf
drwx------.  2 2dd 2dd        6 Mar 30 13:49 .ssh
drwxr-xr-x.  2 2dd 2dd       26 Mar 30 19:26 Templates
drwx------.  3 2dd 2dd       48 Mar 30 20:42 .thunderbird
-rw-r--r--.  1 2dd 2dd      392 Mar 30 16:28 .tmux.conf
drwxr-xr-x.  2 2dd 2dd        6 Mar 30 13:08 Videos
-rw-------.  1 2dd 2dd     3638 Mar 30 20:09 .viminfo
-rw-rw-r--.  1 2dd 2dd       26 Mar 30 15:58 .Xresources
2.List only hidden files and folders
Code:
$ ls -ld .*
drwx------. 24 2dd 2dd 4096 Mar 30 21:22 .
drwxr-xr-x.  3 root  root    19 Mar 30 13:05 ..
-rw-------.  1 2dd 2dd 2707 Mar 30 20:55 .bash_history
-rw-r--r--.  1 2dd 2dd   18 Aug  2  2016 .bash_logout
-rw-r--r--.  1 2dd 2dd  193 Aug  2  2016 .bash_profile
-rw-r--r--.  1 2dd 2dd  274 Mar 30 14:05 .bashrc
drwx------. 21 2dd 2dd 4096 Mar 30 21:23 .cache
drwxr-xr-x. 25 2dd 2dd 4096 Mar 30 20:57 .config
drwx------.  5 2dd 2dd  164 Mar 30 20:57 .dropbox
drwxr-xr-x.  3 2dd 2dd   71 Mar 30 20:49 .dropbox-dist
-rw-------.  1 2dd 2dd   16 Mar 30 13:08 .esd_auth
drwxr-xr-x.  4 2dd 2dd 4096 Feb 24 18:55 .FRD
drwxr-xr-x. 24 2dd 2dd 4096 Mar 30 19:41 .gimp-2.8
drwx------.  3 2dd 2dd   18 Mar 30 15:43 .gnome
drwx------.  2 2dd 2dd   98 Mar 30 21:32 .gnupg
-rw-------.  1 2dd 2dd 2802 Mar 30 18:12 .ICEauthority
drwx------.  3 2dd 2dd   19 Mar 30 13:08 .local
drwxr-xr-x.  5 2dd 2dd   54 Mar 30 13:49 .mozilla
drwx------.  3 2dd 2dd   19 Mar 30 15:43 .pki
drwx------.  2 2dd 2dd    6 Mar 30 13:49 .ssh
drwx------.  3 2dd 2dd   48 Mar 30 20:42 .thunderbird
-rw-r--r--.  1 2dd 2dd  392 Mar 30 16:28 .tmux.conf
-rw-------.  1 2dd 2dd 3638 Mar 30 20:09 .viminfo
-rw-rw-r--.  1 2dd 2dd   26 Mar 30 15:58 .Xresources
3.List only folders (not hidden)
Code:
$ ls -ld */
drwxr-xr-x. 2 2dd 2dd   6 Mar 30 13:08 Desktop/
drwxr-xr-x. 2 2dd 2dd  17 Mar 30 21:42 Documents/
drwxr-xr-x. 2 2dd 2dd 238 Mar 30 21:28 Downloads/
drwx------. 8 2dd 2dd 168 Mar 30 20:57 Dropbox/
drwxr-xr-x. 2 2dd 2dd   6 Mar 30 13:08 Music/
drwxr-xr-x. 2 2dd 2dd   6 Mar 30 13:08 Pictures/
drwxr-xr-x. 2 2dd 2dd   6 Mar 30 13:08 Public/
drwxr-xr-x. 2 2dd 2dd  26 Mar 30 19:26 Templates/
drwxr-xr-x. 2 2dd 2dd   6 Mar 30 13:08 Videos/
4.List only folders (hidden only)
Code:
$ ls -ld .*/
drwx------. 24 2dd 2dd 4096 Mar 30 21:22 ./
drwxr-xr-x.  3 root  root    19 Mar 30 13:05 ../
drwx------. 21 2dd 2dd 4096 Mar 30 21:23 .cache/
drwxr-xr-x. 26 2dd 2dd 4096 Mar 30 21:42 .config/
drwx------.  5 2dd 2dd  164 Mar 30 20:57 .dropbox/
drwxr-xr-x.  3 2dd 2dd   71 Mar 30 20:49 .dropbox-dist/
drwxr-xr-x.  4 2dd 2dd 4096 Feb 24 18:55 .FRD/
drwxr-xr-x. 24 2dd 2dd 4096 Mar 30 19:41 .gimp-2.8/
drwx------.  3 2dd 2dd   18 Mar 30 15:43 .gnome/
drwx------.  2 2dd 2dd   98 Mar 30 21:32 .gnupg/
drwx------.  3 2dd 2dd   19 Mar 30 13:08 .local/
drwxr-xr-x.  5 2dd 2dd   54 Mar 30 13:49 .mozilla/
drwx------.  3 2dd 2dd   19 Mar 30 15:43 .pki/
drwx------.  2 2dd 2dd    6 Mar 30 13:49 .ssh/
drwx------.  3 2dd 2dd   48 Mar 30 20:42 .thunderbird/
Thanks in advance.

Last edited by ddenial; 03-30-2017 at 12:42 PM.
 
Old 03-30-2017, 12:05 PM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,264
Blog Entries: 10

Rep: Reputation: 5172Reputation: 5172Reputation: 5172Reputation: 5172Reputation: 5172Reputation: 5172Reputation: 5172Reputation: 5172Reputation: 5172Reputation: 5172Reputation: 5172
i'd start by reading
Code:
man ls
anything that's still unclear after that, we'll be happy to give you hints (but won't actually do your homework for you).
 
Old 03-30-2017, 12:17 PM   #3
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 342

Original Poster
Rep: Reputation: 54
Quote:
Originally Posted by ondoho View Post
i'd start by reading
Code:
man ls
anything that's still unclear after that, we'll be happy to give you hints (but won't actually do your homework for you).
Home work? Believe me I finished my education some decades ago.

I did go through man pages, but couldn't find it. So I thought its more probably about globbing thing.

I can go through man pages again if you think I can get answers, no problem about that.

Thanks
 
Old 03-30-2017, 12:29 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
The manual page for ls is long and detailed, so it will take several readings.

The glob page is a bit hidden. It is in the 7th section, which is hidden by the glob in the 3rd.

Code:
man glob
man 7 glob
man 3 glob
grep might be involved in some of the last tasks you list.

Last edited by Turbocapitalist; 03-30-2017 at 12:31 PM.
 
1 members found this post helpful.
Old 03-30-2017, 12:32 PM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 633

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Edit:
Quote:
I want to do this only through ls command.
Whoops, missed that part

----------

Quote:
List all files including hidden
List all files (hidden and not-hidden)
What's the difference here?

In any case, you can use find to have some more control with listing files in the terminal

Quote:
List all files including hidden
List only hidden files and folders
List only folders (not hidden)
List only folders (hidden only)
List only files (not hidden) -- Don't know
List only files (hidden) -- Don't know
List all files (hidden and not-hidden) -- Don't know
List all files including hidden
Code:
find . -maxdepth 1
List only hidden files and folders
Code:
find . -maxdepth 1 -iname '.*'
List only folders (not hidden)
Code:
find . -maxdepth 1 -type d -not -iname '.*'
List only folders (hidden only)
Code:
find . -maxdepth 1 -type d -iname '.*'
List only files (not hidden)
Code:
find . -maxdepth 1 -type f -not -iname '.*'
List only files (hidden)
Code:
find . -maxdepth 1 -type f -iname '.*'
List all files (hidden and not-hidden)
Code:
find . -maxdepth 1

Last edited by Sefyir; 03-30-2017 at 12:35 PM.
 
Old 03-30-2017, 12:32 PM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Listing files is easy.
Start by not calling them "folders".
 
Old 03-30-2017, 12:36 PM   #7
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 342

Original Poster
Rep: Reputation: 54
Quote:
Originally Posted by Turbocapitalist View Post
The manual page for ls is long and detailed, so it will take several readings.

The glob page is a bit hidden. It is in the 7th section, which is hidden by the glob in the 3rd.

Code:
man glob
man 7 glob
man 3 glob
grep might be involved in some of the last tasks you list.
Interesting, Thanks. I'll go through it.

@Sefyir, thanks, but I know how to do this in find command. I also know by using grep, I can get what I want. I want to do this explicitly by using ls command.

Thanks
 
Old 03-30-2017, 12:41 PM   #8
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 342

Original Poster
Rep: Reputation: 54
Quote:
Originally Posted by Sefyir View Post
What's the difference here?

List all files including hidden
List all files (hidden and not-hidden)
Sorry about that. I have corrected it now. Thanks
 
  


Reply


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
Help listing installed files and searching for missing files mightymouse2045 Linux - General 3 10-09-2013 07:57 PM
ls command not listing properly in terminal after cd to directory. Daiashi Linux - General 2 09-25-2012 12:19 AM
listing files and burning files - just a question radiodee1 Debian 1 11-26-2006 07:29 AM
Listing kernel modules in a terminal brunnopessoa Fedora 3 10-12-2004 06:58 PM
How to turn off color listing in regular Linux terminal .. purpleburple Linux - General 2 10-28-2002 03:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:02 AM.

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