LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help Urgent With Find Command!! (https://www.linuxquestions.org/questions/linux-newbie-8/help-urgent-with-find-command-947196/)

sukhdip 05-28-2012 04:53 AM

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,

pixellany 05-28-2012 05:49 AM

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"

sukhdip 05-28-2012 05:59 AM

Quote:

Originally Posted by pixellany (Post 4689331)
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.

unSpawn 05-28-2012 06:58 AM

Running find with -printf "%f\n" will display bare file names.

salasi 05-28-2012 09:58 AM

Quote:

Originally Posted by pixellany (Post 4689331)
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.

bigrigdriver 05-28-2012 10:12 AM

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.

sukhdip 05-28-2012 10:38 AM

Hi everybody thanks for your help.
I solved it.
Code:

find /backup/test1/LogFiles/$filename"_"*.* ........
It worked as per my requirements. :)

pixellany 05-28-2012 02:14 PM

Quote:

Originally Posted by salasi (Post 4689453)
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.....;)


All times are GMT -5. The time now is 01:33 AM.