Sreejin TJ
Sreejin TJ

Reputation: 327

listing files in UNIX owned by a particular user

How do I list the files owned by a particular user in UNIX ?.

If I use ls - l command in a shared directory ,it lists all the files with the details .This shared directory contains many files created by many users in a group and I am in a situation where I want to see the files created only by a particular user. Is there any listing command to give username as the input. Refer below example,

command : ls - l

drwxr-xr-x   2 user_1     main 4.0K    Feb 12 16:43 proj_1
drwxrws---   6 user_2     main  20M    Feb 18 11:07 proj_2
drwxr-xr-x   3 user_1     main 1.3M    Feb 18 00:18 proj_3
drwxrwsr-x   2 user_2     main  8.0K   Dec 27 01:23 proj_4
drwxrwsr-x   2 user_3     main  8.1K   Dec 27 01:23 proj_5

I am looking for a command to display only the files created by the user_2 with my expected output as below ,

   drwxrws---   6 user_2     main  20M    Feb 18 11:07 proj_2
   drwxrwsr-x   2 user_2     main  8.0K   Dec 27 01:23 proj_4

Kindly let me know if there is a way .

Upvotes: 5

Views: 7034

Answers (2)

It should be possible to use awk togheter with ls -l

ls -l | awk '$3=="user_2" { print $0 }'

this will print all lines where third field (user) matches "user_2"

Upvotes: 2

Klaus
Klaus

Reputation: 25593

You simply can use the findcommand like this:

find . -maxdepth 1 -user some_user -exec ls -lsad {} \;

Why the options are used:

  • maxdepth we only want to see current directory level
  • user we only want to see files owned by given user
  • exec lets do something with the found file

What we want do with the file:

ls -lsad gives you the long list of current file, if it is a directory, don't go into it.

Upvotes: 6

Related Questions