LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 08-10-2009, 09:24 AM   #1
dexznrl
Member
 
Registered: Jul 2009
Posts: 36

Rep: Reputation: 0
Bash script with find command


Hello,

If I run a command like:
find /home/myname/testlog.*
I will end up with all files that starts with "testlog."

If I put this into a bash script I end up with all files that starts with "testlog.*" and that is not what I want.

How to sort this out? Any ideas?

Best regards

Johan
 
Old 08-10-2009, 10:25 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by dexznrl View Post
If I run a command like:
find /home/myname/testlog.*
I will end up with all files that starts with "testlog."
No, not completely correct. You will end up with all files in the working directory that starts with "testlog." plus all files and sub-directories below any sub-directory that has a name that starts with "testlog."

Quote:
Originally Posted by dexznrl View Post
If I put this into a bash script I end up with all files that starts with "testlog.*" and that is not what I want.
If you only want files in the directory /home/myname/ with names that start with "testlog." do:
Code:
ls /home/myname/testlog.*
I you want all files with names that start with "testlog." in any sub-directory below /home/myname/ do:
Code:
 find -type f -name "testlog.*"
 
Old 08-10-2009, 10:34 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by Hko View Post
I you want all files with names that start with "testlog." in any sub-directory below /home/myname/ do:
Code:
 find -type f -name "testlog.*"
I think you probably mean:
Code:
find /home/myname -type f -name "testlog.*"
--- rod.
 
Old 08-10-2009, 11:42 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by theNbomr View Post
I think you probably mean:
Code:
find /home/myname -type f -name "testlog.*"
Yes, you're right about that.
My mistake.
 
Old 08-10-2009, 11:43 AM   #5
dexznrl
Member
 
Registered: Jul 2009
Posts: 36

Original Poster
Rep: Reputation: 0
Excellent help

Thank you everybody for all your help.
 
Old 08-10-2009, 12:19 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
also if you only need the filename and not include the prefix directories, use
Code:
find /home/myname -type f -name "testlog.*" -printf '%f\n'
 
Old 08-10-2009, 12:52 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
That -printf argument was one I never knew about. That's going to be one I use a lot. Thanks.
--- rod.
 
  


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] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
bash script: how to check stderr after command "find" Vilmerok Programming 1 07-08-2009 07:35 AM
bash command - find |cp crackpipe Linux - Software 4 05-05-2009 02:36 AM
Bash - find command And_pas Programming 7 05-10-2007 03:33 PM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 12:04 PM


All times are GMT -5. The time now is 10:49 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration