Find out whether files exist.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find out whether files exist.
# 1  
Old 06-11-2010
Find out whether files exist.

I have the following data stored in a file.
1 /home/file13 /home/file2
2 /home/file41 /home/file654
3 /home/file61 /home/file45
4 /home/file81 /home/file43
...


I want to print the first column provided the files represented by the second and third column exist.
How to do that?
# 2  
Old 06-11-2010
Hi, try with this:

Code:
while read linea
do
   fich1=$(echo $linea | cut -d" " -f2)
   fich2=$(echo $linea | cut -d" " -f3)

   if [ -e $fich1 ] && [ -r $fich1 ]
   then
      awk '{print $1}' $fich1
   fi
   if [ -e $fich2 ] && [ -r $fich2 ]
   then
      awk '{print $1}' $fich2
   fi

done < IN_FILE


SORRY!

I understood another thing....I have to improve my english.

Last edited by albertogarcia; 06-11-2010 at 04:14 AM.. Reason: I haven't understand the problem!
# 3  
Old 06-11-2010
Please try:

Code:
while read num f1 f2
do
   if [ -s "$f1" ] && [ -s "$f2" ]; then
      echo $num
   fi
done < file

-s will check for non zero size too. if you dont want to do that. just use -f
This User Gave Thanks to clx For This Post:
# 4  
Old 06-11-2010
Thank you, anchal_khare.
The code works fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find command when there exist many files

I want to run find and wondering if it struggles when there are many files. I have tried and does not seem to complain. Is this correct? (8 Replies)
Discussion started by: kristinu
8 Replies

2. UNIX for Beginners Questions & Answers

Check if 10 files exist

Hi All, Whenever i get 10 files(file names like sales*) then another file need to create. May i know how to implement this in KSH. (4 Replies)
Discussion started by: siddireddy
4 Replies

3. Shell Programming and Scripting

Find if a directory exist from a list of the path

Hello, i want to script on sh to check from a path if the directory exist and isn't empty. I explain: path is : /aaa/bbb/ccc/ccc_name/ddd/ Where the cccc_name is in a list, so i think it's $1 My command find -name /aaa/bbb/ccc/$1/ddd/ didn't work because my $1 is the same and not... (5 Replies)
Discussion started by: steiner
5 Replies

4. Shell Programming and Scripting

find + move if destination path does not exist

hi frnds, please help ... what will happen with below command if destination path does not exist on the system.... find /var/adm/cft* -mtime +1 -exec mv {} /global/ \ in unix its remove all my files from the system from soruce file ... how is it possbile (1 Reply)
Discussion started by: dodasajan
1 Replies

5. UNIX for Dummies Questions & Answers

How to find if file exist in directory using *

Hi, I know how to use the test command ( ...) to find a single given name file. However, I have a case in which I have a directory with one file and one sub-directory. I know that the file starts with "fub". The command doesn't work if i call the file "fub*" as it doesn't understand I meant a... (2 Replies)
Discussion started by: buj
2 Replies

6. Shell Programming and Scripting

Delete files if they exist

In a directory a number of files named res0.om res1.om ... resN.om where N can be any unknown number between 1 and 999 Please help me filling out the gaps in the following csh script: I need to delete all files exept res0.om The easy way is rm res1* rm res2* rm res3* rm res4*... (5 Replies)
Discussion started by: pederlol
5 Replies

7. UNIX for Dummies Questions & Answers

testing if files exist

I am trying to test arguments to see if they are files in any directory. I have : but it's not working (7 Replies)
Discussion started by: skooly5
7 Replies

8. Shell Programming and Scripting

perl: When dealing with files that do not exist

I have a process run weekly where I must convert data formats for about thirty files. I read a text file that provides all of the filenames and switch settings. My perl code is: for ($j = 1; $j <= $k; $j++) { open(FIN2,$fin2) || die "open: $!"; do other stuff } Every once in... (2 Replies)
Discussion started by: joeyg
2 Replies

9. UNIX for Dummies Questions & Answers

Find in file. Does this exist under UNIX?

Hello, I have the following problem. I know there is a file somewhere on a UNIX machine that contains a string, but I don't know where. With the "grep" command, I can look into a file but only if I'm located in the correct directory. With the "find" command, I can search across directories... (2 Replies)
Discussion started by: scampsd
2 Replies

10. Shell Programming and Scripting

Can I find wether a particular file exist and size greater than zero...help please

Can I find wether a particular file exist and size greater than zero in one line command. similar to this if && something in one if test .... e.g. if 1.) is it possible ? ... if yes how 2.) what would be the return type in case there is success or failure. I mean if both are... (4 Replies)
Discussion started by: guhas
4 Replies
Login or Register to Ask a Question