UNIX Hints & Hacks

ContentsIndex

Chapter 1: Topics in Administration

 

Previous ChapterNext Chapter

Sections in this Chapter:

1.1 Collecting System Information

 

1.7 Swap on-the-Fly

 

1.13 Remove the ---- Dashes ----

1.2 Backup Key Files!

 

1.8 Keep It Up with nohup

 

1.14 echo Does ls

1.3 Execution on the Last Day of a Month

 

1.9 Redirecting Output to Null

 

1.15 Building Large Dummy Files

1.4 Dealing with Unwanted Daemons

 

1.10 Keeping Remote Users Out

 

1.16 Burning-in Disk Drives

1.5 Keep Those Daemons Running

 

1.11 Rewinding Tapes Fast

 

1.17 Bringing a System Down

1.6 fuser Instead of ps

1.12 Generating a Range of Numbers

 

 

1.14 echo Does ls

1.14.1 Description

1.14.1 Description

Why not use echo instead of ls?

Example

Flavors: All

Shells: Those that understand globbing.

Did you know that echo can list out a directory much like the ls command? The shell you use must understand globbing in order for this to work. The formatting of the information that results is really the only difference between the two. An output of ls yields a single or multiple column listing. When echo is used, the files are all spaced one right after the other.

% cd /var
% ls *
adm/       log/       named/     opt/       saf/       tmp/
audit/     lp/        news/      preserve/  spool@     uucp/
cron/      mail@      nis/       sadm/      statmon/   yp/
% echo *
adm audit cron log lp mail named news nis opt preserve sadm saf spool statmon tmp uucp yp

In looking at the examples for the two commands, you can see right away that echo doesn't output any of the file description labels. The real ls command won't display the file description labels either. These labels help to identify the type of files, which are directories ( /), soft links ( @), or executables ( *).

So why do they show up on the ls command? These description labels appear when the -F argument is passed to ls. A lot of vendors and admins like to set an alias entry in the user startup login scripts as a convenience to help identify what the files are. Check the login script that you are using--.login, .profile, .cshrc, or .alias--and you will see an entry similar to

alias ls     ls -CF

Reasons

There might come a time when you will not be able to use the ls command. It might not even be accessible from miniroot. Script writing is easier without having to unalias the ls command all the time. Using echo displays a clean list of files within a given directory.

Real World Experience

Hard system crashes or drives dying can bring systems down to the point where filesystems are so corrupted that they are unable to mount. When this occurs, at times the only way to see the system files is to use echo for displaying the files and directories.

In writing scripts for users or for the system, you never know whether the account that the script runs under has spurious alias definitions. Some users and admins can get creative with their aliases and pass multiple commands or pipe several commands together within an alias entry. To avoid having to set an unalias in your scripts, use the echo command. Here are some examples where echo might be used within scripts.

A variable definition:

list=`echo *`

To pass files through a loop:

for $list in `echo *`
do
 source code
done

Other Resources

Man pages:

alias, echo, ls

UNIX Hints & Hacks

ContentsIndex

Chapter 1: Topics in Administration

 

Previous ChapterNext Chapter

Sections in this Chapter:

1.1 Collecting System Information

 

1.7 Swap on-the-Fly

 

1.13 Remove the ---- Dashes ----

1.2 Backup Key Files!

 

1.8 Keep It Up with nohup

 

1.14 echo Does ls

1.3 Execution on the Last Day of a Month

 

1.9 Redirecting Output to Null

 

1.15 Building Large Dummy Files

1.4 Dealing with Unwanted Daemons

 

1.10 Keeping Remote Users Out

 

1.16 Burning-in Disk Drives

1.5 Keep Those Daemons Running

 

1.11 Rewinding Tapes Fast

 

1.17 Bringing a System Down

1.6 fuser Instead of ps

1.12 Generating a Range of Numbers

 

 

© Copyright Macmillan USA. All rights reserved.