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 - Наука та технологія
"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.
It's also possible to navigate through arguments history using " Alt + . "
holy shit i will absolutely be using “sudo !!” in the future tysm
Absolutely - that's an example of history expansion. I'll do a full video on all types of expansions in the near(-ish) future :)
for sudo !! is more interessting
the -P is important to prevent infinitely looping through symlinks, since you can have a symlink pointing to a parent directory.
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.
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.
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 :)
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.
"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.
'help cd' will usually give you the same thing as your shells manual
/ cd↩️
and for zsh the manpage is 'man zshbuiltins'
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
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
Isn't that what 'cd -' does?
@@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
This should be top comment
Vid literally goes over 'cd -'
My excuse is that any engagement is good engagement and also I mean it helps navigating the man page
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
This sounds like the most magicky of magic
That sounds amazing. Have to try it
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.
Great info, I had no idea about the autocd option, will definitely be using it now.
This is an incredibly helpful tutorial! Thank you
Glad it was helpful!
Loving these!
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
Tab to complete a path is not particular to CD but it is where I use it most and it is a lifesaver
Huh! There's things in this video I didn't know even though I'd been using it since 1990.
Thank you, more about the little things useful in daily basis
CDPATH is so underrated.
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
}
Got sub number 1000. Great vid!
I salute you, thanks! I din't expect any of this so soon to be honest :)
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.
~ 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!*
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.
@@unbekannter_Nutzer Yes, but without all nice details, like CDPATH or coloring control.
@@bazoo513 CDPATH is covered in the 1POSIX-man page for cd.
Yeah this channel is gold
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
Wow, this person has a great voice!
It has a man page on my system and most UNIX/Linux systems I have used in the past 30+ years
Which de are you using?
anyone knows why i can't use shopt? im running in wsl
My guy. This* guy’s voice literally forced me to absorb the content lmao
Could you make a video to share your desktop environment with all the configs, tools, etc?
...and done - published it a couple of hours ago :)
@@nixtricks no freaking way. Thanks mate!
There is a manual. It's the one for your shell, e.g. man bash.
amazing voice !
`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`).
The "mkcd" command was the only one I didn't know xD
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 :)
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 🙂
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.
zsh does it by default, ... equals to ../../ and .... is ../../../ and so on
What's the name of the text font from your Tilix terminal?
That's MonoLisa (Medium / 12)
Knowledge of ancient civilization
CD doesn't have a man page because its built into the shell, commands are covered by the shells man page.
Or by 'info'.
@@RyanMcQuen but nobody uses info
Which is your Linux distro???
It's Pop!_OS with a bunch of GNOME Shell Extensions
On my system cd does have a man page, so I guess it depends on what distro you're using.
My Xubuntu installation has it too, section 1Posix, maybe from a package I additionally installed from the repo.
but cd did have a man page when i tired it.I am using Fedora
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...
@@nixtricks It also works on Endavour OS or that might be the case because I am using fish shell
it works on manjaro also
vanilla arch, cant find a man entry (zsh)
@@daxtheduck4686 On Arch with mandoc I have a man page for cd, do you use man-db (default)?
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!
tilde and wave dash are not the same unicode character.
cd eez nuts
1:21 tree / -L 1
wait! so `mkcd` is a thing !? I have created my own function in Fish, named mkcd too !! LOL
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.
@@nixtricks coool!! 😆😆
You forgot to mention "cd -" which takes you back to the directory you just changed from.
That was shown at 2:51
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...
@@nixtricks Oops, you're quire right - there it is. I just missed it somehow. Nice job. :)
If you cd somewhere, and want to return to the place you came from "cd -" will change you back
It's a bash build in - it's under `man bash`
This guy sounds like a villain I'd be a minion for. 🙈
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.
2:59
What are you talking about
I get a man page for cd
cd is obsolete. To change directories now use dvd /home/user.
shopt ahahaha
cd ..
Me who uses chdir guess I'm above you
No man page? Good test for minimum IQ to use the OS. 😅
90% of what you explained was just plain shell expansion and not a cd feature.
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.
GOD BLESS XEROX for inventing gui so I don't have to deal with this bs.
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
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
@@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.
cd /eeznuts
[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