Finding Files with Perl on a Hidden Dir?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Finding Files with Perl on a Hidden Dir?
# 1  
Old 06-28-2016
Finding Files with Perl on a Hidden Dir?

Greetings!

Been a while since I futzed around with Perl, and came upon a minor headscratcher for the community Smilie

Here's the basic code which I'm trying to make tick over:
Code:
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

print " starting ";
while (-e "~/.somedir/testFile")
{
    print " file detected ";
    sleep 1;
}
print " file missing ";

exit;

Queerly, this will not work on hidden (.somedir) dirs!

So, not being one to give up too easily, I even tried passing a couple bashlines to the system as well. Here are the better substitute "while" statements which I tried:
Code:
while (`wc -l ~/.somedir/testFile | sed -e 's/ \/.*$//'` eq "0")
while (`ls -a ~/.somedir | grep -e \"testFile\"` eq "testFile")

Of course, none of these worked in our Perl context Smilie

Any ideas for a "simple" solution amended from this rag-tag assortment would be quite appreciated!

Thanks again --
# 2  
Old 06-29-2016
I commend you for making use of the pragmas strict, warnings and diagnosis, especially, when you are troubleshooting/learning.
Still, I wish you would had declared your purpose. It is not clear by looking at the posted snippet.
In other words, I cannot infer what you are trying to do by what you have posted. Please, clarify what you are trying to achieve.

exit; is not necessary.
# 3  
Old 06-29-2016
Thank you, Aia, for posting! Sorry for the confusion...

Here's the program flow which I am seeking to achieve with my crude little snippet:
  1. Begin by printing the word "starting" to the terminal.
  2. Check ~/.somedir using while() to see if the file "testFile" is present.
  3. If it is, send the phrase "file detected" to the terminal; and loop back again to check if "testFile" is still present after a 1-second timeout.
  4. If "testFile" doesn't exist on ~/.somedir, print the phrase "file missing" to the terminal and exit.
Not too exciting; but I think it about encapsulates what I need to do Smilie

To this end, I have formed three different while() statements to interchangeably fit into the main script code:
Code:
  1. while (`wc -l ~/.somedir/testFile | sed -e 's/ \/.*$//'` eq "0")
  2. while (`ls -a ~/.somedir | grep -e \"testFile\"` eq "testFile")
  3. while (-e "~/.somedir/testFile")

The commandlines from 1) and 2) both work well as stand-alone code at the terminal; but do not execute properly from Perl. #2 is particularly well-suited to the work; as it doesn't rely upon the file being of any particular size to allow for successful detection. String comparisons should logically work as expected in this context.

Choice #3 is preferred as a straightforward solution; but, unfortunately, Perl will not find our "testFile" on a hidden directory such as ~/.somedir using the normal
Code:
while (-e "~/.somedir/testFile")

statement layout.

So, that's the problem in a nutshell...

Again, I'd prefer going with the straight-Perl approach which #3 offers. But, if making this work is more complicated than getting the commandline in
Code:
while (`ls -a ~/.somedir | grep -e \"testFile\"` eq "testFile")

to function correctly in this code context, I'll settle for a just "quick fix" of #2 for now.

Thanks again!
# 4  
Old 06-29-2016
Quote:
Originally Posted by LinQ
Queerly, this will not work on hidden (.somedir) dirs!
That sounds fishy to me as nothing about UNIX except ls cares about hidden files and folders.

You know what? It doesn't work on non-hidden dirs either. It always fails, because ~/ which is a shell feature not a perl one.

Use an absolute path, or substitute HOME a la $ENV{"HOME"}."/folder/file" and -e works fine.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 06-29-2016
There is no problems for Perl using the hidden directories and files, the issue is with ~. It does not translate to home like in the shell.
Please, try:
Code:
while ( -e "$ENV{'HOME'}/.somedir/testFile" )
{
    $| = 1; # to flush the print without the newline
    print " file detected ";
    sleep 1;
}

This User Gave Thanks to Aia For This Post:
# 6  
Old 06-30-2016
Thank you both so much for the input! The Perl code seems to work well now; and I picked up yet another kernel of savvy Smilie

One last question, if folks have a moment.

Not furnished with network access yesterday afternoon, I fiddled for a while and found out why
Code:
while (`ls -a ~/.somedir | grep -e \"testFile\"` eq "testFile")

from the OP didn't work either.

Seems as though an unidentified character is being "automagically" appended to the "testFile" string which is returned to Perl. The following updated line works, but it seems a bit "sloppy" to allow anything aside from a neat eq operation with a string comparison operation like this:
Code:
while (`ls -a ~/.somedir | grep -s -e \"testFile\"` ge "testFile")

In the spirit of learning new stuff, is there a way of cleaning up this output with a bit of additional inline instruction to allow something like
Code:
while (`ls -a ~/.somedir | grep -s -e \"testFile\"` eq "testFile")

to actually work as anticipated?

So much to learn...

Thanks again; and have a great day Smilie
# 7  
Old 06-30-2016
Quote:
Thank you both so much for the input! The Perl code seems to work well now; and I picked up yet another kernel of savvy Linux

One last question, if folks have a moment.

Not furnished with network access yesterday afternoon, I fiddled for a while and found out why
Code:

Code:
while (`ls -a ~/.somedir | grep -e \"testFile\"` eq "testFile")


from the OP didn't work either.

Seems as though an unidentified character is being "automagically" appended to the "testFile" string which is returned to Perl. The following updated line works, but it seems a bit "sloppy" to allow anything aside from a neat eq operation with a string comparison operation like this:
Code:

Code:
while (`ls -a ~/.somedir | grep -s -e \"testFile\"` ge "testFile")


In the spirit of learning new stuff, is there a way of cleaning up this output with a bit of additional inline instruction to allow something like
Code:

Code:
while (`ls -a ~/.somedir | grep -s -e \"testFile\"` eq "testFile")


to actually work as anticipated?

So much to learn...

Thanks again; and have a great day Linux
I get that you are learning or trying to learn. Nevertheless, I question the desire to learn something you will not have any use for it, if you wanted to do it correctly.

Calling two different system programs to pipe them together in order to evaluate the resulted output would be the hallmark of someone that probably needs to do things in a shell and not with Perl. Not considering, that what you are accepting as a valid comparison is not.

Addressing the highlighted red part of the quote: Is it not an `unidentified' character. It's a \n from running the command ls -a

Last edited by Aia; 07-01-2016 at 11:29 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rsync - how to copy hidden folder or hidden files when using full path

Hello. I use this command : rsync -av --include=".*" --dry-run "$A_FULL_PATH_S" "$A_FULL_PATH_D"The data comes from the output of a find command. And no full source directories are in use, only some files. Source example... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

PERL - Copying ONLY files from one dir to another

I'm writing a Perl script which has its 1st step as to copy files from one directory to another directory. The Source directory has got files with extension, without extension, directories etc. But I want to copy ONLY files with no extension. The files with extensions and directories should not get... (2 Replies)
Discussion started by: jhamaks
2 Replies

3. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

4. Cybersecurity

Inquiry of .awo hidden dir with bin file inside .eeesync extension

Hi, Me i ask if someone knows about this hidden directory or it me knows where this dir associated with or in a program. I had and notices this .awo dir with bin files inside title 6770669_info.eeesync files in my directory. I wonder if this is associated with my backup program or any program... (0 Replies)
Discussion started by: jao_madn
0 Replies

5. Shell Programming and Scripting

Finding perl files without documentation

I have an application consisting of a number of perl files. I want to find those perl files that have no documentation yet, so I tried the following from the root level of the directory where the application resides: perldoc -r * The output is something like the following: No documentation found... (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

Issue with CD to a hidden dir

I have written a script to secure delete all files in a quarantine folder and my Trash folder. All the commands run trough Terminal via the script. The problem I am having is when I am changing the directory to the hidden Trash folder. When I do cd /Users/WatsonN/.Trash all it does is take it to... (2 Replies)
Discussion started by: WatsonN
2 Replies

7. Shell Programming and Scripting

PERL count files in a dir

Hi Guys, I need to count files in a dir which were updated yesterday. ls -lth | grep -i 'Jul 7' | wc -l The dir holds files of last 15 days and total count is as 2067476. Is it efficient to count the files using perl? I have developed the following perl script making use of system(). Can... (3 Replies)
Discussion started by: Asteroid
3 Replies

8. Shell Programming and Scripting

Finding Hidden files and protecting the folder containing hidden files from deletion

Hi. I have a script which is deleting files with a particular extension and older than 45 days.The code is: find <path> -name "<filename_pattern>" -mtime +45 -exec rm {} \; But the problem is that some important files are also getting deleted.To prevent this I have decide to make a dummy... (4 Replies)
Discussion started by: pochaw
4 Replies

9. UNIX for Dummies Questions & Answers

Finding hidden files under mounted filesystems

I have never heard of this before but someone at work here says there is a command to find files that are under currently mounted filesystems. Does anyone know what this command is and is it available on HP-UX? (3 Replies)
Discussion started by: keelba
3 Replies
Login or Register to Ask a Question