Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to write the linux code for aceiving the functionality of "finding list of files and directories" that is passed as an argument....

i need guidence. please help me.....

ls -l can be used for the purpose, but how to pass the argument
Posted
Comments
OriginalGriff 10-May-11 2:50am    
More information needed: what are you going to write this in?
ZeeroC00l 11-May-11 7:15am    
You want to code this using shell script or any other language in unix ??

See here[^] for a base program to help you.
 
Share this answer
 
If you are looking for the shell script method, you can use one of the below methods.

Method 1 : Read Input from the User After Running the Script.

#!/usr/bin/bash

echo " Enter the Directory Name From which you need to get the list of the Files "
read directoryPath

echo " The Directory You have entered is [ $directoryPath ]"

if [ -d "$directoryPath" ]; then
        ls -l $directoryPath | grep -v total
else
        echo "The Directory Entered is Invalid or does not exist !!! "
fi



Method 2 : Pass the Input as argument in command line :

#!/usr/bin/bash

if [ "$#" -lt 1 -o "$#" -gt 2 ]; then
        echo " usage : $0 <directoryname> "
        exit 0;
fi

if [ -d $1 ]; then
        ls -l $2 | grep -v total
else
        echo " Invalid Directory Name or Directory Does nto exist !!!"
fi

</directoryname>


Both these are designed to take only one value as input for the Directory name. You can make necessary Modification so that as the same time more directories can be given as input.

If you are looking for getting the directory list using programming languages like C/C++ then @Richard MacCutchan 's answer sounds great..


BR//
Harsha
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900