LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-27-2011, 06:34 AM   #1
ajeesh.tr
Member
 
Registered: Jan 2011
Posts: 37

Rep: Reputation: 0
Question given a file path search for file exist


I have file which consists of many files with their path,i need to check for the file exists in the given path,how to check???
 
Old 01-27-2011, 06:36 AM   #2
prodev05
Member
 
Registered: Jul 2009
Location: Planet Earth
Distribution: Unix & Linux Variants
Posts: 304

Rep: Reputation: 20
Try this.

1.) find <your folder path> -name filename // should be your file name
2.) find <your folder path> -name *.txt // should be your extension
3.) find <your folder path> -name ?*.txt // you don't know the file name but you know your file extension.
 
Old 01-27-2011, 06:47 AM   #3
ajeesh.tr
Member
 
Registered: Jan 2011
Posts: 37

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by prodev05 View Post
Try this.

1.) find <your folder path> -name filename // should be your file name
2.) find <your folder path> -name *.txt // should be your extension
3.) find <your folder path> -name ?*.txt // you don't know the file name but you know your file extension.
Actually i want wite a code where it will print out a msg like,file found else file not found
 
Old 01-27-2011, 06:49 AM   #4
prodev05
Member
 
Registered: Jul 2009
Location: Planet Earth
Distribution: Unix & Linux Variants
Posts: 304

Rep: Reputation: 20
Sorry. Understood mistakenly
 
Old 01-27-2011, 06:53 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,492

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
How about something simple like a while loop and test for file exists?
Code:
#!/bin/bash

while read -r file_n_path
do
    if [[ -f $file_n_path ]]
    then
        echo yay the file $file_n_path exists
    else
        echo booo it wasn't there :(
    fi
done<your_file_with_info_in
 
Old 01-27-2011, 06:57 AM   #6
prodev05
Member
 
Registered: Jul 2009
Location: Planet Earth
Distribution: Unix & Linux Variants
Posts: 304

Rep: Reputation: 20
Small snippet for you.

#!/bin/bash
#Filename: Filesearch

echo "Enter the file name to find: "
read FILE_NAME
echo "Enter the path to be searched: "
read PATH_SEARCH
find $PATH_SEARCH -name $FILE_NAME
 
Old 01-27-2011, 07:06 AM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,712
Blog Entries: 27

Rep: Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055Reputation: 2055
Hi,

Quote:
Originally Posted by ajeesh.tr View Post
Actually i want wite a code where it will print out a msg like,file found else file not found
What have you done to find a solution to the problem? Other than to post here.

Sure does look like Homework to me!
 
Old 01-27-2011, 11:12 PM   #8
ajeesh.tr
Member
 
Registered: Jan 2011
Posts: 37

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by onebuck View Post
Hi,


What have you done to find a solution to the problem? Other than to post here.

Sure does look like Homework to me!
I tried executing the code given below but it is not taking the file path.
Code:
#!/usr/bin/env python

import os
from os.path import exists,join,abspath
from os import pathsep
from string import split

def search_file(filename, search_path):
   file_found = 0
   paths = string.split(search_path, pathsep)
   for path in paths:
      if exists(join(path, filename)):
          file_found = 1
          break
   if file_found:
      return abspath(join(path, filename))
   else:
      return None

if __name__ == '___main__':
   search_path = '/home/sam/file.txt'
   find_file = search_file('ls',search_path)
   if find_file:
      print "File found at %s" % find_file
   else:
      print "File not found"

Last edited by ajeesh.tr; 01-28-2011 at 12:18 AM. Reason: making the code readable
 
Old 01-27-2011, 11:52 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,492

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Firstly, please use [code][/code] tags so we can read your code easier. This would also help with something like python that has strict indenting rules.

With respect to the following:
Quote:
search_path = '/home/sam/file.txt'
Is this an incorrect variable name? This is not a path to search but rather a path directly to a single file.
 
Old 01-28-2011, 12:10 AM   #10
ajeesh.tr
Member
 
Registered: Jan 2011
Posts: 37

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by grail View Post
Firstly, please use [code][/code] tags so we can read your code easier. This would also help with something like python that has strict indenting rules.

With respect to the following:

Is this an incorrect variable name? This is not a path to search but rather a path directly to a single file.
Ya it is a path to single file,and i want to check whether the file is existing in that path..
 
Old 01-28-2011, 12:16 AM   #11
ajeesh.tr
Member
 
Registered: Jan 2011
Posts: 37

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Firstly, please use [code][/code] tags so we can read your code easier. This would also help with something like python that has strict indenting rules.

With respect to the following:

Is this an incorrect variable name? This is not a path to search but rather a path directly to a single file.
Code:
#!/usr/bin/env python

import os
from os.path import exists,join,abspath
from os import pathsep
from string import split

def search_file(filename, search_path):
   file_found = 0
   paths = string.split(search_path, pathsep)
   for path in paths:
      if exists(join(path, filename)):
          file_found = 1
          break
   if file_found:
      return abspath(join(path, filename))
   else:
      return None

if __name__ == '___main__':
   search_path = '/home/sam/file.txt'
   find_file = search_file('ls',search_path)
   if find_file:
      print 'File found at %s' % find_file
   else:
      print "File not found"
 
Old 01-28-2011, 01:57 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,492

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Quote:
Ya it is a path to single file
If this is the case and your function requires the following:
Code:
def search_file(filename, search_path):
Then why are you passing the following to it?
Code:
find_file = search_file('ls',search_path)
Here the filename is 'ls' and the path you want to search is '/home/sam/file.txt'.

So are you saying there exists a file called 'ls' located somewhere in this path?
 
Old 01-28-2011, 07:18 AM   #13
ajeesh.tr
Member
 
Registered: Jan 2011
Posts: 37

Original Poster
Rep: Reputation: 0
Lightbulb

Quote:
Originally Posted by ajeesh.tr View Post
I have file which consists of many files with their path,i need to check for the file exists in the given path,how to check???
I got a solution

Code:
#!/usr/bin/env python

import os.path
file = open('Output.txt','r')

while True:

        search_file = file.readline()
        if not search_file:
                break

  
        else:
                search_file = search_file.rstrip("\n")

                if os.path.isfile(search_file):
                        print search_file, 'file found'
                else:
                        print search_file, 'file not found'
file.close()
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Move a file to some path and create that path when it doesn't exist WhisperiN Linux - Newbie 6 10-09-2010 11:47 AM
Need to find if a matching file exist from a list of possible file names wit_273 Linux - General 5 10-25-2007 09:47 AM
Can I Reset Xserver Library File Search Path? Daniel N. Santiago Linux - Newbie 1 08-18-2006 12:45 PM
[SOLVED] latex, search path for style file. kaz2100 Linux - Software 8 08-15-2006 02:18 PM
trying to redirect text to a file to cat at later point. says file doesn't exist. dr_zayus69 Programming 1 10-02-2005 08:10 AM


All times are GMT -5. The time now is 09:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration