LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ls */ explanation (https://www.linuxquestions.org/questions/linux-newbie-8/ls-%2A-explanation-897968/)

zeusys 08-17-2011 09:54 PM

ls */ explanation
 
hi
would someone please explain these arguments.I know what these do,but I want to know the meaning of these characters.For example I know "ls /" lists files/directories under root directory.but in "ls */" what * and / refer to?

ls */

and

ls -ld */


thank you

cargod_hera 08-17-2011 10:15 PM

Just as "ls" lists the contents of the directory that you are in, "ls -l" gives you a list of the sub-directories and gives you information about those sub-directories (i.e. permissions, owner, size, etc.).
The command "ls */" shows not only the contents of the directory that you are in but also the contents of the sub-directories. Likewise, using "ls -ld */" will show the directory contents and information about the sub-directories.

There are some additional "ls" arguments that you might find helpful as well. Using "ls ~" will show the contents of your home directory. Using "ls ../" shows the contents of the parent directory (the parent of the directory you are in) and "ls /" lists the contents of the root directory.

rknichols 08-17-2011 11:07 PM

It's your shell, not ls, that expands the "*", and ls simply acts on the argument list that the shell supplies. A simple "*" expands to a list of all non-dot files in the current directory, where the term "files" includes not only ordinary files but also directory files, device files, named pipes, ... -- all the things that can appear in a Unix file system. When you append a trailing slash, "*/", the list is restricted to just directory files. So,
Code:

ls */
would invoke ls with a list of all of the non-dot directory files in the current directory, and ls would dutifully list the names and contents of each of those directories. If you include the "-ld" flags:
Code:

ls -ld */
then ls gets the same list as before, but the "-l" flag instructs it to produce a long-format listing, and the "-d" flag tells it to list only the names of directories, not their contents.

tommylovell 08-17-2011 11:41 PM

I like rknichols explanation. It's better than mine, but i'll try to make up for it with a lack of brevity...

I don't have formal knowledge of this and probably don't have the terms right, but it's easy enough to do a few experiments and comment on them.

Just an 'ls' command shows the contents of the current directory.

Code:

[root@athlon ~]# ls -F
anaconda-ks.cfg  install.log.syslog  test                    tlpi-book/
home.iso        ps_mem.py*          tlpi-110706-book.tar.gz
install.log      swap*              tlpi-110706-dist.tar.gz

(The '-F' flag is just to tag an indicator onto the end of each directory entry listed: * is executable; / is a directory.)


When you list with an asterisk, 'ls *', it lists the current directory and goes one level deeper.

Code:

[root@athlon ~]# ls -F *
anaconda-ks.cfg  install.log.syslog  test
home.iso        ps_mem.py*          tlpi-110706-book.tar.gz
install.log      swap*              tlpi-110706-dist.tar.gz

tlpi-book:
acl/        filesys/              Makefile.inc.Tru64  psem/    syslim/
altio/      getopt/              memalloc/          pshm/    threads/
BUILDING    inotify/              mmap/              pty/      time/
cap/        lib/                  pgsjc/              README    timers/
CHANGES      libtlpi.a            pipes/              shlibs/  tty/
COPYING      loginacct/            pmsg/              signals/  users_groups/
daemons/    Makefile              proc/              sockets/  vmem/
dirs_links/  Makefile.inc          proccred/          svipc/    xattr/
filebuff/    Makefile.inc.FreeBSD  procexec/          svmsg/
fileio/      Makefile.inc.HP-UX    procpri/            svsem/
filelock/    Makefile.inc.MacOSX  procres/            svshm/
files/      Makefile.inc.Solaris  progconc/          sysinfo/


With a slash appended to a filename, it means "directory". (Like if you did a 'cp file directory/', it won't make a copy of "file" and call it "directory"; instead the slash forces 'cp' to make a file and place it into a directory called "directory".)

So 'ls */' says list off anything (*), but only if it is a directory (and go one level deep). So here we just get the contents of directory "tlpi-book".

Code:

[root@athlon ~]# ls -F */
acl/        filesys/              Makefile.inc.Tru64  psem/    syslim/
altio/      getopt/              memalloc/          pshm/    threads/
BUILDING    inotify/              mmap/              pty/      time/
cap/        lib/                  pgsjc/              README    timers/
CHANGES      libtlpi.a            pipes/              shlibs/  tty/
COPYING      loginacct/            pmsg/              signals/  users_groups/
daemons/    Makefile              proc/              sockets/  vmem/
dirs_links/  Makefile.inc          proccred/          svipc/    xattr/
filebuff/    Makefile.inc.FreeBSD  procexec/          svmsg/
fileio/      Makefile.inc.HP-UX    procpri/            svsem/
filelock/    Makefile.inc.MacOSX  procres/            svshm/
files/      Makefile.inc.Solaris  progconc/          sysinfo/

Now if you use the '-l' flag, you would get a long listing - typically with mode, owner, group, size, date.

Code:

[root@athlon ~]# ls -Fl */
total 420
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 acl/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 altio/
-rw-r--r--. 1 1000 1000  4911 Sep  1  2010 BUILDING
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 cap/
-rw-r--r--. 1 1000 1000  1677 Jul  6 07:45 CHANGES
-rw-r--r--. 1 1000 1000  34520 Mar 27  2010 COPYING
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 daemons/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 dirs_links/
drwxr-xr-x. 2 1000 1000  4096 Jul  6 07:47 filebuff/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 fileio/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 filelock/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 files/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 filesys/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 getopt/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 inotify/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 lib/
-rw-r--r--. 1 root root 162616 Aug  9 20:16 libtlpi.a
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 loginacct/
-rw-r--r--. 1 1000 1000    835 Jul  6 07:38 Makefile
-rw-r--r--. 1 1000 1000    704 Sep 13  2010 Makefile.inc
-rw-r--r--. 1 1000 1000    323 Feb 22  2010 Makefile.inc.FreeBSD
-rw-r--r--. 1 1000 1000    462 Feb 22  2010 Makefile.inc.HP-UX
-rw-r--r--. 1 1000 1000    318 Mar 27  2010 Makefile.inc.MacOSX
-rw-r--r--. 1 1000 1000    415 Feb 22  2010 Makefile.inc.Solaris
-rw-r--r--. 1 1000 1000    377 Feb 22  2010 Makefile.inc.Tru64
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 memalloc/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 mmap/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:16 pgsjc/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 pipes/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 pmsg/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 proc/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 proccred/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 procexec/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 procpri/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 procres/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 progconc/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 psem/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 pshm/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 pty/
-rw-r--r--. 1 1000 1000  4300 Jan 17  2011 README
drwxr-xr-x. 5 1000 1000  4096 Aug  9 20:17 shlibs/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 signals/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 sockets/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 svipc/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 svmsg/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 svsem/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 svshm/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 sysinfo/
drwxr-xr-x. 2 1000 1000  4096 Jul  6 07:47 syslim/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 threads/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 time/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 timers/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 tty/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 users_groups/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 vmem/
drwxr-xr-x. 2 1000 1000  4096 Aug  9 20:17 xattr/

And if you use the '-d' flag, it lists the directory, not its contents. From 'man ls',
Quote:

-d, --directory
list directory entries instead of contents, and do not derefer
ence symbolic links
Code:

[root@athlon ~]# ls -Fld */
drwxr-xr-x. 46 1000 1000 4096 Aug  9 20:16 tlpi-book//

This explanation is pretty unscientific, but it illustrates your question for others that might be reading this post.


All times are GMT -5. The time now is 12:41 PM.