6

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?

3

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.

  • I don't think OP wants to look recursively. Also, your first example repeats file names unnecessarily. – Arminius Apr 12 '17 at 16:52
3

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.)

  • It's better to use $LOGNAME as it's guaranteed to contain the user's login name on all Unices. $USER may not exist everywhere on non-Linux Unices. – Kusalananda Apr 12 '17 at 9:52
  • Also, rather than -exec ls ... you could use -ls. – Kusalananda Apr 12 '17 at 12:23
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)
0

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!

  • You really shouldn't parse the output of ls. – a CVn Apr 12 '17 at 9:19
  • Not to mention that this will find files named username, or foousername or owned by username2. – terdon Apr 12 '17 at 9:27
  • I agree that this answer is not safe. Anyways, this is what I'm always doing interactively, ll | grep user. Most of my interactive grep usage is not rock solid but less typing is more important here. – rudimeier Apr 12 '17 at 10:43

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.