Solved

bash script, find using passed path

Posted on 2013-11-04
4
286 Views
Last Modified: 2013-11-09
There's probably a super exotic way to do this, and I AM interested!
If someone could show me how to lessen the 'if/then', I am interested in that too.

What I can't get working is the 'find'.  When tracing, the command looks perfect!


TreeTop=$1                     # TreeTop is the path to start searching
maybe_shorter=${TreeTop#* }    # Get the first part if there's a space
if [ ${#TreeTop} != ${#maybe_shorter} ]
then
  TreeTop=`echo "$TreeTop" | sed -e 's/ /\\\ /g'`   # Escape spaces
fi
...
find "$TreeTop"  -type f | paste -s -d '\n' - | grep -v ".DS_Store" > killme10.txt

I can't get the following to work!

bash -x ./FindDuplsInOneTree.sh   "~/Music/iTunes/iTunes Media/Music"
This yields

+ TreeTop='~/Music/iTunes/iTunes Media/Music'
+ maybe_shorter=Media/Music
+ '[' 33 '!=' 11 ']'
++ echo '"~/Music/iTunes/iTunes Media/Music"'
++ sed -e 's/ /\\ /g'
+ TreeTop='"~/Music/iTunes/iTunes\ Media/Music"'
...
+ cat /dev/null
+ find '"~/Music/iTunes/iTunes\ Media/Music"' -type f
+ paste -s -d '\n' -
+ grep -v .DS_Store
find: "~/Music/iTunes/iTunes\ Media/Music": No such file or directory

Any advice much appreciated.  I'm trying to fix a merged library.
Thanks!
eTop=$1                     # TreeTop is the path to start searching
maybe_shorter=${TreeTop#* }    # Get the first part if there's a space
if [ ${#TreeTop} != ${#maybe_shorter} ]
then
  TreeTop=`echo "$TreeTop" | sed -e 's/ /\\\ /g'`   # Escape spaces
fi
...
find 

Open in new window

0
Comment
Question by:Ralph
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
You don't need the if, since sed will make no substitutions when $TreeTop has no spaces,
but you don't need the sed, since you are quoting "$TreeTop"  when you do the find.
In fact, since you are quoting it, you don't want to do the sed.
0
 

Author Comment

by:Ralph
Comment Utility
I tried just quotes, and since the ~ is there, it won't work as
"~/blah/blah blah/blah"
nor as
~"/blah/blah blah/blah"

Also, I'm not sure about a leading or trailing space if the user has it in his quotes.
Maybe I don't need to be that idiot proof.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
try
find "${1/#~/$HOME}"  -type f
or
find "`eval echo $1`"  -type f
with no sed
or, try calling the script with
bash -x ./FindDuplsInOneTree.sh   ~/"Music/iTunes/iTunes Media/Music"
0
 

Author Closing Comment

by:Ralph
Comment Utility
All of the advice given was good, (even when you think you have things covered).

MORE importantly for me though, is the "Oh yeah" new find search path handler,
and more? fun, the challenge of figuring out a tricky regular expression search path handling.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video discusses moving either the default database or any database to a new volume.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now