15

What would be the best shell command "one liner" that I could use to list all of the files in a directory, only showing those that I own?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
James
  • 151

5 Answers5

15

A short one-liner would be:

find . -maxdepth 1 -user $USER

If you're looking in the current directory, you can omit the .. If you don't know whether $USER is available, you can replace it with $LOGNAME or $(whoami).

Add -ls to show file details, e.g.:

find / -maxdepth 1 -user root -ls

If you want to supply custom flags to ls you can use it via -exec:

find / -maxdepth 1 -user root -exec ls -ld {} +

(In that case the -d flag to ls is required to list directories as themselves and not their content.)

Arminius
  • 726
4

Use below command

[username@localhost~]$ find / -user username -exec ls -l {} \; 2>/dev/null

find all file in the whole system owned by username. If you find from specific directory just replace the location / .

[username@localhost~]$ find /path/of/direcotry -user username -exec ls -l {} \; 2>/dev/null

NB:2>/dev/null nullify the error output.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Rakib
  • 2,435
  • I don't think OP wants to look recursively. Also, your first example repeats file names unnecessarily. – Arminius Apr 12 '17 at 16:52
1

Since you did not specify the format of the output, you could also do this with ls and grep:

ls -lG | grep username

First we use ls with the -l parameter, to get the listing which includes username and groupname.

Then we prune the groupname from the result with the -G parameter.

After that, we simply pipe it to grep and get all of the results with the desired username.

EDIT: As pointed out in the comments, this is not a safe or bulletproof solution - however, depending on your circumstances, it might be a quick & dirty one. Interactively, it might be acceptable, but you should not use it in any scripts!

user6075516
  • 898
  • 6
  • 11
0
for i in ./*; do [ -f "$i" ] && [ -O "$i" ] && echo "$i"; done

find . ! -name . -prune -user "$(who -m)" -type f

Are the two ways that Will list out plain files in the current directory that you own.

0

With zsh, you could do that using the glob qualifier u:

uid

files owned by user ID id if that is a number. Otherwise, id specifies a user name: the character after the 'u' will be taken as a separator and the string between it and the next matching separator will be taken as a user name. The starting separators '[', '{', and '<' match the final separators ']', '}', and '>', respectively; any other character matches itself. The selected files are those owned by this user. For example, 'u:foo:' or 'u[foo]' selects files owned by user 'foo'.

so e.g. with user ID

print -rl ./*(u1027)

or with user name

print -rl ./*(u_yourusernamegoeshere_)

As usual, you can combine it with other qualifiers, modifiers etc e.g. to list only regular files, hidden or not, owned by user with UID 1027 and sorted by their mtime:

print -rl ./*(.Domu1027)
don_crissti
  • 82,805