LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2009, 09:03 PM   #31
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15

I want to see who owns more files me or root? I do not want to include directories in my search. I want it to output two lines...root (# of files) myusername (# of Files). What command tells who owns a file? and where is a good place to start?

I am thinking find \ -name root ... but I do not know how to start from the root in the file tree and I do not know how to count them.

Thank you
 
Old 03-26-2009, 09:37 PM   #32
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
I want to see who owns more files me or root? I do not want to include directories in my search. I want it to output two lines...root (# of files) myusername (# of Files). What command tells who owns a file? and where is a good place to start?

I am trying unix command: find -user but I do not know how to count them and do both my files and root at the same time. I also want to do others can I check them If I am not the owner?

Thank you
 
Old 03-26-2009, 11:42 PM   #33
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by circuit_girl View Post
I want to see who owns more files me or root? I do not want to include directories in my search. I want it to output two lines...root (# of files) myusername (# of Files). What command tells who owns a file? and where is a good place to start?

I am trying unix command: find -user but I do not know how to count them and do both my files and root at the same time. I also want to do others can I check them If I am not the owner?

Thank you
Why ? I.e. what bad is going to happen if you first find files owned by root and then by a non-root user ?
 
Old 03-27-2009, 12:48 AM   #34
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
TB0ne
thank you so much it has lead me to many other things I wanted to do. Thank you
Circuit_Girl
 
Old 03-27-2009, 01:00 AM   #35
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
Code:
$root_str = `find / -user root | wc -l`;
I am trying to experiment I am doing this but I want to leave out directories in the count and block devices...I do not want the count to include /dev and /proc. I have been experimenting with -prune but can not get it to work.
 
Old 03-29-2009, 11:56 AM   #36
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by circuit_girl View Post
Code:
$root_str = `find / -user root | wc -l`;
I am trying to experiment I am doing this but I want to leave out directories in the count and block devices...I do not want the count to include /dev and /proc. I have been experimenting with -prune but can not get it to work.
Try this:

Code:
$root_str = `find / -user root -type f | grep -v "/dev" | grep -v "/proc" | wc -l`;
Very dirty, with the double-greps. Check the MAN pages on the find command, it will explain what the -type option does. Also pay particular attention to the -iregex flag. Regular expressions are VERY handy to know, and are useful not only in Perl, but alot of 'regular' Linux commands. The regex can be used to filter out the things you don't want to see....
 
Old 03-29-2009, 01:04 PM   #37
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by TB0ne View Post
Try this:

Code:
$root_str = `find / -user root -type f | grep -v "/dev" | grep -v "/proc" | wc -l`;
Very dirty, with the double-greps. Check the MAN pages on the find command, it will explain what the -type option does. Also pay particular attention to the -iregex flag. Regular expressions are VERY handy to know, and are useful not only in Perl, but alot of 'regular' Linux commands. The regex can be used to filter out the things you don't want to see....
Not precise enough - think of

Code:
foo/devwhatever
bar/procwhatever
.
 
Old 03-29-2009, 01:07 PM   #38
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Sergei Steshenko View Post
Not precise enough - think of

Code:
foo/devwhatever
bar/procwhatever
.
Right...which is why I said "Very dirty", and to look up how to use REGEX'es, which is the best way to do it.
 
Old 03-29-2009, 02:24 PM   #39
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
So,

... | grep -v '^/dev/' | ...

will only identify "/dev/" at the beginning of a line.
 
Old 03-29-2009, 05:24 PM   #40
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by choogendyk View Post
So,

... | grep -v '^/dev/' | ...

will only identify "/dev/" at the beginning of a line.
Yep...but you spoiled the hours of fun "regex" learning, though.
 
Old 04-01-2009, 09:55 PM   #41
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
I am working on a new script that compares two files. The two files are list of users from two different groups. The scripts should look for common userids and print out one per line the userids common in the two files as well as the user numbers for those id's in each list.

The files will be in this format below...

file1:

Code:
jdoe:x:102:1001:John Doe:/home/jdoe:/usr/bin/tcsh
apache:x:103:60::/usr/local/apache2:
cmadeup:x:109:1001:Chris Madeup:/home/cmadeup:/usr/bin/bash
file2:
Code:
jdoe:x:107:1001:John Doe:/home/jdoe:/usr/bin/tcsh
apache:x:210:60::/usr/local/apache2:
asomthing:x:115:1001:Amy Somthing:/home/asomething:/usr/bin/bash
The output should find user names that are similar between the two files and print them in an output to the screen. Output two other columns (machine number of the first file) (machine number of second file). So the script would find jdoe and apache similar and print the 3rd field in each file.
Something like....
Code:
Userid Machine1 Machine2
jdoe   102      107
apache 103      210
Any help I would greatly appreciate..Super stumped.
Thank you
 
Old 04-01-2009, 10:18 PM   #42
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you have some knowledge in Perl, why don't you try doing that with Perl. then if you have problems still, post here.
 
Old 04-01-2009, 10:22 PM   #43
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by circuit_girl View Post
I am working on a new script that compares two files. The two files are list of users from two different groups. The scripts should look for common userids and print out one per line the userids common in the two files as well as the user numbers for those id's in each list.

Any help I would greatly appreciate..Super stumped.
Thank you

Sticking with Perl, you need to check out the Array::Compare and List::MoreUtils:airwise modules from CPAN. Map will also work, and will probably prove to be much faster as well, although with smaller files, the different will be negligible.

Some examples:
Code:
use List::MoreUtils q=pairwise=;
my @a = ( 1, 2, 4, 5, 3 );
my @b = ( 1, 2, 4, 2, 3 );
my @c = pairwise { $a == $b ? 1 : 0 } @a, @b;
print "@c";
This assumes the input is two arrays, "1,2,4,5,3" and "1,2,4,2,3". If they match, print a 1. If they don't, print a 0. Output will be "1 1 1 0 1" Define your first file as array @a, second as @b. A quick bit of research will tell you how to get a file into an array..don't want to spoil all the fun. And if you look on CPAN for that module, you'll fine very complete notes on it, and how it's chewing things up on the @c line.

The built-in map function works too:

Code:
my @c = map { $a[$_] == $b[$_] ? 1 : 0 } 0 .. $#a;
If you use map, you can omit the "use List..." line. From the notes on map:

Quote:
Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value composed of the results of each such evaluation. Evaluates BLOCK or EXPR in a list context, so each element of LIST may produce zero, one, or more elements in the returned value
Enjoy.
 
Old 04-02-2009, 09:47 PM   #44
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
I do not understand the examples listed. I am trying to find the users that are common in two files (these files are the password files) with a machine number that is the third field on a specific line. I only want to print the users that are in both list.
 
Old 04-03-2009, 09:36 AM   #45
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by circuit_girl View Post
I do not understand the examples listed. I am trying to find the users that are common in two files (these files are the password files) with a machine number that is the third field on a specific line. I only want to print the users that are in both list.
Right. The example above takes two arrays, and compares them, which is what you want to do...print a 1 if the array elements are the same, a 0 if not. That's easily modified to print whatever, if whatever element of the array, matches an element of the other array. Check out the modules I mentioned...they're worth playing with.

However, you COULD do this with shell commands too:
Code:
$ sort -k 1,1n file1 > file1.sorted
$ sort -k 1,1n file2 > file2.sorted
$ join -j 1 file1.sorted file2.sorted > same.names
Check the man pages for sort and join.
 
  


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
i need to write a script that automatically logins venix Linux - Newbie 3 03-03-2009 01:34 PM
Strange remote logins after starting a gnome session Clemente Linux - Desktop 2 11-11-2008 02:14 PM
Looking for software to track logins and commands with auto-email DukeLeto Linux - Software 1 07-20-2007 05:14 PM
scripting the unix command script lmcthbe Linux - General 7 05-28-2003 02:49 PM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 06:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:45 AM.

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