LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-17-2008, 03:15 AM   #1
aarontwc
Member
 
Registered: Oct 2008
Posts: 66

Rep: Reputation: 15
Obtaining list of files in all users directories


Let's say i have users: "aaron", "john", "peter" etc on a server, within which they have the file path "~/home/user/.prog/instances" individually.

As root, i am trying to run a cron job so that it reads all the files within "~/home/user/.prog/instances" and does something...

The question is, how do i obtain the list of files inside directory .prog/instances for all users?

I am certain this should be fairly simple, but I just can't find anything like this on Google

Aaron
 
Old 12-17-2008, 03:45 AM   #2
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Code:
for file in $(find /home/*/.prog/instances -type f -print)
do
    # something
    sed -i 's|foo|bar|g' $file
done
 
Old 12-17-2008, 04:06 AM   #3
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Or maybe

find /home -type f -wholename '/home/*/.prog/instances/*' -exec dosomething \;

Last edited by billymayday; 12-17-2008 at 04:07 AM.
 
Old 12-18-2008, 09:23 PM   #4
aarontwc
Member
 
Registered: Oct 2008
Posts: 66

Original Poster
Rep: Reputation: 15
Hi there,

I am doing it like this:

Code:
my $cmd = "find /home/*/.prog/instances -type f -printf '%f\n' |";
open (MYFILES, $cmd) or die "Directory does not exist";
my @myFiles = <MYFILES>;
close MYFILES;
But, how do I create the directory if it does not exist?

I suppose the code is going to be:
Code:
if (!(-e $progDir)) {
    if (!mkdir($progDir,0755)) {
	die "$prog: Could not create $progDir.\n";
    }
}
How do i include that into the earlier code? So that I check for the directory, and if it does not exist, create it.

Thanks!!
Aaron
 
Old 12-21-2008, 08:08 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Looks like you want to sweep the dirs in Perl, so use opendir() http://perldoc.perl.org/functions/opendir.html.
Actually, Id start with the list of users and check '-e $wanted_dir', then opendir if exists, else mkdir...
 
  


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
Howto? List all files in all directories ordered by size? rusty_turkey Linux - Newbie 2 06-17-2008 07:55 PM
users accessing directories,files using php scripts ckob Linux - Security 3 03-14-2008 08:06 AM
List all files in multiple directories haiders Linux - Newbie 6 05-10-2007 12:05 PM
Getting a list of directories with certain files inside... Banacek Linux - Newbie 7 11-27-2006 05:43 PM
List all files and recursively open directories. unreal128 Linux - General 2 07-16-2005 02:06 PM

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

All times are GMT -5. The time now is 09:01 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