Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

bash script, find using passed path

Posted on 2013-11-04
4
Medium Priority
?
299 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 85

Expert Comment

by:ozo
ID: 39621234
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
ID: 39621568
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 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 39623076
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
ID: 39636597
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 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