Find + cd in one command?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find + cd in one command?
# 1  
Old 05-21-2014
Find + cd in one command?

Hi there,

I'm new to Unix and wondered whether there is some command which allows me to search for a directory and directly go there if it exists. My problem is that I've got quite a lot of directories nested into each other. If I want to quickly get to a file which is nested 5 times, it is cumbersome. Also, if I'm not 100% sure about a file-name and I search for it with wild-cards, I want to get to the document directly without having to use the mouse to copy paste the address of the file. In fact I want to first search sth, i.e.
Code:
find . -name "myfile" -type f

but directly cd into it. In fact I want to do something like this:
Code:
find . -name "myfile" -type f -cd

which unfortunately doesn't work.

An alternative would perhaps be to be able to know the command of copy pasting something from the last output such that it can be used as new input when using the cd command.

I didn't find anything in the internet.. Can anybody of you help me? Thanks!
# 2  
Old 05-21-2014
If you're running bash, then this should work.

Code:
FCD() { dir="$(find . -name "$1" -exec dirname {} \; | gawk 'NR == 1 { print }')"; if [ "${dir}" != "" ]; then echo "change to '${dir}'"; cd "${dir}"; fi; }

Then whenever you type "FCD filename" it will run the script and change directories to the first place it finds something by that name.

Obviously, you'd want to define that shell function in a login script rather than typing it.

Last edited by cnamejj; 05-21-2014 at 07:18 PM.. Reason: forgot to make the script general purpose, had hardcoded test name...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Is it possible to find the seek rate of the find command in Solaris?

Hello, I am running some performance based tests on Solaris, and I was wondering how fast the "seeking" rate of Solaris is, or how fast Solaris can get information about files with the "find" command. Does anyone know what 'find' command I could run to traverse through my system to see the rate... (1 Reply)
Discussion started by: bstring
1 Replies

2. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

3. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

4. Shell Programming and Scripting

Find, regular expression, anyway to simplify this find command?

Hello everyone, first post here, trying to learn scripting on my own and this forum as been really helpful so far. I made few little scripts working great but I m facing some problems with RE. I have a bunch of files in many subdirectories called *001.ext *002.ext OR simple *.ext or *01.ext... (7 Replies)
Discussion started by: Sekullos
7 Replies

5. Shell Programming and Scripting

what is the find command to find exact dir from the root

I want to find a dir called STOP from the root.so what is the find command. Thanks & Regards Rajkumar (1 Reply)
Discussion started by: rajkumar_g
1 Replies

6. Linux

Simplified find command to find multiple file types

Hi, I'm using the following command to find the multiple requierd file types and its working fine find . -name "*.pl" -o -name "*.pm" -o -name "*.sql" -o -name "*.so" -o -name "*.sh" -o -name "*.java" -o -name "*.class" -o -name "*.jar" -o -name "*.gz" -o -name "*.Z" -type f Though... (2 Replies)
Discussion started by: vickramshetty
2 Replies

7. Shell Programming and Scripting

find: No match due to find command being argument

I am using csh and getting the error "find: No match." but I cannot figure out why. What I am trying to do is set the find command to a variable and then execute the variable as a command. I ran it through a debugger and it looks like $FIND is getting set but the find command can not actually be... (2 Replies)
Discussion started by: mst3k4l
2 Replies

8. UNIX for Dummies Questions & Answers

how to find a file named vijay in a directory using find command

I need to find whether there is a file named vijay is there or not in folder named "opt" .I tried "ls *|grep vijay" but it showed permission problem. so i need to use find command (6 Replies)
Discussion started by: amirthraj_12
6 Replies

9. Shell Programming and Scripting

Little bit weired : Find files in UNIX w/o using find or where command

Yes , I have to find a file in unix without using any find or where commands.Any pointers for the same would be very helpful as i am beginner in shell scritping and need a solution for the same. Thanks in advance. Regards Jatin Jain (10 Replies)
Discussion started by: jatin.jain
10 Replies

10. Shell Programming and Scripting

command find returned bash: /usr/bin/find: Argument list too long

Hello, I create a file touch 1201093003 fichcomp and inside a repertory (which hava a lot of files) I want to list all files created before this file : find *.* \! -maxdepth 1 - newer fichcomp but this command returned bash: /usr/bin/find: Argument list too long but i make a filter all... (1 Reply)
Discussion started by: yacsil
1 Replies
Login or Register to Ask a Question