Find File Then Cd to That Directory in Linux

Find file then cd to that directory in Linux

You can use something like:

pax[/home/pax]> cd "$(dirname "$(find / -type f -name ls | head -1)")"
pax[/usr/bin]> _

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 piping through head -1 will filter out all but the first.
  • $() 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.

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]> _

How to find -exec cd in linux / unix

The command cd is a shell built-in, not found in /bin or /usr/bin.

Of course, you can't change directory to a file and your search doesn't limit itself to directories. And the cd command would only affect the executed command, not the parent shell that executes the find command.

Use:

cd $(find . -name config -type d | sed 1q)

Note that if your directory is not found, you'll be back in your home directory when the command completes. (The sed 1q ensures you only pass one directory name to cd; the Korn shell cd takes two values on the command and does something fairly sensible, but Bash ignores the extras.)

A Shell script to find and cd into a folder taking a folder name as argument in one step

You must run cd in your current shell. Running it in another shell won't work, as you've seen. Create a function. Example:

mycd (){
cd "${1}foo"
}

Is there a way to find a specific file and then change into the directory containing it in one go?

cd $(find . -name Subscription.java | xargs dirname)

linux cd to folder containing the file

Assuming your are using Bash: You can put in your ~/.bashrc

function smart_cd() {
cd "$(dirname $1)"
}

then: (after re-loading your .bashrc)

$ smart_cd /Users/user1/Documents/workspace/project/src/file.py

Bonus: you can also replace your cd command to handle both normal and smart cd:

function cd() {
if [ $# -eq 0 ] ; then
# no arguments
builtin cd
elif [ -d $1 ] ; then
# argument is a directory
builtin cd "$1"
else
# argument is not a directory
builtin cd "$(dirname $1)"
fi
}

For more information see:

  • Special Parameters in man bash for $#
  • man test for explanation about [ ... -eq ... ] and [ -d ... ]

Cding into directory hangs terminal

A) type alias | grep cd to see if its aliased, or type cd to check wether its been re-defined as a function.

B) start a new shell without startup file: bash --noprofile --norc

C) use a different shell: sh, or whatever



Related Topics



Leave a reply



Submit