6 ms·
Linux command line for you and me
- rafa123456 4y ago
- StuckDuck 4y agoSomething to note which wasn't mentioned here is that `cd` is not a binary executable, but instead a shell command. You won't find it anywhere in /bin, /sbin and so on. That's also why `sudo cd ` doesn't work, `sudo` will try to execute `cd` as root but it's not an executable so it won't find it. (tip: do `sudo -i` instead)
- saagarjha 4y agoMany platforms will ship a (mostly) useless cd binary.
- rjsw 4y agoAn executable of 'cd' wouldn't do anything useful, it could only change its own idea of working directory then exit, the shell or other executable that invoked it would be unchanged.
- stjohnswarts 4y agowhy couldn't it call an api in the shell to change directory?
- remram 4y agoWhat would that improve? And what happens if you call it from another shell?
- stjohnswarts 4y agoI never implied that it was better or worse but that it was possible
- remram 4y agocd is a good example of something that has to be a built-in command, because it makes the shell itself do something (change its current working directory). Other commands in that category are umask, the job control ones (fg, bg, wait, ...), the ones setting variables (read, printf), and the control flow ones (if, while, break, return, exit, ...). A lot of commands are also built-in for convenience but don't strictly need to be.
- nixcraft 4y agoWe can use the `compgen -b` (`help cd` to get short help or `man bash-builtins` to read man pages) to list all internal bash commands. For example, we can use the `type -a cmd1` or `command -V cmd1` to see if `cmd1` is built-in or external binary.
- emmelaich 4y agoAnd yet ... https://unix.stackexchange.com/questions/50058/what-is-the-point-of-the-cd-external-command https://unix.stackexchange.com/questions/50058/what-is-the-p... Yes, it's idiotic.
- version_five 4y agoThe question and highest rated reply there are very interesting and worth reading (at least for trivia value), thanks for that. I don't understand what you meant is idiotic though
- ianai 4y agoAs that comment said, it’s there so utilities can use it for checking the return of “chdir()”. It’s at best a name conflict with a shell command with a well established past and use. At worst, it probably enables some bad code smells or outright malicious code.
- emmelaich 4y agoI've seen questions about sudo (for instance) that ask why does "sudo chdir" not work. By idiotic I think it's a mistake to blur the line between shell builtins and external commands.
- davidpfarrell 4y agoTIL that mac has an executable `cd` command: $ cat /usr/bin/cd #!/bin/sh # $FreeBSD: src/usr.bin/alias/generic.sh,v 1.2 2005/10/24 22:32:19 cperciva Exp $ # This file is in the public domain. builtin `echo ${0##*/} | tr \[:upper:] \[:lower:]` ${1+"$@"} Although it didn't behave as expected when invoked ...
- cperciva 4y agoRequired by POSIX, and it does exactly what it's supposed to do: Changes directory in the process (not the parent shell). This isn't completely useless -- you get an exit status and potentially error messages out. (And yes, I'm the cperciva in that version tag.)
- davidpfarrell 4y ago> And yes, I'm the cperciva in that version tag. Makes my day that you could discover this post - Thanks so much for chiming in !
- simonebrunozzi 4y agoYou never cease to amaze me, Colin. Big fan of yours. Keep rocking.
- deleted 4y ago[deleted]
- orsenthil 4y agoVery nicely written and comprehensive website. It is handy as we often work with docker and linux command line these days.
- firstSpeaker 4y agoAnyone knows of something similar for MacOs?
- r0f1 4y agoI've created my own list, which is a more condensed version: https://github.com/r0f1/linuxhelp https://github.com/r0f1/linuxhelp
- photochemsyn 4y agoNice basic introduction. For anyone wanting a comprehensive overview of each command, with examples (and generally more readable than man pages), the computerhope site is good. For example here's wc: https://www.computerhope.com/unix/uwc.htm https://www.computerhope.com/unix/uwc.htm
- unixbane 4y agoIt's UN*X. The one thing it actually does good is having documentation on the system, instead of some weirdo's web page covered in ads. Type man <cmd> or info <cmd> in the shell. Also, for most of the world population, this will be a few seconds faster since it avoids loading a web page.
- codegeek 4y agoThese are always fun and informative. One of the cool options that a lot of people don't know about the "cd" command is the : cd - This takes you to the last directory you were in (not the same as cd .. which takes you one level up)
- 5e92cb50239222b 4y agopushd / popd are also useful in scripts. For some reason they don't seem to be very well known and I rarely see them in practice. $ pwd /foo $ pushd /bar && pwd /bar $ pushd /quux && pwd /quux $ popd && pwd /bar $ popd && pwd /foo So it's basically a stack.
- GuB-42 4y agoPushd / popd are not POSIX and some shells like ash/dash don't support them. I would avoid them unless you are targeting a specific shell you know supports them (ex: bash).
- harry8 4y ago"targeting" seems like an odd thing to say there. This is an interactive shell command. Does the shell you are using interactively support it seems like a more interesting concern? We don't have to be portable typing commands into a shell. :-) Never seen {push,pop}d in a shell script.
- GuB-42 4y agoGP was specifically talking about scripts, and I see the utility. As an interactive feature, I use the "autopushd" option in zsh, which make all "cd" behave like pushd, and you can also use "cd -n" where "n" is the number of levels you want to go up the stack (so "cd -" is equivalent to "cd -1")
- harry8 4y agooh wow. How did I not see that? Sorry!
- chiefalchemist 4y agoProbably not popular to say here, but it's ironic that the key benefit of computers is the ability to distance the user from complexity and mind-defeating details, yet computers themselves don't - yet? - offer said type of benefits.
- 74ls00 4y agoWhat are you talking about? Of course they do! Try performing any of the operations listed in this article, or making a reply to this comment, by only twiddling the state of the transistors in your CPU.