Wednesday, September 15, 2010

5 different ways to do file listing without ls command

  ls has to be the command which every UNIX user has used the most. Well, what if ls command does not exist. Can we still list the files and directories without the ls command? In other words, can we simulate the ls command? There are many different ways to achieve that. Lets us see some of them.

1. The simplest of all is using the echo command:
echo *
  In case, if you also want to list the hidden files as well,

echo * .*
2. Using the set command, the listing can be done by saving it in positional parameters:
set -- *
echo $*
3.  Using the find command, we can list the files only in the current directory. However, the maxdepth option will be available only in the GNU versions.
find . -maxdepth 1
4. The most conventional option to run a for loop to do the file listing:
for file in * ; do echo $file; done
5. A simple shell script named say 'list' can be written with the following contents
#cat list
echo $*
  Now on running the script as shown below, all the files will be displayed.
#./list *
Happy File listing!!!

No comments:

Post a Comment