Display or list all directories
Type the following command:
$ ls -l | egrep `^d'
Display or list only files
Type the following command:
$ ls -l | egrep -v `^d'
grep command used to searches input. It will filter out directories name by matching first character d. To reverse effect (just to display files) you need to pass -v option. It invert the sense of matching, to select non-matching lines.
$ ls -l | egrep -v `^d'
Task: Create aliases to save time
You can create two aliases as follows to list only directories and files.
alias lf="ls -l | egrep -v '^d'"
alias ldir="ls -l | egrep '^d'"
Put above two aliases in your bash shell startup file:
$ cd
$ vi .bash_profile
Append two lines:
alias lf="ls -l | egrep -v '^d'"
alias ldir="ls -l | egrep '^d'"
Save and close the file.
Now just type lf - to list files and ldir - to list directories only:
$ cd /etc
$ lf
Output:
-rw-r--r-- 1 root root 2149 2006-09-04 23:25 adduser.conf
-rw-r--r-- 1 root root 44 2006-09-29 05:11 adjtime
-rw-r--r-- 1 root root 197 2006-09-04 23:48 aliases
-rw------- 1 root root 144 2002-01-18 13:43 at.deny
-rw-r--r-- 1 root root 162 2006-09-22 23:24 aumixrc
-rw-r--r-- 1 root root 28 2006-09-22 23:24 aumixrc1
....
..
....
List directory names only:
$ cd /etc
$ ldir
Output:
drwxr-xr-x 4 root root 4096 2006-09-22 16:41 alsa
drwxr-xr-x 2 root root 4096 2006-09-20 20:59 alternatives
drwxr-xr-x 6 root root 4096 2006-09-22 16:41 apm
drwxr-xr-x 3 root root 4096 2006-09-07 02:51 apt
drwxr-xr-x 2 root root 4096 2006-09-08 01:46 bash_completion.d
....
.....
.
alias lf="ls -l | egrep -v '^d'"
alias ldir="ls -l | egrep '^d'"
Put above two aliases in your bash shell startup file:
$ cd
$ vi .bash_profile
Append two lines:
alias lf="ls -l | egrep -v '^d'"
alias ldir="ls -l | egrep '^d'"
Save and close the file.
$ cd /etc
$ lf
Output:
$ cd /etc
$ ldir
Output:find command
The find command can be used as follows to list all directories in /nas, enter:
find /nas -type d
find /nas -type d -ls
find . -type d -ls
Sample outputs:
1070785 8 drwxrwxrwt 8 root root 4096 Jul 5 07:12 .
1070797 8 drwx------ 2 root root 4096 Jul 4 07:22 ./orbit-root
1070843 8 drwxr-xr-x 2 root root 4096 Jun 16 18:55 ./w
1070789 8 drwxr-xr-x 10 root root 4096 Jun 17 14:54 ./b
1071340 8 drwxr-xr-x 2 root root 4096 Jun 16 18:55 ./b/init.d
1071581 8 drwxr-xr-x 3 root root 4096 Jun 16 18:55 ./b/bind
1071584 8 drwxr-xr-x 2 root root 4096 Jun 16 18:55 ./b/bind/bak
1071617 8 drwxr-xr-x 2 root root 4096 Jun 16 18:55 ./b/fw
1071628 8 drwxr-xr-x 8 root root 4096 Jun 16 18:55 ./b/scripts
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop
April 26, 2007 at 1:07 pm
To get just file names without the long list data:
ls -l | grep ‘^d’ | awk ‘{ print $9 }’
Or
for foo in *; do if [ -d $foo ]; then print -n ” $foo”; else false; fi; done
2 September 18, 2007 at 7:39 am
find . -type d -maxdepth 1
3 September 21, 2007 at 12:15 am
ls -d */
or for hidden directories
ls -d .*/
4 September 21, 2007 at 12:52 pm
Thanks for contributing all other tips :)
5 September 26, 2007 at 5:56 am
Thanks a lot for nice recipe… :-)
6 October 1, 2007 at 2:48 pm
To get just the subdirectories names
find . -type d -maxdepth 1 -exec basename {} \;
However, in this case you also get the directory itself! To avoid this, add mindepth 1:
find . -type d -maxdepth 1 -mindepth 1 -exec basename {} \;
7 November 18, 2007 at 3:12 pm
For some reasons on FAT fs
ls -d */
is not working. But this works on any filesystem:
ls -d ./*/
8 January 4, 2008 at 10:52 am
echo */
9 January 11, 2009 at 5:19 pm
mysurface — you are the man!
10 January 21, 2009 at 1:18 am
(folders)
ls -F | grep /
(files)
ls -F | grep -v /
11 January 30, 2009 at 5:13 am
Lists all the subdirectories in the current directory..
ls -R | grep ./
12 May 1, 2009 at 6:19 pm
can some1 please help me i need to know what the significant effects are of the following characters in unix filenames. 2 dots / 1 dot / tilde or “squiggle”
..
.
~
14 May 5, 2009 at 7:35 am
Can you just list the directories name, without the permission, datetime, and etc … ?
15 May 8, 2009 at 10:22 am
Thanx, nice post. I was just thinking of doing it with awk but couldnt get the wildcard working. Now I am one step ahead in my shell knowledge ^^
To Dennis Quek above :
A rather dirty solution but does the job.
ls -l | egrep ‘^d’ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘| awk ‘$1=” “‘| awk ‘$1=” “‘
(forgive me if seeing this code gives someone a heart attack, all i can say is that I’m still learning :P )
I would love if someone can give my code a neater look :)
- 16 June 20, 2011 at 3:58 pm
-
A cleaner Recipe for the same using AWK :
ls -l | egrep ‘^d’ | awk ‘{ print $9}’
another alternate is bit clumsy :
ls -l | egrep ‘^d’ | awk ‘$1=” “,$2=” “‘ | awk ‘$1=” “,$2=” “‘ | awk ‘$1=” “,$2=” “‘ | awk ‘$1=” “,$2=” “‘
Another Simpler and Faster way out to get the Desired result :
ls -l|grep ‘^d’
17 May 8, 2009 at 10:34 am
oops! didnt read the other comments. Already many nice solutions have been posted :P
18 June 30, 2009 at 12:29 pm
i used to the $ du command to retrieve all directory names.
$ du
19 July 28, 2009 at 2:38 pm
> i used to the $ du command to retrieve all directory names.
Jagan,
du is used to calculate Disk Usage. I want to know did you tweak this command to display directory listing in pwd. Please explain.
20 October 7, 2009 at 6:36 pm
$ /bin/ls -l | grep “^d” | cut -d’ ‘ -f8
it’s a neat trick on linux systems.
i’m using /bin/ls and not ls because it may be aliased
21 October 20, 2009 at 5:30 am
one can list directory i simple and easy way by :
1. ls -ltr | grep -e d
2. ls -ld */
3. du
third one is the most easiest way. :-)
22 October 23, 2009 at 7:58 am
I tried with both GNU ls and BSD one and the best option is as someone else posted above:
ls -ld */
23 December 9, 2009 at 11:29 am
i renamed a directory name with a space in between the directory name within GUI(Graphical User Interface)
which is allowed and now in the shell prompt i.e. in the terminal command mode i want to enter the directory which has a space in between but it does not allows to enter into that directory it says “INVALID DIRECTORY”
is their any alternative solution or trick to enter into the directory with the name having space.please explain
the directory name is “file data”
i tried with — $cd file data but it didnt work out.
24 February 25, 2010 at 6:27 pm
nice stuff!
i learned a lot from the original post, but the comments are a treasure trove!
one thing about the original post, you have a back tick instead of a single quote in the 1st two snippets
ls -l | egrep `^d’ for example should be ls -l | egrep ‘^d’ for the copy / pasters
thanks again ;)
25 March 3, 2010 at 6:52 pm
if you wanted to pull a date from the directory to only list files from datea/timea through dateb/timeb (say March 2 at 6:00 AM to March 8 at 6:00 AM) and move into a new file the output, how is this accomplished? I tried this but no output
#!/bin/bash
FILES=’ls -A1′
OUTFILE=”c:/temp/RRDfilesinrange.txt”
STARTDATE=’date –utc –date “2010-02-27 06:00:00″ %s’
ENDDATE=’date –utc –date “2010-03-02 06:00:00″ %s’
cd C: temp/tsfr-complete
for f in $FILES
do
if [ -f $f ]; then
FDATE=’stat -c “%Z” $f’
if [ $FDATE -ge $STARTDATE ]; then
if [ $FDATE -le $ENDDATE ]; then
‘echo $f >> $OUTFILE’
fi
fi
fi
done
26 April 27, 2010 at 2:17 am
How do you get the directory listing to display only: the file name, the user that owns the file and that user’s permissions?
27 August 4, 2010 at 2:34 pm
//Mohammed asif December 9, 2009
is their any alternative solution or trick to enter into the directory with the name having space.please explain//
did you try
cd “file data”
or
cd ‘file data’
( i.e. directory name with double quotes or single quote)
you yourself have given the hint :-)
28 August 23, 2010 at 10:20 am
@Davis:
ls -l | awk ‘{ print $1,$3,$9 }’
29 October 8, 2010 at 2:53 am
alias ldir="ls -d -1 */ | tr -d '/'"
ldir
- 30 February 15, 2011 at 11:19 am
-
works :)) Thanks
31 October 21, 2010 at 8:20 am
Hello.
I would like to do some batching in my linux but I couldn’t find any way of initializing an array containing folder’s location information. In short, I have data in two folders located at:
/home/alireza/Desktop/DTIBetula101019/B2509.30
/home/alireza/Desktop/DTIBetula101019/B2509.50
and I want to make an array following by a loop to do some specific command inside each folder seprately like this:
fn_list= (‘/home/alireza/Desktop/DTIBetula101019/B2509.30′,’/home/alireza/Desktop/DTIBetula101019/B2509.50′)
for fn in ${fn_list}
do
cd $fn
fsl4.1-fslmerge -t big4D 2501.45-DTI1-s007 2501.45-DTI2-s008 2501.45-DTI3-s009
done
can anybody help me to figure out how to specify an array that can pointed to a folder which can be reused during a for loop?
Thanks
/Alireza
32 January 6, 2011 at 6:53 pm
alias lsdir=’ls -d ./*/ | cut -d / -f 2′
lsdir
for i in `lsdir` ; do du -ks $i; done
33 September 21, 2011 at 3:26 pm
Commenting on Eric’s ls | grep | awk combination:
Cygwin (1.7.9) displays 8, not 9, fields in
ls -l
.
So change his “awk ‘{print 9}’” to awk ‘{print 8}’ and you’ll get more than a bunch of deadspace in mintty.
Just in case it wasn’t already obvious. *S*
BZT
34 May 30, 2012 at 10:50 am
ls -lrt | grep /
35 June 30, 2012 at 6:32 am
From all the above I feel the best way is …
ls -ld */
36 October 30, 2012 at 9:53 am
Thank you for a very helpful and useful post – much appreciated… – James
37 April 4, 2013 at 9:34 am
A sed version:
ls -F1 | grep ‘/’ | sed ‘s/\///’
38 May 21, 2013 at 11:29 am
ls -d */ or ls -ld */ (Use these commands to display only directories in current user)
cd /etc
ls -ld */ ( Use these two commands to display only directories for all users including root)
39 May 24, 2013 at 10:28 pm
list of directories:
ls -alp /
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop
To get just file names without the long list data:
ls -l | grep ‘^d’ | awk ‘{ print $9 }’
Or
for foo in *; do if [ -d $foo ]; then print -n ” $foo”; else false; fi; done
find . -type d -maxdepth 1
ls -d */
or for hidden directories
ls -d .*/
ls -d .*/
Thanks for contributing all other tips :)
Thanks a lot for nice recipe… :-)
To get just the subdirectories names
find . -type d -maxdepth 1 -exec basename {} \;
However, in this case you also get the directory itself! To avoid this, add mindepth 1:
find . -type d -maxdepth 1 -mindepth 1 -exec basename {} \;
For some reasons on FAT fs
ls -d */
is not working. But this works on any filesystem:
ls -d ./*/
echo */
mysurface — you are the man!
(folders)
ls -F | grep /
(files)
ls -F | grep -v /
ls -F | grep /
(files)
ls -F | grep -v /
Lists all the subdirectories in the current directory..
ls -R | grep ./
can some1 please help me i need to know what the significant effects are of the following characters in unix filenames. 2 dots / 1 dot / tilde or “squiggle”
..
.
~
..
.
~
Can you just list the directories name, without the permission, datetime, and etc … ?
Thanx, nice post. I was just thinking of doing it with awk but couldnt get the wildcard working. Now I am one step ahead in my shell knowledge ^^
To Dennis Quek above :
A rather dirty solution but does the job.
To Dennis Quek above :
A rather dirty solution but does the job.
ls -l | egrep ‘^d’ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘ | awk ‘$1=” “‘| awk ‘$1=” “‘| awk ‘$1=” “‘
(forgive me if seeing this code gives someone a heart attack, all i can say is that I’m still learning :P )
I would love if someone can give my code a neater look :)
I would love if someone can give my code a neater look :)
- 16 June 20, 2011 at 3:58 pm
- A cleaner Recipe for the same using AWK :ls -l | egrep ‘^d’ | awk ‘{ print $9}’another alternate is bit clumsy :ls -l | egrep ‘^d’ | awk ‘$1=” “,$2=” “‘ | awk ‘$1=” “,$2=” “‘ | awk ‘$1=” “,$2=” “‘ | awk ‘$1=” “,$2=” “‘Another Simpler and Faster way out to get the Desired result :ls -l|grep ‘^d’
oops! didnt read the other comments. Already many nice solutions have been posted :P
i used to the $ du command to retrieve all directory names.
$ du
$ du
> i used to the $ du command to retrieve all directory names.
Jagan,
du is used to calculate Disk Usage. I want to know did you tweak this command to display directory listing in pwd. Please explain.
Jagan,
du is used to calculate Disk Usage. I want to know did you tweak this command to display directory listing in pwd. Please explain.
$ /bin/ls -l | grep “^d” | cut -d’ ‘ -f8
it’s a neat trick on linux systems.
it’s a neat trick on linux systems.
i’m using /bin/ls and not ls because it may be aliased
one can list directory i simple and easy way by :
1. ls -ltr | grep -e d
2. ls -ld */
3. du
1. ls -ltr | grep -e d
2. ls -ld */
3. du
third one is the most easiest way. :-)
I tried with both GNU ls and BSD one and the best option is as someone else posted above:
ls -ld */
i renamed a directory name with a space in between the directory name within GUI(Graphical User Interface)
which is allowed and now in the shell prompt i.e. in the terminal command mode i want to enter the directory which has a space in between but it does not allows to enter into that directory it says “INVALID DIRECTORY”
is their any alternative solution or trick to enter into the directory with the name having space.please explain
the directory name is “file data”
i tried with — $cd file data but it didnt work out.
which is allowed and now in the shell prompt i.e. in the terminal command mode i want to enter the directory which has a space in between but it does not allows to enter into that directory it says “INVALID DIRECTORY”
is their any alternative solution or trick to enter into the directory with the name having space.please explain
the directory name is “file data”
i tried with — $cd file data but it didnt work out.
nice stuff!
i learned a lot from the original post, but the comments are a treasure trove!
one thing about the original post, you have a back tick instead of a single quote in the 1st two snippets
ls -l | egrep `^d’ for example should be ls -l | egrep ‘^d’ for the copy / pasters
ls -l | egrep `^d’ for example should be ls -l | egrep ‘^d’ for the copy / pasters
thanks again ;)
if you wanted to pull a date from the directory to only list files from datea/timea through dateb/timeb (say March 2 at 6:00 AM to March 8 at 6:00 AM) and move into a new file the output, how is this accomplished? I tried this but no output
#!/bin/bash
FILES=’ls -A1′
OUTFILE=”c:/temp/RRDfilesinrange.txt”
STARTDATE=’date –utc –date “2010-02-27 06:00:00″ %s’
ENDDATE=’date –utc –date “2010-03-02 06:00:00″ %s’
cd C: temp/tsfr-complete
for f in $FILES
do
if [ -f $f ]; then
FDATE=’stat -c “%Z” $f’
if [ $FDATE -ge $STARTDATE ]; then
if [ $FDATE -le $ENDDATE ]; then
‘echo $f >> $OUTFILE’
fi
fi
fi
done
#!/bin/bash
FILES=’ls -A1′
OUTFILE=”c:/temp/RRDfilesinrange.txt”
STARTDATE=’date –utc –date “2010-02-27 06:00:00″ %s’
ENDDATE=’date –utc –date “2010-03-02 06:00:00″ %s’
cd C: temp/tsfr-complete
for f in $FILES
do
if [ -f $f ]; then
FDATE=’stat -c “%Z” $f’
if [ $FDATE -ge $STARTDATE ]; then
if [ $FDATE -le $ENDDATE ]; then
‘echo $f >> $OUTFILE’
fi
fi
fi
done
How do you get the directory listing to display only: the file name, the user that owns the file and that user’s permissions?
//Mohammed asif December 9, 2009
is their any alternative solution or trick to enter into the directory with the name having space.please explain//
did you try
cd “file data”
or
cd ‘file data’
( i.e. directory name with double quotes or single quote)
you yourself have given the hint :-)
is their any alternative solution or trick to enter into the directory with the name having space.please explain//
did you try
cd “file data”
or
cd ‘file data’
( i.e. directory name with double quotes or single quote)
you yourself have given the hint :-)
@Davis:
ls -l | awk ‘{ print $1,$3,$9 }’
ls -l | awk ‘{ print $1,$3,$9 }’
alias ldir="ls -d -1 */ | tr -d '/'"
ldir
- 30 February 15, 2011 at 11:19 am
- works :)) Thanks
Hello.
I would like to do some batching in my linux but I couldn’t find any way of initializing an array containing folder’s location information. In short, I have data in two folders located at:
/home/alireza/Desktop/DTIBetula101019/B2509.30
/home/alireza/Desktop/DTIBetula101019/B2509.50
I would like to do some batching in my linux but I couldn’t find any way of initializing an array containing folder’s location information. In short, I have data in two folders located at:
/home/alireza/Desktop/DTIBetula101019/B2509.30
/home/alireza/Desktop/DTIBetula101019/B2509.50
and I want to make an array following by a loop to do some specific command inside each folder seprately like this:
fn_list= (‘/home/alireza/Desktop/DTIBetula101019/B2509.30′,’/home/alireza/Desktop/DTIBetula101019/B2509.50′)
for fn in ${fn_list}
do
cd $fn
fsl4.1-fslmerge -t big4D 2501.45-DTI1-s007 2501.45-DTI2-s008 2501.45-DTI3-s009
done
do
cd $fn
fsl4.1-fslmerge -t big4D 2501.45-DTI1-s007 2501.45-DTI2-s008 2501.45-DTI3-s009
done
can anybody help me to figure out how to specify an array that can pointed to a folder which can be reused during a for loop?
Thanks
/Alireza
/Alireza
alias lsdir=’ls -d ./*/ | cut -d / -f 2′
lsdir
for i in `lsdir` ; do du -ks $i; done
Commenting on Eric’s ls | grep | awk combination:
Cygwin (1.7.9) displays 8, not 9, fields in
Cygwin (1.7.9) displays 8, not 9, fields in
- ls -l
.
So change his “awk ‘{print 9}’” to awk ‘{print 8}’ and you’ll get more than a bunch of deadspace in mintty.
Just in case it wasn’t already obvious. *S*
BZT
ls -lrt | grep /
From all the above I feel the best way is …
ls -ld */
Thank you for a very helpful and useful post – much appreciated… – James
A sed version:
ls -F1 | grep ‘/’ | sed ‘s/\///’
ls -d */ or ls -ld */ (Use these commands to display only directories in current user)
cd /etc
ls -ld */ ( Use these two commands to display only directories for all users including root)
cd /etc
ls -ld */ ( Use these two commands to display only directories for all users including root)
list of directories:
ls -alp /
ls -alp /
Random Thoughts: Howto: Linux / Unix List Just Directories Or Directory Names >>>>> Download Now
ReplyDelete>>>>> Download Full
Random Thoughts: Howto: Linux / Unix List Just Directories Or Directory Names >>>>> Download LINK
>>>>> Download Now
Random Thoughts: Howto: Linux / Unix List Just Directories Or Directory Names >>>>> Download Full
>>>>> Download LINK RN