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 6829115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:27:43+00:00 2026-05-26T22:27:43+00:00

I want to run two commands but the second command depends on the first.

  • 0

I want to run two commands but the second command depends on the first.

Is there a way to do something like this.

 find . -name '*.txt' -exec  'y=$(echo 1); echo $y' {} \;

…

And actually, I want to do this.
Run the find command, change to that directory that the file is in and then run the command on the file in the current directory.

find . -name '*.txt' -exec 'cd basedir && /mycmd/' {} \;

How do I do that?

  • 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-26T22:27:44+00:00Added an answer on May 26, 2026 at 10:27 pm

    Find’s -exec option expects an executable with arguments, not a command, but you can use bash -c cmd to run an arbitrary shell command like this:

    find . -name '*.txt' -exec bash -c 'cd $(dirname {}) && pwd && /mycmd $(basename {})' \;
    

    I have added pwd to confirm that mycmd executes in the right directory. You can remove it. dirname gives you the directory of each file and basename gives you the filename. If you omit basename your command will receive (as {}) pathname to each file relative to the directory where you run find which is different from mycmd‘s current directory due to cd, so mycmd will likely fail to find the file. If you want your command to receive absolute pathname, you can try this:

    find $PWD -name '*.txt' -exec bash -c 'cd $(dirname {}) && pwd && /mycmd {}' \;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to find out how to run two WPF windows (one in
I want to write a bash script where I run two commands simultaneously, then
Why this code don't work,when i want run this code vwd 2008 express show
I want to run a command as soon as a certain text appears in
I have this query which I want to run in my PHP application back
I want to run JBoss 4.2.3 and JBoss 5.1 on the same server, but
I'd like to join two files in bash using a common column. I want
whenever I run this below it gives the output but it also gives an
I want to be able to run a program through command line and I
I want to follow TDD, but the command grails test-app CUT needs almost a

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.