folder existing and file existing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting folder existing and file existing
# 1  
Old 08-28-2008
folder existing and file existing

I want to look into a folder to see if there are any folders within it. If there are, I need to check inside each folder to see if it contains a .pdf file

So

If /myserver/myfolder/
contains a folder AND that folder conatins a .pdf file
do X
Else
do Z

I may have multiple folders and multiple .pdf files under myfolder. I don't know ahead of time what the folder should be called to do a test. I don't care about the folder name. I don't know what the pdf should be named ahead of time to do a test either. I just care that something ending in .pdf is in the folder under myfolder.
# 2  
Old 08-28-2008
So for each subdirectory, if the subdirectory contains a PDF file, do X, else do Y. What if there are multiple PDF files in a directory? The following will loop over them.

Code:
set -o nullglob
for f in /myserver/myfolder/*/; do
  pdf=false
  for p in "$f"/*.pdf; do
    X
    pdf=true
  done
  if ! $pdf; then
    Z
  fi
done

# 3  
Old 08-28-2008
It is ok if a subdirectory contains multiple pdf files. It only needs to have at least one. If I have NO subdirectory OR any subdirectory withOUT a pdf, that should arrive at the same Z error.
# 4  
Old 08-28-2008
Thanks for your help! Is that something I put into a script or put into a C program that I can call from the script. Forgive my newbie question.
# 5  
Old 08-28-2008
That's a script, but it doesn't really conform to your requirements. Specifically, it ignores the case when there is no subdirectory.

The following is a bit contorted but should perhaps work.

Code:
#!/bin/sh
set -o nullglob
pdf=false
for f in /myserver/myfolder/*; do
  test -d "$f" || continue
  for p in "$f"/*.pdf; do
    pdf=true
    X
    break
  done
  $pdf || break
done
$pdf || Z


Last edited by era; 08-28-2008 at 07:10 PM.. Reason: Changed to cope correctly with "any subdirectory without a PDF" requirement
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting a new line into an already existing file

hello .. I am new to perl scripting, I have a text file, and would like to insert 3 new lines into the same file at different line numbers using perl scripting. Any Help regarding this will be very useful. say the file is sample.txt, contents are aaaaa bbbb ccccc dddd eeeee ffffffff... (4 Replies)
Discussion started by: hemalathak10
4 Replies

2. UNIX for Dummies Questions & Answers

Appending lines from an existing list to each line in another existing list

Evening all ! I would like to ask your expertise on how to accomplish the following ; I have 2 lists, and would like each line from list2 to be appended to each line in list1, resulting in list3 ; List1; alpha beta charlie List2; one two three (4 Replies)
Discussion started by: TAPE
4 Replies

3. UNIX for Advanced & Expert Users

how to put a .gz file in already existing archive folder

Hi.. I have a folder within which I have two files : abc.gz and xyz.tar.gz Actualy abc was a 8 GB File and while zipping other files in xyz.tar.gz it could not be zipped due to its large size.. I have zipped abc using -E option of tar. Can someone help to put abc.gz in xyz.tar.gz..... (1 Reply)
Discussion started by: kanus
1 Replies

4. Infrastructure Monitoring

modifying existing file using C

Hi all, I have a snmpd.conf file as below. in "SECTION: Trap Destinations" line I want to add "trap2dest <IP>:162 <com_str>" on a new line. For this I wrote following code #include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp; ssize_t read_char_count = 0; ... (2 Replies)
Discussion started by: zing_foru
2 Replies

5. Shell Programming and Scripting

insert pipes for existing and non-existing records

I have a source file like this, L4058S462 34329094 F51010141TK1070000483L4058S462 34329094 0232384840 381892 182 5690 L4058S462 34329094 F51020141FIRST CLEARING, LLC A/C 3432-9094 L4058S462 34329094 F51030141JOHAN HOLMQVIST ... (1 Reply)
Discussion started by: saravanamr
1 Replies

6. Solaris

Add existing user into an existing group

Pre: no gpasswd/adduser there is just usermod can be used, also there is no -a option for usermod. How should I add a user into a group? (4 Replies)
Discussion started by: a2156z
4 Replies

7. Shell Programming and Scripting

how to add a new column in an existing file

Hi guys, Please help me if u have some solution. I have a file with three columns separated by ':' - INPUT_FILE C416722_2 : calin Dirigent : Dirigent AC4174_6 : Jac : cal_co TC4260_5 : [no : lin kite BC426302_1 : [no : calin Dirigent lin JC426540_3 : lin Pymo_bin : calin TC428_3 : no7... (4 Replies)
Discussion started by: sam_2921
4 Replies

8. Shell Programming and Scripting

How to copy one folder to another with existing files

For example, /tmp/folder1 includes /tmp/folder1/a /tmp/folder1/b /tmp/folder2 includes /tmp/c Is there a command without removing files in /tmp/folder2 first to copy the /tmp/folder1 to /tmp/folder2? and the result should be /tmp/folder2 will include only /tmp/folder2/a... (2 Replies)
Discussion started by: lalelle
2 Replies

9. UNIX for Dummies Questions & Answers

how can i change the date of an existing file

Hi, Plz suggest me how can i change the date of a file. Suppose my file has been created in some date and i want to give it present date. How can i do this???? (2 Replies)
Discussion started by: adityam
2 Replies

10. Shell Programming and Scripting

if test for empty and non-existing file

How to write this condition in ksh? if myfile is empty or myfile does not exist then do action1 fi is this OK? if ] -o ] then then do action1 fi Thanks. (3 Replies)
Discussion started by: GNMIKE
3 Replies
Login or Register to Ask a Question