Apologies if this is trivial- but how do list just one file in each subdirectory of my root folder? Linux terminal or MS DOS syntax doesn't matter. I would guess it would be an ls or dir command with some parameter but I havent found anything in the manual for either command.

link|improve this question
feedback

3 Answers

If you want to see the directory names use -

ls -R1 | grep -A 1 ":"

If you DON'T want to see the directory names use -

ls -R1 | grep -A 1 ":" | grep -v ":"

link|improve this answer
feedback

Wrote a little script to do this.

#!/bin/bash
for dir in `find . -type d` # Find directories recursively
do
  # cd into directory, quotes are for directory names with spaces
  cd "$dir"             

  # Print out directory name
  echo "In directory $dir:"

  # Force list output to be one entry per line, pipe through inverted grep to 
  # exclude directories, pipe through awk to get the first item. You can specify
  # the number of files you want.     
  ls -p1 | grep -v / | awk -v "num_of_files=1" 'NR<=num_of_files { print $1 }'

  # cd back to root directory
  cd "$OLDPWD"              
done

Output:

In directory .:
test.sh
In directory ./bar:
barfile.txt
In directory ./baz:
bazfile.txt
In directory ./baz/quux:
something.txt
In directory ./foo:
foofile.txt
link|improve this answer
feedback

Following command do:

for d in `find / -maxdepth 1 -mindepth 1 -type d`; do find $d -maxdepth 1 -type f | head -n1 ;done
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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