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
) calledls
(-name ls
). There are other things you can add tofind
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
Preserve Colouring After Piping Grep to Grep
Installing Node.Js on Debian 6.0
How to Learn the Structure of Linux Wireless Drivers (Mac80211)
Linux Free Shows High Memory Usage But Top Does Not
Internals of a Linux System Call
How to Perform a 'Git Pull' Without Re-Entering My Ssh Password
JSON Command Line Formatter Tool for Linux
Linux Kernel "Historical" Git Repository with Full History
Fast Way to Get Image Dimensions (Not Filesize)
How to Shield a CPU from the Linux Scheduler (Prevent It Scheduling Threads Onto That Cpu)
How to Delete All Files Older Than 3 Days When "Argument List Too Long"
How to Know Which of the /Dev/Input/Eventx (X=0..7) Have the Linux Input Stream
Check Battery Level of Connected Bluetooth Device on Linux
Find Files in Created Between a Date Range
Best Practices for Git Repositories on Open Source Projects
I Want to Contribute to the Linux Kernel