Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 526619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:47:45+00:00 2026-05-13T08:47:45+00:00

I am trying to use the output from mdfind to create a bunch of

  • 0

I am trying to use the output from mdfind to create a bunch of symlinks. Output of mdfind is like this:

/pathtofile1/
/pathtofile2/
/pathtofile3/

So, I used sed to add ln -s to the start of each line, and awk {print $0 "/directory where I want this/"};

after my single-line script successfully outputs this:

ln -s “/pathtofile1/” “/directory where I want this”
ln -s “/pathtofile2/” “/directory where I want this”
ln -s “/pathtofile3/” “/directory where I want this”

Problem is, when I run this, I get this error: “/directory where I want this: File does not exist”

The weird thing is that when I run these lines individually, they links are created as expected, but running the entire command returns the error above.

Any ideas?

I don’t think that this is the ideal way to do what I’m trying to do, so let me know if you have any better solutions.


Edited with more information.

#! /bin/bash
itemList=`mdfind -s "$1"| awk '{ print "ln -s \""$0"\" \"/Users/username/Local/Recent\""}'`
echo "$itemList"
`$itemList`

$1 is a test *.savedSearch that returns a list of files.

My result (from the echo) is:

ln -s "/Users/username/Dropbox/Document.pdf" "/Users/username/Local/Recent"
ln -s "/Users/username/Dropbox/Document2.pdf" "/Users/username/Local/Recent"

and the error that I get is:

ln: “/Users/username/Local/Recent”: No such file or directory

But, if I run a copy-pasted of each line individually, the links are created as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:47:45+00:00Added an answer on May 13, 2026 at 8:47 am

    One way to keep it simple:

    mdfind -0 "query" | ( cd "/Users/username/Local/Recent" ; xargs -0 -I path ln -s path . )
    

    This is of course doesn’t handle duplicate file names, etc.

    EDIT:

    The reasons your solution is failing is that, first, the contents of $itemList is being executed as one long command (i.e. the line feeds output by awk are ignored), and then, second, the command substitution occurs before quote removal. What is actually processed is roughly equivalent to:

    ln '-s' '"/pathtofile1/"' '"/to"' 'ln' '-s' '"/pathtofile2/"' '"/to"' 'ln' '-s' '"/pathtofile3/"' '"/to"'
    

    /bin/ln recognizes this as the:

    ln [-Ffhinsv] source_file ... target_dir
    

    form of the command and checks to see that the final parameter is an existing directory. That test fails because the directory name includes the surrounding quote marks. Note carefully the error message you report and compare:

    $ ln a b c "/Users/username/Local/Recent"
    ln: /Users/username/Local/Recent: No such file or directory
    $ ln a b c '"/Users/username/Local/Recent"'
    ln: "/Users/username/Local/Recent": No such file or directory
    

    So the morals of the story are, when you are dealing with file names in a shell, the safest solution is to avoid shell processing of the file names so you don’t have to deal with quoting and other side effects (which is a big advantage of an xargs solution) and keep it simple: avoid constructing complex multi-line shell commands. It’s too easy to get unexpected results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse an output from a server that looks like this: {
I'm trying to use PDO in this way and reuse the output template: selected_products_show
I'm trying to generate an XML output from SQL and need to use a
I am trying to make use of the output from a toDataURL call on
I'm trying to use awk to add up the numbers from an output file,
Trying to save output from this script to a file based on a cell
I am trying to use R to create a raster image from a matrix
I am trying to save the output from webAudio API for future use ,
I am trying to use a XslCompiledTransform, and use the output as a XPathDocument.
I'm trying to use XSLT text output to generate a file (in a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.