Best way to only list files that I own
Solution 1
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.)
Solution 2
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.
Solution 3
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!
Related videos on Youtube
James
Updated on September 18, 2022Comments
-
James over 1 year
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?
-
user about 7 years
-
terdon about 7 yearsNot to mention that this will find files named
username
, orfoousername
or owned byusername2
. -
Kusalananda about 7 yearsIt'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. -
drHogan about 7 yearsI agree that this answer is not safe. Anyways, this is what I'm always doing interactively,
ll | grep user
. Most of my interactivegrep
usage is not rock solid but less typing is more important here. -
Kusalananda about 7 yearsAlso, rather than
-exec ls ...
you could use-ls
. -
Arminius about 7 yearsI don't think OP wants to look recursively. Also, your first example repeats file names unnecessarily.