Linux: list contents of sub directories with given name?

2

I'm using linux to analyze a windows directory structure. The structure is:

/Documents and settings
  /username1
    /My Documents
    ...
  /username2
    /My Documents
    ...
  ...

What command can I execute so that the contents (and sub folders) of all the "My Documents" directories are listed like:

/Documents and Settings/username1/My Documents/filename
/Documents and Settings/username1/My Documents/subdir/filename
/Documents and Settings/username2/My Documents/filename

Basically there are a ton of users but almost none have anything in their My Documents folder. I just want to find and show the contents of those user's that do have documents.

EDIT: Each "username" directory contains many sub directories. I only want to list the tree below the My Documents folder but do so for all usernames at once.

Dane O'Connor

Posted 2009-09-18T16:09:42.437

Reputation: 1 118

Answers

2

Combining the first two answers, use find and ls -R:

find "/Documents and settings" -name "My Documents" -exec ls -R {} \;

This will find all of the My Documents directories and list everything underneath them, with full pathnames. You need the quotes since the directory names have spaces in them.

Edit: An explination of how this works. It starts at /Documents and settings, looking for any file or directory that matches My Documents. For each one it finds, it substitutes the path for {} in the ls. The \; signifies the end of the command.

KeithB

Posted 2009-09-18T16:09:42.437

Reputation: 8 506

What does {} ; mean? – Dane O'Connor – 2009-09-18T16:34:47.613

that command is throwing: "missing argument to -exec" – Dane O'Connor – 2009-09-18T16:38:47.667

Make sure that you have the ; (backslash semicolon) on there. – KeithB – 2009-09-18T16:45:09.370

Ah, once you explained I noticed I didn't have a space between '{}' and ';'. Perfect. I assume the ';' is to prevent the shell from expanding ';'? – Dane O'Connor – 2009-09-18T16:50:10.373

Shoot, this is listing all the contents of Documents and Settings – Dane O'Connor – 2009-09-18T16:54:17.353

2

Just to add to the collection, ls -R doesn't really produce the nice output so I use tree command instead

find "/Documents and settings" -name "My Documents" -exec tree -f -i --noreport {} \;

iddqd

Posted 2009-09-18T16:09:42.437

Reputation: 196

1

ls -R path_to_your_dir

or

ls -lR path_to_your_dir

for better view

However, best view is achieved with

find path_to_your_dir

NOTE: -R means recursive

linux_is_for_desktop

Posted 2009-09-18T16:09:42.437

Reputation: 678

see my edit, I only need the tree below each my documents folder. Not the tree below Documents and Settings. – Dane O'Connor – 2009-09-18T16:29:43.270

0

You can use the command # find your_dir

lyarwood

Posted 2009-09-18T16:09:42.437

Reputation: 1 804

see my edit, I only need the tree below each my documents folder. Not the tree below Documents and Settings. – Dane O'Connor – 2009-09-18T16:33:24.543

0

If you do want to see the pathname and don't want to have the full recursive -R listing, this seems to do roughly what you want, it's a little clunky though:

find . -type d -name '*Documents' ! -empty | (while read d; do echo $d; ls -la $d; done)

wallenborn

Posted 2009-09-18T16:09:42.437

Reputation: 266

0

If you want the recursive listing of files and folders under Documents and Settings/User/My Documents:

find Documents\ and\ Settings/*/My\ Documents -print

If you only want only the files and folders directly under Documents and Settings/User/My Documents:

ls -dr Documents\ and\ Settings/*/My\ Documents/*

The former will include a file like Documents and Settings/User/My Documents/dir1/file1, whereas the latter will list only Documents and Settings/User/My Documents/dir1

user4358

Posted 2009-09-18T16:09:42.437

Reputation: