I want to list all directories using a shell script. I am using the following code:
DIR="$1"
if [ $# -ne 1 ]
then
echo "Usage: $0 {dir-name}"
exit 1
fi
cd "$DIR"
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for user in $( ls -d */)
do
for dirs in $( ls -d $PWD/$user*)
do
echo $PWD/$user/$dirs;
done
done
IFS=$SAVEIFS
It is working for me if that directory don’t have any spaces on it, else it split the output for every spaces on it. I got the following output:
abhinaba@abhinaba-desktop:~/software$ sh test.sh /media/2C44138344134F48/RB1
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/3001/*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/3002/*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/VB*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/DLI*: No such file or directory
ls: cannot access /m: No such file or directory
ls: cannot access dia/2C44138344134F48/RB1/3003/*: No such file or directory
EDITED: removed
lsto optimize the script (fromtripleee)If you use BASH shell:
Run the above “test.sh” script like this:
or simply (if you are already in BASH and you have set the eXecutable flag)
Reference