Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-10-2007, 12:49 PM
|
#1
|
Member
Registered: Aug 2003
Posts: 426
Rep:
|
unwanted characters returned with bash find - how to remove?
this finds the first fiile in a directory:
find . -name "something-to-identify-with*" | sort -n | head -n 1
and this finds the last file:
find . -name "something-to-identify-with*" | sort -nr | head -n 1
one question: the directory i tested contains files in the naming
format:
Budgies_12345.bmp
the return from my cmd line is :
./Budgies_12345.bmp
what's with the ./? is there some simple syntaxing i
can use that will eliminate this from what is returned?
thanks,
BabaG
|
|
|
06-10-2007, 02:31 PM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
You can call it a reference point.
You used find ., which says from the dir I'm in and all subdirs. If you would have used find /path/to/dir the output would have been /path/to/dir/Budgies_12345.bmp instead of ./Budgies_12345.bmp
Ok, that explains why it's there.
To answer your question:
One way of removing it is this (using sed):
find . -name "something-to-identify-with*" | sed 's%\./%%' | sort -nr | head -n 1
Hope this helps.
|
|
|
06-10-2007, 02:35 PM
|
#3
|
Member
Registered: Aug 2003
Posts: 426
Original Poster
Rep:
|
thanks druuna. also got this from dawkcid:
basename $(find . -name "something-to-identify-with*" | sort -n | head -n 1)
thanks again,
BabaG
|
|
|
06-10-2007, 02:39 PM
|
#4
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
basename will work, but if you have a deeper directory tree things could go wrong (depending on what you want to do). If the following is found: ./X/Y/somefile, basename will return somefile. It will also return somefile for the following: ./X/Y/Z/somefile.
But like I said, it all depends on what you want to do with the output.
Hope this clears things up.
|
|
|
06-10-2007, 03:23 PM
|
#5
|
Member
Registered: Aug 2003
Posts: 426
Original Poster
Rep:
|
interesting. thanks!
BabaG
|
|
|
06-10-2007, 10:36 PM
|
#6
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
if you don't mind full path names, you can also use full path
Code:
find $(pwd) -name "something-to-identify-with*" | sort -nr | head -n 1
|
|
|
All times are GMT -5. The time now is 08:39 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|