Ubuntu: Show Directory Names in Bash

This is a very short tip.

I made a small shell script to delete and create a link to my localhost htdocs directory. That way, I never have to move my projects around, I just keep them in separate directories under one main directory.

The problem comes when I forget important things: such as directory names. Instead of just typing ls /projects, I want the script to tell me the options I have.

And so, this little code is born, by not giving the script any arguments:

#!/bin/bash
	if [ -z $1 ]
	then
		echo "No valid user was provided!"
		echo "Possible users:"
		declare -a FOO
		let count=0
		let count2=0
		for d in $( find /projects -maxdepth 1 -type d -printf "%f " ); do
			if [ $count2 -gt 0 ]
			then
				FOO[$count]=$d
			fi
			((count++))
			((count2++))
		done
		echo ${FOO[@]}
	fi

Note: I did it this way because otherwise you get “projects” in the list of results.

Tags: , ,


%d bloggers like this: