LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-07-2020, 05:10 AM   #1
Lassie111
LQ Newbie
 
Registered: Mar 2020
Posts: 4

Rep: Reputation: Disabled
Count the number of objects in the filesystem owned by the current user


I'm trying to count all files/directories in the filesystem owned by the current user.
I tried these 2 commands but they give different result.

Code:
find / -printf "%u\n" 2> /dev/null | grep "^$(whoami)$" | wc -l
Output: 46130

Code:
find / -user "$(whoami)" 2> /dev/null | wc -l
Output: 45944

Why is that?
 
Old 03-07-2020, 05:43 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,153

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Print the data and find out - you obviously know how to format the strings so they can be diff'd.
 
Old 03-07-2020, 05:44 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Example: Two users, jagger and mjagger. If the current user is jagger, the first command counts files owned by mjagger as well.

You could run
Code:
find / -printf "%u %p\n" -a ! -user "$(whoami)" | grep "$(whoami)"
or similar to understand that better.
 
Old 03-07-2020, 05:50 AM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Run both commands again and confirm that the figures are reproducible.

Bear in mind that your filesystem may be changing during the execution of the commands.
 
Old 03-07-2020, 09:38 AM   #5
Lassie111
LQ Newbie
 
Registered: Mar 2020
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
Run both commands again and confirm that the figures are reproducible.

Bear in mind that your filesystem may be changing during the execution of the commands.
Yes, both command's outputs always differ.
 
Old 03-07-2020, 09:39 AM   #6
Lassie111
LQ Newbie
 
Registered: Mar 2020
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
Example: Two users, jagger and mjagger. If the current user is jagger, the first command counts files owned by mjagger as well.

You could run
Code:
find / -printf "%u %p\n" -a ! -user "$(whoami)" | grep "$(whoami)"
or similar to understand that better.
I know that, that's why I used "^$(whoami)$".
 
Old 03-07-2020, 09:45 AM   #7
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by Lassie111 View Post
Yes, both command's outputs always differ.
Yes, but do you get the same figures each time?

If not, try it again with a read-only filesystem.

Last edited by hydrurga; 03-07-2020 at 09:47 AM.
 
Old 03-07-2020, 09:57 AM   #8
Lassie111
LQ Newbie
 
Registered: Mar 2020
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
Yes, but do you get the same figures each time?

If not, try it again with a read-only filesystem.
No, they change.
 
Old 03-07-2020, 10:04 AM   #9
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by Lassie111 View Post
No, they change.
Ok. Boot up into Linux off a live media and mount your drive as read-only. Then try the commands again (ensuring that you use the correct path).
 
Old 03-07-2020, 10:10 AM   #10
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Salix
Posts: 6,150

Rep: Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314
What do you get using the file manager of your desktop?
 
Old 03-07-2020, 01:15 PM   #11
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by Lassie111 View Post
I know that, that's why I used "^$(whoami)$".
You didn't do that in both commands...

Also, as suggested in #2, redirect the output to files and diff the files to see what's different.

Last edited by scasey; 03-07-2020 at 01:17 PM.
 
Old 03-07-2020, 02:25 PM   #12
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,628

Rep: Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557
Quote:
Originally Posted by scasey View Post
You didn't do that in both commands...
The OP did it in the one that matters - running "find -user "^$(whoami)$"" errors because it's not doing a regex match.

Quote:
Also, as suggested in #2, redirect the output to files and diff the files to see what's different.
Yep, that is what the OP needs to do, and then any files not on both sides can be examined to determine the reason why not.

To do that means re-writing the first command to include filename and updating the grep to look at the username part, plus prefixing all lines of the second file with the username - both simple enough operations.


Last edited by boughtonp; 03-07-2020 at 02:32 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Configure sudoers in CentOS 5.10 for a user to run a script owned by another user as that user ripelivejam Linux - Newbie 2 12-22-2015 02:02 PM
count multiple objects in shell script raggmopp1961 Linux - General 1 07-22-2010 08:56 AM
LXer: Java Data Objects and Service Data Objects in SOA LXer Syndicated Linux News 0 01-17-2009 06:10 AM
File is not owned by current user Curtor Linux - Newbie 1 03-26-2008 07:57 AM
Make a file owned by root owned by a user sharpie Linux - Newbie 2 02-26-2004 01:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration