Linux question: I like to browse using ls. Than when I find what I'm looking for, I would like to directly move there with cd without having to retype the path. Is there an elegant solution for that?

Maybe there is a tool that does that? Maybe I'm doing this all wrong and there is a different way I should browse?

126 points · 63 comments · view on lemmy.world

63 Comments

ccunning@lemmy.world · 119 pts · 4d (25 replies)

You mean something like this?
ls /your/long/path/name
cd !$

valar@lemmy.ca · 109 pts · 4d (19 replies)

To add,

!$ gets replaced with the last word from the previous command line

!! Repeats the most recent command

!-N Repeats the Nth previous command

!string Repeats the most recent command that began with 'string'

village604@adultswim.fan · 50 pts · 4d (3 replies)

I just learned more about Linux than I did from my whole IT degree.

4am@lemmy.zip · 19 pts · 3d (1 reply)

You learned more about shells than you knew. This stuff works on BSD and some UNIX systems too, you know

GreyEyedGhost@piefed.ca · 4 pts · 3d

Most POSIX-compliant systems?

bluelabel@lemmy.world · 1 pts · 2d

I learned Ubuntu in uni decades ago, and I didn't know those commands exist.

mp3@lemmy.ca · 35 pts · 4d (2 replies)

!! is the goat when you needed to run the last command using sudo

Hal@piefed.nz · 10 pts · 3d

Hot damn, and here's me thinking thefuck was the way to do this.

ccunning@lemmy.world · 10 pts · 4d

💯

4am@lemmy.zip · 25 pts · 3d (5 replies)

If !$ isn’t pronounced “ba-ching!” I’m quitting Linux

daychilde@lemmy.world · 11 pts · 3d (4 replies)

Better than "bang-ching" which sounds racist to me. lol

SkaveRat@discuss.tchncs.de · 8 pts · 3d

Great, now omarchy is going to adopt it

HeyThisIsntTheYMCA@lemmy.world · 4 pts · 3d (2 replies)

the (ng) is silent when used in compound punctuation in ba(ng)

daychilde@lemmy.world · 5 pts · 3d (1 reply)

okay, but now THAT sounds racist for whole OTHER reasons...... ;-)

HeyThisIsntTheYMCA@lemmy.world · 3 pts · 3d

it probably is, that professor who taught me it was belgian

Exec@pawb.social · 22 pts · 3d (1 reply)

!!:n where n is the nth word after the command in your last line

cp file1 file2
nano !!:2

Here nano opens file2.

valar@lemmy.ca · 6 pts · 3d

Love it

klymilark@herbicide.fallcounty.omg.lol · 15 pts · 3d (1 reply)

... How have I used Linux for two decades and not run into this once. Amazing stuff

GreyEyedGhost@piefed.ca · 9 pts · 3d

I've never come across this before, and I am completely unsurprised it exists.

abbadon420@sh.itjust.works · 6 pts · 4d

I'm loving this

ccunning@lemmy.world · 2 pts · 4d

Huh - TIL about !-N

Thanks 🙏

Openopenopenopen@lemmy.world · 8 pts · 3d

I think, if your using bash

alt + .

Does the same thing as typing

!$

ls /super/long/path/to/dir

cd alt + .

I believe if you hit it again, it cycles through all previous last words

https://ostechnix.com/list-useful-bash-keyboard-shortcuts/

iturnedintoanewt@lemmy.world · 7 pts · 3d

And there goes my daily new knowledge snippet! Thanks!

gothic_lemons@lemmy.world · 4 pts · 3d

Holy fucking shit. I've dabbled in Linux for so long to not know. Thank you!

quarrel4you@piefed.social · 3 pts · 4d

I like the way you think! Too bad a I replied to a seaming empty post some 26 min after you. I always disliked this about Reddit. Seeing someone rewording the top reply for karma.

Raja@lemmy.world · 3 pts · 4d

In some shells (e.g. fish), "running" a directory changes to it so you can save keystrokes and just delete the "ls ".

HubertManne@piefed.social · 31 pts · 4d (2 replies)

so you have better answers but still going to do mine. up arrow and change ls to cd.

abbadon420@sh.itjust.works · 10 pts · 4d (1 reply)

Yeah, thats what I was doing,but I was hoping for something more elegant haha

usernameunnecessary@lemmy.zip · 10 pts · 4d
^ls^cd

will do exactly what you want, replace ls with cd from the previously run command

quarrel4you@piefed.social · 27 pts · 4d

You may want to learn about the shell's "history expantion" functions. as an example:

~$ ls ~/Documents/taxes/tax_year_2012/2012-12-01.txt  
/home/tom/Documents/taxes/tax_year_2012/2012-12-01.txt  

~$ cd !!$:h  
cd ~/Documents/taxes/tax_year_2012  

~/Documents/tax_2012$ pwd  
/home/tom/Documents/taxes/tax_year_2012  

Here the string "!!$:h" is basically the last command's [ !! ] last argument [$], stripped of its last path component [:h].

notabot@piefed.social · 21 pts · 4d (3 replies)

Type cd then press Alt+. and it'll copy the last argument of the previous command, make sure it's what you want, and press Enter.

sin_free_for_00_days@sopuli.xyz · 3 pts · 3d (2 replies)

Nice, I don't remember having seen that before. I'm sure I'll just keep using !$, because muscle memory, but I like seeing new approaches.

notabot@piefed.social · 2 pts · 3d (1 reply)

The shell, and lots of other things that take per-line input, use readline to do it. The readline man page is... extensive... to say the least. There are many more useful shortcuts in there, but I doubt anyone uses more than a fraction of them.

sin_free_for_00_days@sopuli.xyz · 1 pts · 2d

I remember delving into the documentation maybe 15 years ago. Even then it was a bit much. I remember changing whatever setting I was looking for, and thinking one day I'd look back through it at my leisure. Well, that never happened.

UnfortunateShort@lemmy.world · 20 pts · 3d

You can hit TAB in any decent shell to autocomplete paths. Personally I highly recommend fish

NeatNit@discuss.tchncs.de · 10 pts · 4d (5 replies)

What I do is: Up, Home, type cd , Delete (hold/repeat as many times as needed), Enter.

The Home key instantly brings your cursor to the start of the command line (before the ls), even if the command is long enough to wrap.

I'm not a terminal guru, and I don't aspire to become one. I don't want to memorize arcane symbols that each has a subtly different meaning, and I especially don't want to execute them before I see explicitly what they're going to do. So while the top comment of cd !$ is a perfectly good and correct answer, I offer this alternative in case you, like me, like to see what you're doing as you do it. Home and End are pretty good.

abbadon420@sh.itjust.works · 6 pts · 4d (3 replies)

I had already found ctrl+a that brings me back to the front of the line. It's bit more convenient for my hands than home.

NeatNit@discuss.tchncs.de · 5 pts · 4d (1 reply)

I have a really strong muscle memory for Home so it's super convenient to me, but ctrl+a sounds just as good.

Zarobi@aussie.zone · 2 pts · 3d

Minor addition: I've had some shells where home will bring you back to the beginning of the current line, not the whole command. Ctrl+Home always brings you to the very start of the command if this applies to you

andyburke@fedia.io · 3 pts · 3d

ctrl-a: beginning of line ctrl-e: end of line ctrl-k: delete rest of line from cursor ctrl-u: delete line before cursor

these are helpful in these situations, as well.

charokol@lemmy.zip · 5 pts · 3d

The seconds I’d save remembering which combination of dollar signs and exclamation points to use would never help me find a better job or otherwise improve my life. I’m okay with the up arrow too.

Noctambulist@lemmy.world · 9 pts · 3d

The way I solve this problem is to use nnn instead of ls. It's a full file manager for the terminal but so light and quickly started that it's as convenient as ls (a lot more so actually). You can navigate around your file system with vim-like key bindings and then have the option to exit either to the directory you started from or the one currently displayed.

gole@lemmy.zip · 8 pts · 3d

For me I replaced cd with https://github.com/ajeetdsouza/zoxide

cd to anywhere from anywhere, don't even need to type the whole thing. Algorithm: https://github.com/ajeetdsouza/zoxide/wiki/Algorithm#matching

arbitrary_sarcasm@lemmy.world · 6 pts · 3d

Looks like no one has mentioned this yet. So I'll be the FZF champion in this thread.

The ZSH plugin for FZF gives you a few commands that use the fuzzy search to find files by name, keyword in content etc.

It could be very easy to add something like that for your use case where you can use and you pick the file name from the output and it automatically cd's into the selected file's directory.

tal@lemmy.today · 5 pts · 4d (2 replies)

If you're using bash and haven't modified the readline default, which is to use emacs keystrokes:

$ ls foo/bar/baz

Just what I wanted!

Control-P Control-A Alt-D cd Enter

BonkTheAnnoyed@piefed.blahaj.zone · 3 pts · 4d (1 reply)

absolutely brilliant

You know, I always kind of wondered what happened when I accidentally pressed control C, control V or whatnot in bash.

Level unlocked

tal@lemmy.today · 1 pts · 4d

Well...Control-C is treated by bash differently than in emacs. It basically cancels the existing line and gives you a fresh line.

And IIRC, Control-V is also different, used to input literal control characters. Like, the terminal will handle that if it's in cooked mode.

checks

$ od -x

hits Control-V Control-C Enter Control-D

^C

0000000 0a03
0000002
$

Yeah.

Control-U is also a notable difference I know off-the-cuff. In emacs, it's the universal-argument prefix, which is an important way to pass arguments to functions. In readline, it's kill-to-beginning-of-line.

kik2civeswt1@sh.itjust.works · 5 pts · 3d

There's already some great answers in the thread, so I won't add to those.

Some shortcuts are shell-specific. For example, !!, !:$, and ^old^new are in bash, but not in fish.

Some things, like Alt-. and Ctrl-a, are implemented by readline, which can be used elsewhere, as well, for example in the python interpeter.

Depending on your context, it may be better to invest time learning methods implemented by readline. While not necessarily universal, they are more versatile.

There were heaps of functions I was using not realising they were from readline. For example, Ctrl-r for reverse interactive search, Ctrl-a/e for start/beginning if line, Ctrl-w for deleting a word.

anotherandrew@lemmy.mixdown.ca · 5 pts · 3d

I use tab complete a lot to find a file/path. would cd /foo/some<tab>/more<tab>/desti<tab> get you there any easier?

usernameunnecessary@lemmy.zip · 4 pts · 4d

In addition to

^ls^cd

I mentioned in another comment, I highly recommend you try "z", installable on macos via "brew install z" (not sure if it's available on linux package managers). Suppose you want to jump to ~/Desktop/project1/foo, you can just type "z foo". It works without a full path, from anywhere, as long as you cd'd in there at least once.

Steve@startrek.website · 4 pts · 3d (2 replies)

Is it uncool to use a GUI?

daychilde@lemmy.world · 3 pts · 3d

Use what you like.

I'm not OP, but I use SSH a lot, meaning I'm very much in a character-based terminal of some sort. :)

khannie@lemmy.world · 3 pts · 3d

Nah not at all, you do you but when you get used to the command line it's generally much faster and offers a lot of power.

luthis@lemmy.nz · 3 pts · 4d (2 replies)

Just change the ls to cd?

LifeInMultipleChoice@lemmy.dbzer0.com · 4 pts · 4d (1 reply)

The way they had it worded I figured they meant they wanted to go ls /mnt/drive/backups/2017/03/19/documents/taxes/w2s

Then type cd !$ so they don't have to type all of that again instead of copy/pasting or pressing the up key and then holding the left button to get back and delete the ls.

tgxn@lemmy.tgxn.net · 4 pts · 3d

You guys dont have Home buttons? No holding left for me.

bhamlin@lemmy.world · 3 pts · 2d

Up arrow, ctrl-a, delete, delete, cd, enter

palordrolap@fedia.io · 3 pts · 4d (1 reply)

The soft option perspective (I see plenty of command-line-only solutions here already):

These days, if I'm doing any major navigation, I do it the uncool way and use the GUI file manager bundled with my distro. I have the option enabled to show the "open a terminal at this location" button if I really want to start issuing commands.

If you'd rather stay on the command line, a compromise might be to use something like Midnight Commander. Back in my DOS days I used to use an equivalent there on a regular basis, but that's because I hadn't launched Windows and didn't feel like it was necessary to do what I wanted. (It was possible to patch Win 9x to behave like earlier Win 3.x so that it stopped at the DOS prompt and didn't load Windows, which fit my aesthetic at the time.)

These days I prefer my comforts and I sure as heck don't boot Linux to a command line unless something's really wrong!

tal@lemmy.today · 1 pts · 3d

If you'd rather stay on the command line, a compromise might be to use something like Midnight Commander. Back in my DOS days I used to use an equivalent there on a regular basis, but that's because I hadn't launched Windows and didn't feel like it was necessary to do what I wanted.

Note that this class of file managers (two-pane, Norton Commander-alike) are called "orthodox file managers".

https://en.wikipedia.org/wiki/Category:Orthodox_file_managers

https://softpanorama.org/OFM/index.shtml

My experience is that people out of Eastern Europe in particular seem to have a history of using these.

There are also some non-OFM console file managers, like ranger, which might appeal to vim-folk:

https://github.com/ranger/ranger

Ranger is a console file manager with VI key bindings. It provides a minimalistic and nice curses interface with a view on the directory hierarchy.

I use dired in emacs, which does a number of things similarly to an OFM if you have two panes open (though dired is not itself an OFM, and emacs does have Sunrise Commander, which is an OFM).

Generally, I live in bash, but I find that file managers are handy when you want to manually mark a set of files to run an operation on (like, you can't programmatically identify them).

scrion@lemmy.world · 3 pts · 4d
  1. You could start by typing cd and use tab completion to explore your target folder(s). This would require at least a rudimentary knowledge of what you're looking for so you can prefix your search

  2. If you really want to explore the contents and pick your target without knowing anything in advance, cd "$(find . -type d | fzf)" would give you a smart browser. You can of course replace '.' with any base directory you like. Requires fzf.

  3. In general, zoxide can help with navigation.

  4. If you want to stay a purist, I'd go for simply recalling your last ls command from the history and replacing it with cd. An example has been given in the thread already.

catlover@sh.itjust.works · 1 pts · 3d

Havent seen this mentioned: https://github.com/dylanaraps/fff

check the cd on exit part: https://github.com/dylanaraps/fff#cd-on-exit

hexagonwin@lemmy.today · 1 pts · 7h

kinda unrelated but check out midnight commander (mc). switch views using alt-t and change to shell with ctrl o.

DummyWOW@lemmy.zip · 1 pts · 3d

Bookmarking this whole thread