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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:18:48+00:00 2026-05-16T05:18:48+00:00

In a shell script how would I find a file by a particular name

  • 0

In a shell script how would I find a file by a particular name and then navigate to that directory to do further operations on it?

From here I am going to copy the file across to another directory (but I can do that already just adding it in for context.)

  • 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-16T05:18:49+00:00Added an answer on May 16, 2026 at 5:18 am

    You can use something like:

    cd -- "$(dirname "$(find / -type f -name ls | head -1)")"
    

    This will locate the first ls regular file then change to that directory.

    In terms of what each bit does:

    • The find will start at / and search down, listing out all regular files (-type f) called ls (-name ls). There are other things you can add to find to further restrict the files you get.
    • The | head -1 will filter out all but the first line.
    • $() is a way to take the output of a command and put it on the command line for another command.
    • dirname can take a full file specification and give you the path bit.
    • cd just changes to that directory, the -- is used to prevent treating a directory name beginning with a hyphen from being treated as an option to cd.

    If you execute each bit in sequence, you can see what happens:

    pax[/home/pax]> find / -type f -name ls
    /usr/bin/ls
    
    pax[/home/pax]> find / -type f -name ls | head -1
    /usr/bin/ls
    
    pax[/home/pax]> dirname "$(find / -type f -name ls | head -1)"
    /usr/bin
    
    pax[/home/pax]> cd -- "$(dirname "$(find / -type f -name ls | head -1)")"
    
    pax[/usr/bin]> _
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I execute a shell script from PHP while giving constant/live feedback to
I would like to execute a MySQL command in a shell script/cron job that
I have a python script that will take each file in a directory, iterate
I made a shell script that get a remote file with ftp protocol, if
I would to give the user the feature to run the shell script in
I would like to introduce multithreading feature in my shell script. I have a
I would like my script to act differently in an interactive shell session and
In shell script i need to redirect output from dd command to /dev/null -
I am writing a shell script in which I have to find the last
I have a script that I'm trying to run from cron. When I run

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.