Wednesday, November 23, 2011

Alternative of "ls" command

What if your "ls" binary get corrupted ? Is there any alternative of "ls" command. The Answer is "YES"

echo Does ls

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 /usr
% ls *
bin  etc  games  include  java  kerberos  lib  libexec  local  man  nsh  openv  sbin  share  src  tmp  X11R6

% echo *
bin etc games include java kerberos lib libexec local man nsh openv sbin share src tmp X11R6


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

No comments:

Post a Comment