Bash to list all folders in a specific directory

The below bash is trying to list the folders in a specific directory. It seems close but adds the path to the filename, which basename could strip off I think, but not sure why it writes the text file created? This list of folders in the directory will be used later, but needs to only be the folder names. Thank you :).

Bash

#!/bin/bash
dir="/home/cmccabe/Desktop/tetsfolder/"
for file in ${dir}/*
  do  
    echo "$file"
  done > /home/cmccabe/Desktop/tetsfolder/list

folders in /home/cmccabe/Desktop/tetsfolder

f1
f2
f3

current output

/home/cmccabe/Desktop/tetsfolder/f1
/home/cmccabe/Desktop/tetsfolder/f2
/home/cmccabe/Desktop/tetsfolder/f3
/home/cmccabe/Desktop/tetsfolder/list

desired output

f1
f2
f3

Hi,

can you try the below one ?

A="/home/cmccabe/Desktop/tetsfolder/f1"
echo ${A##*/}

Gives desired output

Seems i misinterpret it. If you want to print only directories, following might help:

find /home/cmccabe/Desktop/tetsfolder -type d -exec basename {} \;

ls -dF1 */

Options may be differents depending on your OS (check man ls )

#!/bin/bash
dir="/home/cmccabe/Desktop/tetsfolder/"
cd ${dir}
for file in *
  do  
    echo "$file"
  done > /home/cmccabe/Desktop/tetsfolder/list

Perhaps a neater way if this is available:-

pushd $dir
ls -1
popd

Does this help?

Robin

hi, try:

#!/bin/bash
dir="/home/cmccabe/Desktop/tetsfolder/"
for file in ${dir}/*/
  do
    file="${file%%/}"  
    echo "${file##.*/}"
  done > /home/cmccabe/Desktop/tetsfolder/list

Regards.