Live data from Hacker News

Quickly navigate your filesystem from the command line

jeroenjanssens.com

91–100 of 148 posts

Re: Quickly navigate your filesystem from the command line

#91

I had to modify Jeroen's code to get the `marks` shortcut working under Mac OS 10.8: export MARKPATH=$HOME/.marks function jump { cd -P $MARKPATH/$1 2> /dev/null || echo "No such mark: $1" } function mark { mkdir -p $MARKPATH; ln -s $(pwd) $MARKPATH/$1 } function unmark { rm -i $MARKPATH/$1 } function marks { ls -l $MARKPATH | sed 's/ / /g' | cut -d' ' -f9- && echo }

Here is another version of 'marks' for Mac OS that aligns the arrows. It also works when 'ls' is aliased. function marks { \ls -l $MARKPATH | tail -n +2 | sed 's/ / /g' | cut -d' ' -f9- | awk -F ' -> ' '{printf "%-10s -> %s\n", $1, $2}' }

If you are on a system with BSD stat like OS X you can get the same with

  function marks {
    (cd $MARKPATH && stat -f"%-10SN%SY" *)
  }
Even better with column

  (t="$(printf "\t")"; cd $MARKPATH && stat -f"%N$t%SY" * | column -ts"$t")

Re: Quickly navigate your filesystem from the command line

#92

For Windows users I have a closely related shameless plug: cdhere navigates your cmd.exe to wherever the current topmost Explorer window is pointed at. It's not the same as the OP's trick, of course, but since you're voluntarily using Windows, I'm going to assume you "live" on the command line less than the average UNIX user, and more on GUI tools such as good old Explorer. https://github.com/eteeselink/cdhere

I used to have a really hacky AppleScript to do the same thing in OS X. The crappy part was that launching the whole AppleScript runtime was really slow, but that was the only API I could find with getting that information from the Finder.

Re: Quickly navigate your filesystem from the command line

#93

For Windows users I have a closely related shameless plug: cdhere navigates your cmd.exe to wherever the current topmost Explorer window is pointed at. It's not the same as the OP's trick, of course, but since you're voluntarily using Windows, I'm going to assume you "live" on the command line less than the average UNIX user, and more on GUI tools such as good old Explorer. https://github.com/eteeselink/cdhere

I used to have a really hacky AppleScript to do the same thing in OS X. The crappy part was that launching the whole AppleScript runtime was really slow, but that was the only API I could find with getting that information from the Finder.

You can also drag any file or folder onto your terminal, which will paste the absolute filepath at your cursor point.

Re: Quickly navigate your filesystem from the command line

#94

I set my version up with a -f on the unmark rm so that I don't need to be prompted to remove it. Also added some basic completion for both jump and unmark export MARKPATH=$HOME/.marks function jump { cd -P $MARKPATH/$1 2>/dev/null || echo "No such mark: $1" } function mark { mkdir -p $MARKPATH; ln -s $(pwd) $MARKPATH/$1 } function unmark { rm -if $MARKPATH/$1 } function marks { ls -l $MARKPATH | sed 's/ / /g' | cut -…

The variable expansions really ought to be quoted, like this:

  function unmark {
      rm -if "$MARKPATH/$1"
  }
Otherwise you are asking for much sadness.

Re: Quickly navigate your filesystem from the command line

#95
post #15

Earlier quoted context omitted.

Put the following in your .bashrc instead: shopt -s autocd If you type a directory in the command-line, it’ll cd into it, e.g.: $ .. # cd .. $ ~ # cd ~ $ foo # cd foo I can’t live without this.

I believe this is set by default in oh-my-zsh as well.

In Zsh, you can set it so that specifying a bare directory goes to the directory as well, for any directory. Just do

  setopt autocd

Re: Quickly navigate your filesystem from the command line

#96
post #41

I usually just alias directories I frequent in .bashrc... Easy enough, I guess.

I'll do it all the time in my local shell session, along the following:

  # I make a point to always have the trailing slash,
  # it makes life easier.
  # Also, tab-complete works with this in Zsh.
  a=/some/really/long/path/that/I/dont/like/typing/
  b=/some/other/really/long/path/too/
  cd $a
  # ... do stuff ...
  cp one two three $b
  cd $b
  # ... do more stuff ...
  cp four five six $a
  cd $b/even/longer/path
And so forth. Since it is a normal variable, you can use it anywhere you need the path.

Re: Quickly navigate your filesystem from the command line

#98
Nice idea, reminds me of ncd from a far away, foggy past. I suggest some escaping on the directory name parameter though. If you're not careful, you're going to run into trouble with paths that have spaces or risk some nasty security issues with embedded special characters.

Re: Quickly navigate your filesystem from the command line

#99

For Windows users I have a closely related shameless plug: cdhere navigates your cmd.exe to wherever the current topmost Explorer window is pointed at. It's not the same as the OP's trick, of course, but since you're voluntarily using Windows, I'm going to assume you "live" on the command line less than the average UNIX user, and more on GUI tools such as good old Explorer. https://github.com/eteeselink/cdhere

I'm not sure voluntary is necessarily true. I have had to use Windows several times because we were working with Windows only programs.

On a different note, back in UNIX land I found myself doing the opposite of what you described: opening the file explorer to the same directory as the command-line. Of course, there is not much to that trick other than choosing an explorer that opens to the working directory or a path provided as an arguement.

Re: Quickly navigate your filesystem from the command line

#100

I set my version up with a -f on the unmark rm so that I don't need to be prompted to remove it. Also added some basic completion for both jump and unmark export MARKPATH=$HOME/.marks function jump { cd -P $MARKPATH/$1 2>/dev/null || echo "No such mark: $1" } function mark { mkdir -p $MARKPATH; ln -s $(pwd) $MARKPATH/$1 } function unmark { rm -if $MARKPATH/$1 } function marks { ls -l $MARKPATH | sed 's/ / /g' | cut -…

The purpose of the -i flag is to prompt the user before deleting. Mixing the -i and -f flags is a little moot. Try removing the -i option to obtain what you want.

Didn't catch that, it was a quick hack of an edit, thanks.
Post reply on HN