LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-28-2012, 05:53 AM   #1
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Rep: Reputation: Disabled
Question Help Urgent With Find Command!!


hi
I used find command to find some file names as per input from user. I used it for current directory. It was working fine. Now I tried with giving some other directory path. Its giving issues.
Here what I tried. Script will take input from user say 1_abc.txt, find the file and print list. if files are not there it will print No Files with this name message.
Finding files in the current Directory.
Code:
echo View Log files;    
echo Enter Log file name;
read -r filename ; find  "$filename"_*.* -type f ! -name ".*" 2>errorlog 
if [ -s errorlog ] ; then echo "No Files with this name"; fi 
echo Press Enter; 
read x;;
Then I tried with some different path, as:
Code:
echo View Log files;    
echo Enter Log file name;
read -r filename ; find /backup/test1/LogFiles/ "$filename"_*.* -type f ! -name ".*" 2>errorlog | awk -F/ '{print $NF}'
if [ -s errorlog ] ; then echo "No Files with this name"; fi 
echo Press Enter; 
read x;;
Now what is the problem with above code.
1. Its printing files names with fullpath i.e., /backup/test1/LogFiles/1_abc.txt
This is not required. I need to print only file names.
2. The if statement is always in execution. Its always printing the "No Files with this name" statement.
Simply its not working. I tried to use find with -P -H options but not of use.

Urgent help is needed.
Please help me to make it work.
Thanks,
 
Old 05-28-2012, 06:49 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
sukhdip;

You've been here long enough to know not to put "urgent" in posts---but you DO have a good record of saying "please" and "thank you"---

I looked thru the man page for "find", and I did not find a way to tell it not to print the full pathname (It might be there--I just ran out of patience). The easy way out:
Code:
find <location> -name "<search string>" | sed 's#.*/##'
the sed statement simply strips off everything up to and including the last "/"

As for the test statement--I'd start by adding a diagnostice statement that prints out what is actually getting written to "errorlog"
 
Old 05-28-2012, 06:59 AM   #3
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
sukhdip;

You've been here long enough to know not to put "urgent" in posts---but you DO have a good record of saying "please" and "thank you"---

I looked thru the man page for "find", and I did not find a way to tell it not to print the full pathname (It might be there--I just ran out of patience). The easy way out:
Code:
find <location> -name "<search string>" | sed 's#.*/##'
the sed statement simply strips off everything up to and including the last "/"

As for the test statement--I'd start by adding a diagnostice statement that prints out what is actually getting written to "errorlog"
@pixellany thanks, I will look forward in future NEVER to use "urgent" in posts.
I will try with your given method. I will post the result after that.
 
Old 05-28-2012, 07:58 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Running find with -printf "%f\n" will display bare file names.
 
Old 05-28-2012, 10:58 AM   #5
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,064

Rep: Reputation: 894Reputation: 894Reputation: 894Reputation: 894Reputation: 894Reputation: 894Reputation: 894
Quote:
Originally Posted by pixellany View Post
The easy way out:
Code:
find <location> -name "<search string>" | sed 's#.*/##'
the sed statement simply strips off everything up to and including the last "/"
Ugh! I'm sorry to do this, but shouldn't you use the 'basename' command for this, rather than sed? It looks as if this is exactly what basename was created for. Mind you, unSpawn's suggestion is really the right one.
 
Old 05-28-2012, 11:12 AM   #6
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,898

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
2. The if statement is always in execution. Its always printing the "No Files with this name" statement.
When find searches through directories/subdirectories which do not contain matching filenames, the if condition is true, and should print the error message. The if condition is false only when find searches through the directory/subdirectory which contains a matching filename.
 
Old 05-28-2012, 11:38 AM   #7
sukhdip
Member
 
Registered: Sep 2011
Posts: 55

Original Poster
Rep: Reputation: Disabled
Hi everybody thanks for your help.
I solved it.
Code:
find /backup/test1/LogFiles/$filename"_"*.* ........
It worked as per my requirements.
 
Old 05-28-2012, 03:14 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by salasi View Post
Ugh! I'm sorry to do this, but shouldn't you use the 'basename' command for this, rather than sed? It looks as if this is exactly what basename was created for. Mind you, unSpawn's suggestion is really the right one.
Touche!!---I know sed and I don't know basename.....back to the showers for me....

sometimes I follow the rule we used in our early 60s motorcycle shop: "If it works, it's OK". Trust me, when dealing with British bikes from the 40s and 50s, we NEEDED that rule.....
 
  


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
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 03:14 AM
URGENT: Help for Find and Replace with AWK onacorpuscle Linux - Newbie 3 12-07-2007 12:22 PM
URGENT:Can not find root system manolakis Debian 5 09-28-2007 12:27 PM
Not Very Urgent, Just can't find a website! bob3dan General 3 02-09-2007 07:17 PM
URGENT-BIND can't find... xailer Linux - Newbie 4 11-30-2003 06:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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