LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 04: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, 05:49 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 05: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, 06:58 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

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

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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, 10:12 AM   #6
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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, 10: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, 02:14 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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



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 02:14 AM
URGENT: Help for Find and Replace with AWK onacorpuscle Linux - Newbie 3 12-07-2007 11:22 AM
URGENT:Can not find root system manolakis Debian 5 09-28-2007 11:27 AM
Not Very Urgent, Just can't find a website! bob3dan General 3 02-09-2007 06:17 PM
URGENT-BIND can't find... xailer Linux - Newbie 4 11-30-2003 05:40 PM

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

All times are GMT -5. The time now is 12:54 PM.

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
Open Source Consulting | Domain Registration