user15421218
user15421218

Reputation:

How do I navigate fast through directories with the command line?

I spent some time finding a solution for my problem but google couldn't provide me a sufficient answer... I'm working a lot with the command line in linux and I simply need a way to navigate fast through my file system. I don't want to type cd [relative or absoulte path] all the time. I know there is pushd and popd but that still seems too complicated for a simple problem like this.

When I'm in ~/Desktop/sampleFile I simply want to use sampleCommand fileToGo to get to ~/Desktop/anotherFile/anotherFile/fileToGo, no matter, where the file is located. Is there an easy command for this?

Thanks in advance!

Upvotes: 0

Views: 1612

Answers (3)

user3001290
user3001290

Reputation: 1

You can try cd++ from here:

https://github.com/vitalij555/cd_plus_plus

Once installed, it's easy to use:

  1. Mark your current directory with label(or several labels for convenience):

mark label_name

  1. Return to that folder anytime later by just issuing:

go label_name

I wrote this script as I usually need to switch between many different folders during the workday and was bored of creating aliases manually.

Upvotes: 0

Léa Gris
Léa Gris

Reputation: 19675

This can be done with native Bash features without involving a sub-shell fork:

You can insert this into your "$HOME/.bashrc":

cdf(){
  # Query globstar state
  shopt -q globstar
  # and save it in the gs variable (gs=0 if set, 1 if not)
  local gs=$?

  # Need globstar to glob find files in sub-directories
  shopt -s globstar

  # Find the file in directories
  # and store the result into the matches array
  matches=(**/"$1")

  # globstar no longer needed, so restore its previous state
  [ $gs -gt 0 ] && shopt -u globstar

  # Change to the directory containing the first matched file
  cd "${matches[0]%/*}" # cd EXIT status is preserved
}

Upvotes: 4

Ætérnal
Ætérnal

Reputation: 332

Hmm, you could do something like this:

cd $(dirname $(find . -name name-of-your-file | head -n 1))

That will search the current directory (use / instead of . to search all directories) for a file called name-of-your-file and cd into the parent directory of the first file with that name that it finds.

If you're in a large directory, typing the path and using cd will probably be faster than this, but it works alright for small directories.

Upvotes: 1

Related Questions