3.3.2 Looking at the contents of directories

Rate this post

Next: 3.3.3 Creating new directories Up: 3.3 First Steps into Previous: 3.3.1 Moving around

          Now that you know how to move around directories you probably think, « So what? » The basic skill of moving around directories is fairly useless, so let’s introduce a new command, ls. ls prints a listing of files and directories, by default from your current directory. For example:

/home/larry# ls Mail letters papers


Here we can see that Larry has three entries in his current directory: Mail, letters, and papers. This doesn’t tell us much—are these directories or files? We can use the -F option on the ls command to tell us more.

/home/larry# ls –F Mail/ letters/ papers/


From the / appended to each filename, we know that these three entries are in fact subdirectories.

    Using ls -F may also append « * » to the end of a filename. This indicates that the file is an executable, or a program which can be run. If nothing is appended to the filename using ls -F, the file is a « plain old file », that is, it’s neither a directory, or an executable.

In general, each UNIX command may take a number of options in addition to other arguments. These options usually begin with a « – », as demonstrated above with ls -F. The -F option tells ls to give more information about the type of the files involved—in this case, printing a / after each directory name.

If you give ls a directory name, it will print the contents of that directory.

Lire aussi...  The Linux Intranet Server HOWTO

/home/larry# ls –F papers english-lit history-final masters-thesis notes/


Or, for a more interesting listing, let’s see what’s in the system’s /etc directory.

/home/larry# ls /etc

Images          ftpusers        lpc             rc.new          shells
adm             getty           magic           rc0.d           startcons
bcheckrc        gettydefs       motd            rc1.d           swapoff
brc             group           mount           rc2.d           swapon
brc~            inet            mtab            rc3.d           syslog.conf
csh.cshrc       init            mtools          rc4.d           syslog.pid
csh.login       init.d          pac             rc5.d           syslogd.reload
default         initrunlvl      passwd          rmt             termcap
disktab         inittab         printcap        rpc             umount
fdprm           inittab.old     profile         rpcinfo         update
fstab           issue           psdatabase      securetty       utmp
ftpaccess       lilo            rc              services        wtmp

(For those MS-DOS users out there, notice how the filenames can be longer than 8 characters, and can contain periods in any position. It is even possible to have more than one period in a filename.)

Let’s cd up to the top of the directory tree, using « cd .. », and then down to another directory: /usr/bin.

/home/larry# cd ..
/home# cd ..
/# cd usr
/usr# cd bin
/usr/bin# You can also move into directories in multiple steps, as in cd /usr/bin.

Try moving around various directories, using ls and cd. In some cases, you may run into a foreboding « Permission denied » error message. This is simply the concept of UNIX security kicking in: in order to ls or to cd into a directory, you must have permission to do so. We’ll talk more about this in         Section 3.9.

Next: 3.3.3 Creating new directories Up: 3.3 First Steps into Previous: 3.3.1 Moving around

Matt Welsh