LinuxQuestions.org
Help answer threads with 0 replies.
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, 10: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, 11:25 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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, 11:34 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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, 12:42 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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, 12:43 PM   #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, 01:19 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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, 01:52 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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 12:10 PM
bash script: how to check stderr after command "find" Vilmerok Programming 1 07-08-2009 08:35 AM
bash command - find |cp crackpipe Linux - Software 4 05-05-2009 03:36 AM
Bash - find command And_pas Programming 7 05-10-2007 04:33 PM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 01:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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