CD in Linux - Didn't Think It Could Do THIS 😲

Поділитися
Вставка
  • Опубліковано 10 чер 2024
  • Some commands are so… familiar, you don’t even notice when you use them anymore. “cd” is one of them. It stands for “change directory” and yes, it’s about as basic as it gets - and you may think you know Kung Fu, but I bet I can still show you a couple of tricks that you can’t find in the manual. Also, there is no manual.
    CHAPTERS
    ------------------
    0:00 - Intro
    0:33 - How come there's no manual?
    0:50 - No arguments vs. "tilde" expansion
    1:20 - Absolute and relative paths
    1:39 - Basic usage examples
    2:08 - What are the dots?
    2:25 - The -P argument and symlinks
    2:51 - Hopping between directories
    3:05 - Last argument of the last command
    3:24 - How to turn on "autocd"
    3:57 - How to add multiple base paths
    5:02 - Share your own tips and tricks
  • Наука та технологія

КОМЕНТАРІ • 116

  • @mrwensveen
    @mrwensveen 11 місяців тому +30

    "cd !$" isn't technically a feature of cd, and works for other commands as well (like cat /path/to/file and nano !$). !$ is the (last) argument to the previous command, !! is the entire previous command (useful for sudo). There are a few others.

    • @simondslz
      @simondslz 11 місяців тому +3

      It's also possible to navigate through arguments history using " Alt + . "

    • @duckonaroll1913
      @duckonaroll1913 11 місяців тому +3

      holy shit i will absolutely be using “sudo !!” in the future tysm

    • @nixtricks
      @nixtricks  11 місяців тому +3

      Absolutely - that's an example of history expansion. I'll do a full video on all types of expansions in the near(-ish) future :)

    • @schwingedeshaehers
      @schwingedeshaehers 10 місяців тому

      for sudo !! is more interessting

  • @rich1051414
    @rich1051414 11 місяців тому +13

    the -P is important to prevent infinitely looping through symlinks, since you can have a symlink pointing to a parent directory.

  • @AWriterWandering
    @AWriterWandering 11 місяців тому +5

    2:14 dot files were actually created by accident. They wanted to hide the . and .., but but did so in a really lazy way by just filtering out anything that starts with a period.

  • @DeathStocker
    @DeathStocker 11 місяців тому +8

    Wait what I've never used mkcd! I've always done mkdir and then cd'd manually 🤦‍♂️
    EDIT: I just realised it is a shell dependent thing. It works on my zsh setup on termux with omz but not on its vanilla bash.

    • @nixtricks
      @nixtricks  11 місяців тому

      Nice! Just found the mkcd() function in zsh - what you can see at 2:04 is an alias from my bash alias video, where I created a one-liner with the same name :)

  • @AndersJackson
    @AndersJackson 11 місяців тому +8

    Another cool tricks is shortcuts, or key bindings in bash (which uses a library, so you can add this to your own commands).
    C-p for previous line, C-n for next line.
    C-f for forward a char, M-f (or Alt-f) for forward a work
    (Pattern is C- for single entities, M-x for a collection of entities)
    C-b for backward a char, M-b for backward a word
    C-r for reverse incremental search, and C-s (if not interpreted as Xoff, then C-q is on) for forward incremental search
    C-d d for delete character, M-d for delete word
    C-BackSpace for delete previous char, M-BackSpace for delete previous word.
    C-k for kill line from cursor, C-y for yank it back in (C-y can also yank back last removed word)
    M-y for change last yanked text with previous in kill ring (ring of previous removed works and lines)
    C-t will toggle (switch order) of two characters. M-t will switch two words. Try repeat them to move character/word backward.
    C-u 42 = will repeat the next command, insert a '=' in this case, but works with keybindings like C-t, M-t, C-d and M-d too.
    And now you know basic use of Emacs, just add C-x C-c (C-x is eXtended) to exit program, and C-x C-f for find file, that is to read files and directories into Emacs. And C-x C-s to save file in buffer and C-x C-w to write buffer to new name.
    M-x shell to start a shell and C-x b to switch buffer and C-x 2, C-x 3, C-x 1 an C-x 0 to change number of windows shown. C-x o to change which window is edited of the ones shown. C-x b to change buffer shown in current window.
    Have a nice day.

  • @NoEgg4u
    @NoEgg4u 11 місяців тому +13

    "cd" is included in the shell's man page, along with all of the other shell built-ins.
    man bash
    ...will bring up the man page that contains "cd".
    Now the fun begins, locating the section for "cd", as the shell's man page goes on for ½ of forever.
    And if you search for "cd" in the shell's man page, you see countless occurrences, none of which are what you are searching for.
    A technique that I use (to find a specific shell built-in within the shell's man page) is to search for some other shell built-in, that you will not find all over the place.
    For example, search the shell's man page for the built-in "caller". That will get you into the neighborhood of the shell built-in information, where the relevant "cd" information is located.

    • @your-mom-irl
      @your-mom-irl 11 місяців тому +1

      'help cd' will usually give you the same thing as your shells manual

    • @arvinakshuptabhijevankarni6337
      @arvinakshuptabhijevankarni6337 11 місяців тому

      / cd↩️

    • @scarecat
      @scarecat 11 місяців тому +1

      and for zsh the manpage is 'man zshbuiltins'

  • @DNero13
    @DNero13 11 місяців тому +4

    Bro the way you teach seems so simple but technically is amazing. The repetetive use of commands in the background, the simple explanation etc its really good, its like if i was sat practising it.
    Hope you blow soon . Bless up

  • @themartdog
    @themartdog 11 місяців тому +20

    In most shells I've ever used, there's always an OLDPWD variable set, so you can run `cd $OLDPWD` to move back and forth between two far separated dirs without typing them if you're moving between them a lot

    • @ytdlgandalf
      @ytdlgandalf 11 місяців тому +16

      Isn't that what 'cd -' does?

    • @themartdog
      @themartdog 11 місяців тому +10

      @@ytdlgandalf thanks for the tip, it seems like cd - is a shortcut for what I mentioned. It actually uses the value of OLDPWD and won't work if it's not set

    • @oo-bb4qs
      @oo-bb4qs 11 місяців тому +4

      This should be top comment

    • @your-mom-irl
      @your-mom-irl 11 місяців тому +4

      Vid literally goes over 'cd -'

    • @oo-bb4qs
      @oo-bb4qs 11 місяців тому

      My excuse is that any engagement is good engagement and also I mean it helps navigating the man page

  • @worgenzwithm14z
    @worgenzwithm14z 11 місяців тому +23

    You can use direnv and the nix package manager to make it so that cding into your project directory puts everything you need for that project into your path (like node or python at a specific version), until you leave that directory

    • @gunstorm05
      @gunstorm05 11 місяців тому +6

      This sounds like the most magicky of magic

    • @TimoWelde
      @TimoWelde 11 місяців тому +1

      That sounds amazing. Have to try it

  • @ehrichweiss
    @ehrichweiss 11 місяців тому +2

    That shopt -s autocd reminds me of the old Commodore Amiga's way of handling the cd command. That said, it's been almost 25 years since I used that intuitively so I doubt I'd be able to get back into the swing of using that again but I might give it a shot at some point.

  • @TheCode-BrandedBeard
    @TheCode-BrandedBeard 11 місяців тому

    Great info, I had no idea about the autocd option, will definitely be using it now.

  • @1upgarage84
    @1upgarage84 11 місяців тому +4

    This is an incredibly helpful tutorial! Thank you

    • @nixtricks
      @nixtricks  11 місяців тому

      Glad it was helpful!

  • @CreachterZ
    @CreachterZ 11 місяців тому

    Loving these!

  • @KerboOnYT
    @KerboOnYT 11 місяців тому

    Neat video. I've been a Linux user since about 1995 and had never used any of cd's command arguments. The -P looks useful

  • @itsPenguinBoy
    @itsPenguinBoy 11 місяців тому

    Tab to complete a path is not particular to CD but it is where I use it most and it is a lifesaver

  • @brickviking667
    @brickviking667 11 місяців тому +1

    Huh! There's things in this video I didn't know even though I'd been using it since 1990.

  • @naranyala_dev
    @naranyala_dev 11 місяців тому +1

    Thank you, more about the little things useful in daily basis

  • @ytdlgandalf
    @ytdlgandalf 11 місяців тому +2

    CDPATH is so underrated.

  • @x0rZ15t
    @x0rZ15t 11 місяців тому +1

    Your channel is awesome! 🔥🔥🔥
    Here is one tip I have used for years now, execute ls automatically once you CD into a directory (tested in ZSH only).
    function chpwd() {
    emulate -L zsh
    ls -alh
    }

  • @Jokerlevin
    @Jokerlevin 11 місяців тому

    Got sub number 1000. Great vid!

    • @nixtricks
      @nixtricks  11 місяців тому +1

      I salute you, thanks! I din't expect any of this so soon to be honest :)

  • @nyverinorlyth9555
    @nyverinorlyth9555 11 місяців тому +2

    Since cd is a built-in all features are dependent on the implementation of the shell you are using. For example as a school project they made us create a shell, and we also had to write a cd command, as such some features weren't present and some extra ones we added.

  • @bazoo513
    @bazoo513 11 місяців тому +1

    ~ 0:15 - I beg to differ to "there's no manual". As _cd_ is a shell built-in, the command and the mirriad of environment variables affecting its behavior and other quirks are all covered in the shall (bash, csh, zsh...) manuals.
    Update: note so self: *stop commenting before watching the whole video!*

    • @unbekannter_Nutzer
      @unbekannter_Nutzer 11 місяців тому

      That's what the `help cd` command does - it displays the paragraph from the bash man page, which is much more convenient, than browsing through the whole bash manpage.

    • @bazoo513
      @bazoo513 11 місяців тому

      @@unbekannter_Nutzer Yes, but without all nice details, like CDPATH or coloring control.

    • @unbekannter_Nutzer
      @unbekannter_Nutzer 11 місяців тому

      @@bazoo513 CDPATH is covered in the 1POSIX-man page for cd.

  • @Prakyy
    @Prakyy 11 місяців тому

    Yeah this channel is gold

  • @mcpr5971
    @mcpr5971 11 місяців тому +2

    my tip is to use globs in middle of a stem. say you have /mnt/music/metal/Metallica:
    cd /mnt/music/*/Metallica will get you there with fewer characters. Or if something is a long name but has a unique beginning or ending: cd /mnt/music/metal/*ica will work as long as another dir doesn't have the same last 3 letters.
    Another one: rm -rf /mnt/music/*/{Metallica,Megadeth} will remove both of those subdirs in only one command

  • @jamtea388
    @jamtea388 11 місяців тому +1

    Wow, this person has a great voice!

  • @AlexEvans1
    @AlexEvans1 11 місяців тому +1

    It has a man page on my system and most UNIX/Linux systems I have used in the past 30+ years

  • @su1nta
    @su1nta 11 місяців тому

    Which de are you using?

  • @dandiyuanaridianto4391
    @dandiyuanaridianto4391 11 місяців тому

    anyone knows why i can't use shopt? im running in wsl

  • @DoBetter.g
    @DoBetter.g 11 місяців тому +1

    My guy. This* guy’s voice literally forced me to absorb the content lmao

  • @TimoWelde
    @TimoWelde 11 місяців тому

    Could you make a video to share your desktop environment with all the configs, tools, etc?

    • @nixtricks
      @nixtricks  11 місяців тому

      ...and done - published it a couple of hours ago :)

    • @TimoWelde
      @TimoWelde 11 місяців тому +1

      @@nixtricks no freaking way. Thanks mate!

  • @uplink-on-yt
    @uplink-on-yt 11 місяців тому +3

    There is a manual. It's the one for your shell, e.g. man bash.

  • @terror403
    @terror403 11 місяців тому +1

    amazing voice !

  • @laurenlewis4189
    @laurenlewis4189 11 місяців тому

    `cd` is in the shell builtins. The man page is `man bash` under the "SHELL BUILTIN COMMANDS" heading (or for the cool kids: `man zshbuiltins`).

  • @theknarf
    @theknarf 11 місяців тому

    The "mkcd" command was the only one I didn't know xD

    • @nixtricks
      @nixtricks  11 місяців тому

      That one is actually an alias (you can see the whole thing in my bash alias video) - but it seems to be a popular one, just found it in zsh with the same name :)

  •  11 місяців тому +1

    You can also use ~username/dir as a shorthand to go to someone else's home directory. Not very useful on your own computer, but I use it a lot on servers 🙂

  • @PaulJohnsonM
    @PaulJohnsonM 11 місяців тому

    I believe in Windows, _cd_ with no arguments will display your current directory (in case you don't have it in your prompt). Also, in Windows, for each directory above the parent directory, you can add another dot to refer to it. That is to say, "cd ...." will move you up three directory levels.

    • @AnxiousFrenchFries
      @AnxiousFrenchFries 11 місяців тому +1

      zsh does it by default, ... equals to ../../ and .... is ../../../ and so on

  • @astrolemonade349
    @astrolemonade349 11 місяців тому

    What's the name of the text font from your Tilix terminal?

    • @nixtricks
      @nixtricks  11 місяців тому +1

      That's MonoLisa (Medium / 12)

  • @mkostrikin
    @mkostrikin 11 місяців тому +1

    Knowledge of ancient civilization

  • @GoonyMclinux
    @GoonyMclinux 11 місяців тому +2

    CD doesn't have a man page because its built into the shell, commands are covered by the shells man page.

    • @RyanMcQuen
      @RyanMcQuen 11 місяців тому

      Or by 'info'.

    • @apmcd47
      @apmcd47 8 місяців тому

      @@RyanMcQuen but nobody uses info

  • @suryar3173
    @suryar3173 11 місяців тому +1

    Which is your Linux distro???

    • @nixtricks
      @nixtricks  11 місяців тому +2

      It's Pop!_OS with a bunch of GNOME Shell Extensions

  • @Sollace
    @Sollace 11 місяців тому

    On my system cd does have a man page, so I guess it depends on what distro you're using.

    • @unbekannter_Nutzer
      @unbekannter_Nutzer 11 місяців тому

      My Xubuntu installation has it too, section 1Posix, maybe from a package I additionally installed from the repo.

  • @dramaoppa7099
    @dramaoppa7099 11 місяців тому +2

    but cd did have a man page when i tired it.I am using Fedora

    • @nixtricks
      @nixtricks  11 місяців тому +2

      Hmm, that's interesting - I won't be able to test this myself for another couple of weeks, but would be curious what "type cd" returns in Fedora. And also whether "man builtins" has any more information...

    • @dramaoppa7099
      @dramaoppa7099 11 місяців тому +1

      @@nixtricks It also works on Endavour OS or that might be the case because I am using fish shell

    • @jebk3447
      @jebk3447 11 місяців тому +1

      it works on manjaro also

    • @daxtheduck4686
      @daxtheduck4686 11 місяців тому

      vanilla arch, cant find a man entry (zsh)

    • @danielkruyt9475
      @danielkruyt9475 11 місяців тому

      @@daxtheduck4686 On Arch with mandoc I have a man page for cd, do you use man-db (default)?

  • @Calslock
    @Calslock 11 місяців тому +2

    Welp, now you've showed with $! designator, why not making a video about all designators from history expansion? There's some back magic there.
    For curious !$ is the same as !!:$, so take last argument ($) from (:) last entry in history (!!). If you wanted to do the same with command one before it'd be !-2:$. If you want to only take first argument it'd be !!:1 or !^. Simple!

  • @Joe3D
    @Joe3D 11 місяців тому

    tilde and wave dash are not the same unicode character.

  • @Erlisch1337
    @Erlisch1337 11 місяців тому

    cd eez nuts

  • @JohnWasinger
    @JohnWasinger 11 місяців тому +1

    1:21 tree / -L 1

  • @feechuong
    @feechuong 11 місяців тому +1

    wait! so `mkcd` is a thing !? I have created my own function in Fish, named mkcd too !! LOL

    • @nixtricks
      @nixtricks  11 місяців тому +1

      And I just found a similar function in zsh with the same name :D Well, mine was a one-liner I created for the bash aliases video.

    • @feechuong
      @feechuong 11 місяців тому

      @@nixtricks coool!! 😆😆

  • @keira_churchill
    @keira_churchill 11 місяців тому

    You forgot to mention "cd -" which takes you back to the directory you just changed from.

    • @syntro6607
      @syntro6607 11 місяців тому +2

      That was shown at 2:51

    • @nixtricks
      @nixtricks  11 місяців тому +1

      This timecode is the answer to many such comments :) I knew I talked about it on the symlinks video, but honestly started doubting that I mentioned it here at all...

    • @keira_churchill
      @keira_churchill 11 місяців тому +1

      @@nixtricks Oops, you're quire right - there it is. I just missed it somehow. Nice job. :)

  • @AndrewStannardMagic
    @AndrewStannardMagic 11 місяців тому

    If you cd somewhere, and want to return to the place you came from "cd -" will change you back

  • @scottmiller2591
    @scottmiller2591 11 місяців тому

    It's a bash build in - it's under `man bash`

  • @rogermiller5411
    @rogermiller5411 11 місяців тому

    This guy sounds like a villain I'd be a minion for. 🙈

  • @NostraDavid2
    @NostraDavid2 11 місяців тому

    I did not know about "cd !$", but you did not know about "cd -" to go back to the previous location.
    Cd to some absolute path, now cd - to go back.

  • @RenderingUser
    @RenderingUser 11 місяців тому +1

    What are you talking about
    I get a man page for cd

  • @randylejeune
    @randylejeune 9 місяців тому

    cd is obsolete. To change directories now use dvd /home/user.

  • @Axlefublr
    @Axlefublr 10 місяців тому +1

    shopt ahahaha

  • @astralxing988
    @astralxing988 11 місяців тому

    cd ..

  • @srivathsansudarsanan3372
    @srivathsansudarsanan3372 11 місяців тому

    Me who uses chdir guess I'm above you

  • @crapphone7744
    @crapphone7744 11 місяців тому

    No man page? Good test for minimum IQ to use the OS. 😅

  • @GottZ
    @GottZ 11 місяців тому

    90% of what you explained was just plain shell expansion and not a cd feature.

    • @nixtricks
      @nixtricks  11 місяців тому

      Once my video on expansions drops, I can only hope that the people who were exposed to some of this stuff early on will have an easier time getting it.

  • @sersys1
    @sersys1 11 місяців тому +1

    GOD BLESS XEROX for inventing gui so I don't have to deal with this bs.

    • @your-mom-irl
      @your-mom-irl 11 місяців тому +3

      Dude i was introduced to computers with windows 98. GUI user my whole life and at first i thought the terminal was weird and clunky, but now i find navigating on the terminal easier than GUI based file explorers. Problem is now i am annoyed when i am forced to use the mouse for anything lol

    • @guruware8612
      @guruware8612 11 місяців тому

      i was starting with dos 3.2, imagine this without a prompt... (the time i knew where my files are, now an OS thinks it has to tell me where i should put what)
      gui's are nice to look at (more or less), but they slow down things, ie. try to delete a big folder with lots of sub-folders with lots of files:
      a) you can watch a giu showing flying papers for some minutes (which possibly blocks your file-explorer)
      b) do it by del-command in that scary black window, and stuff is gone in seconds
      OT-PS:
      overall, gui's get more and more bad every year, flicker, non-sensical or child-ish icons, popups, way too big intrusive asocial media icons - some call this "modern" interfaces

    • @sersys1
      @sersys1 11 місяців тому

      @@guruware8612 complaining about how the gui looks is the same as complaining about the font size being too small to read. Just customize it. Guis are indeed getting worse and worse but not for any of the reasons you've listed. The time it takes to learn how to use cmd efficiently, you might as well download a skin for your gui.
      I rather have 10 flying papers while laying back clicking on the nice big icons, than painfully typing out CD commands, then sort files with command, then type all the filenames one by one. I'd like to see you do 10 simultaneous read-write operations performed on your precious dos 3.2 to begin with.
      If you let the OS tell you where to put your files and make you its gimp that's a you thing, not a general problem. Even if you have only 1 HDD you can still partition off the OS and place all your files on the remaining partition where ever you want it.

  • @elrandohorse
    @elrandohorse 11 місяців тому

    cd /eeznuts

  • @marcoose777
    @marcoose777 11 місяців тому

    [spongebob@krabbypatty ~]$ info cd
    POSIX programmers manual.....
    ps
    for cd to work without a path the HOME environment variable has to be set
    echo $HOME