1

I want to send a list of all folders including one level down to a txt file. Excluding some named folders and no files. Imagine a folder structure like this. CAPS are folders.

FOLDER 1

.hidden

TEMP

somefile

=========

=========

FOLDER 2

.hidden

TEMP

DATA1

DATA2

somefile

========

========

FOLDER 3

.hidden

TEMP

DATA1

somefile

I would like to run "insert magic command here" and end up with an output that looks like below

FOLDER 1

FOLDER 2

DATA1

DATA2

FOLDER 3

DATA1
0

I did this to create your folder structure:

for     f in    FOLDER\ 1 FOLDER\ 2 FOLDER\ 3
do      mkdir   -p "$f/TEMP"
        touch   "$f/.hidden" "$f/somefile"
        case    "$f" in
        (*2)    mkdir -p    "$f/DATA1/TEMP" "$f/DATA2/TEMP";;
        (*3)    mkdir -p    "$f/DATA1/TEMP"
        esac
done

That created a tree like:

find .

.
./FOLDER 1
./FOLDER 1/TEMP
./FOLDER 1/.hidden
./FOLDER 1/somefile
./FOLDER 2
./FOLDER 2/TEMP
./FOLDER 2/.hidden
./FOLDER 2/somefile
./FOLDER 2/DATA1
./FOLDER 2/DATA1/TEMP
./FOLDER 2/DATA2
./FOLDER 2/DATA2/TEMP
./FOLDER 3
./FOLDER 3/TEMP
./FOLDER 3/.hidden
./FOLDER 3/somefile
./FOLDER 3/DATA1
./FOLDER 3/DATA1/TEMP

And last I did...

find . ! -path './*/*/*' ! -name TEMP -type d

.
./FOLDER 1
./FOLDER 2
./FOLDER 2/DATA1
./FOLDER 2/DATA2
./FOLDER 3
./FOLDER 3/DATA1
0

Thanks that worked great I forgot that .hidden was a folder as well so added that into the find command. Find was outputting the folders in the order they were added so used the sort command to sort back into alphabetical order. So I ended up with

find . ! -path './*/*/*' ! -name TEMP ! -name .hidden -type d | sort > folderlist.txt

Cleaned up the result a bit with

sed -n -e 's;^.*/;;p'

So it lists as

FOLDER 1

FOLDER 2

DATA1

DATA2

FOLDER 3

DATA1

Finally ended up with this beast of a line

find . ! -path './*/*/*' ! -name TEMP ! -name .hidden -type d | sort | sed -n -e 's;^.*/;;p' > folderlist.txt

Might be messy but hey I'm no expert. Thanks for all the help.

  • If you only want the file names without path, pass -printf "%f\n" to find. No need to use sed. – daniel kullmann Oct 12 '15 at 13:29
  • I did try that but couldn't get it to sort right, probably me being thick. – GTM Oct 12 '15 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.