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?
5 Answers
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.)
- 726
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.
- 67,283
- 35
- 116
- 255
- 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
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!
- 898
- 6
- 11
-
-
Not to mention that this will find files named
username, orfoousernameor owned byusername2. – terdon Apr 12 '17 at 09:27 -
I agree that this answer is not safe. Anyways, this is what I'm always doing interactively,
ll | grep user. Most of my interactivegrepusage is not rock solid but less typing is more important here. – rudimeier Apr 12 '17 at 10:43
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.
With zsh, you could do that using the glob qualifier u:
uidfiles 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)
- 82,805
$LOGNAMEas it's guaranteed to contain the user's login name on all Unices.$USERmay not exist everywhere on non-Linux Unices. – Kusalananda Apr 12 '17 at 09:52-exec ls ...you could use-ls. – Kusalananda Apr 12 '17 at 12:23