Solved

bash script, find using passed path

Posted on 2013-11-04
4
287 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
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 84

Accepted Solution

by:
ozo earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

12 Experts available now in Live!

Get 1:1 Help Now