I never really understood how to use the command line. I kind of got it in principle but not in practice.
So I taught myself the basics by reading this mini- book on Learn Code The Hard Way. Itās awesome. The book is an alpha release and may have some errors but I didnāt spot any. Would I, though?
Here are my thoughts and notes I jotted down while learning this. Itās fairly stream of conscious and certainly less accurate or helpful than the aforelinked mini-book. If youād like to learn it along with me, maybe this could possibly be useful to you. Itās a little different on a Windows computer, but if youāre on a Mac or Linux computer, this should all work for you too.
I wonder how much stuff Iām gonna have to memorize.
Iām in the terminal! Iām in Terminal!
Iām learning bash. Apparently my nerd friends will tell me to learn zsh instead. I wonder if my actual nerd friends would.
Ok I think I get the whole cd
and ..
thing. If youāre in a directory you can cd
into any subdirectory or cd ..
to go back one. From the downloads directory I can cd ../documents
to get to the documents folder. That goes up one and then back down into another.
I wonder if up and down are the right words to use there. In the Finder I think of it more as left and right, in the column view.
So far basically all Iām doing is making folders and moving through them. Loving it.
Whoa, I just deleted john! john was a directory I made. rmdir john
is deadly.
At this point, the guide is saying if I want to I can take a break and come back tomorrow. It thinks Iām weak. I can do this.
I like that I can do mkdir -p i/like/icecream
and itāll make all three of those folders from scratch.
I wonder if I rmdir i
, if itāll remove all the subfolders too. Ohh cool, it wonāt let me. Thatās nice.
Itās kind of weird that when I rmdir something
, it doesnāt go to the trash, itās just gone.
pushd
and popd
are kind of baking my noodle (as Corey likes to say (note: this used to link to a podcast, but that podcast is gone). Is this like the popping and locking of the programming world? Maybe I should take a break.
OK so youāre in a directory. You pushd to/a/folder
and now youāre there, but you sort of bookmarked where you were. If you want to go back there you popd
. If you push somewhere, then push somewhere else, then pushd
over and over you can cycle back and forth between them. If you popd
over and over you can go back through the stack (from most recent to least recent I think?) of your pushd
s.
I love putting things in code brackets
itās really easy in Markdown/Calepin. I might be misusing it.
Ahh and the stack isnāt hidden! Thatās whatās printed/returned when you pushd
. I think Iām wrapping my head around this. Not sure if Iāve explained it well or anything, though.
Iām up to chapter nine. I just made an empty text file by writing touch iamcool.txt
. Hey, your words.
I just wrote touch butts.mp3
and it made a song called Butts that Iām gonna be sending out to radio stations first thing tomorrow.
Ok so if I want to make a copy of iamcool.txt called awesome.txt itās as easy as writing cp iamcool.txt awesome.txt
! Okay! (In fact I am getting tired).
Iām slowly piecing together that -r
does⦠well, something. I donāt know. If I mkdir afolder
, I can then cp -r afolder ~/Desktop
to make a copy of that folder on my desktop. But to copy awesome.txt to my desktop I just write cp awesome.txt ~/Desktop
. So that -r
I guess makes it work for a directory.
Hmm so now Iām moving files. But it sounds like renaming to me. mv awesome.txt lame.txt
will basically rename it from awesome to lame. I suppose itās moving the data from one file to a new one? Like, when you die your soul leaves your body and enters a baby just as itās being born? If you believe in a very literal, specific form of reincarnation?
I know how to open a file in vim, and I even know how to do some basic vim commands, but I have no idea how to save a file and exit vim.
Ok I took a break. Itās two days later from when I started this. Iām happy that I still remember the commands Iāve learned this far.
I donāt know how to delete files but I can sort of do it by renaming one file into something else that exists, and it basically disappears I think.
I can also move a file from one folder into another folder with this. mv hello.txt afolder/hello.txt
moves that file into the folder (though it doesnāt create the folder). So itās moving and renaming.
Hmm.
Okay so now Iām using the less
command to view the contents of text files. it seems to work just fine. I press q
to exit. If itās a long document I can page through it with w
(up) or space
(down), one page-worth at a time. Got it.
I can read markdown files in the terminal pretty nicely. Iām gonna navigated to my Calepin folder and open up this draft file. cd ../Dropbox/Apps/Calepin
, ls
, less cli.md
wow there it is! Ha! This is fun.
I just whipped up a background image for my terminal. I wish it could tile and not just stretch. Looks good as long as I donāt resize the window.
Now Iām cat
ting things. That just sort of displays the contents of the text file in a slightly different way than less
. So exciting.
Oh thank goodness rm
exists. I didnāt like that other way of deleting files.
OK so I can rm hello.txt
to delete it but I canāt use rm
to delete a folder (thatās what rmdir
is for). Oh wait, I can, I just have to do some weird business. rm -rf folder
works. Yoosh how do I remember that? Wait a moment. rm -rf
can delete folder even if they have stuff in them. That one ups rmdir
. This is ārecursive deletingā.
I like this possibly self-delusional quote from the mini-book:
Now we get to the cool part of the command line: redirection.
I kinda knew about the |
but not the <
or >
.
Oh! This is fun.
I just had a breakthrough wherein I learned how to save and close from vim, so Iāve just been playing in that for a little while. Itās :wq
! So simple!
Ok, wildcard matching⦠ya yah.
Ex, ls *.txt
will list only the text files. thatās handy. This command surprised me: cat *.txt > bigfile.txt
took all of the txt files and combined them into one new one, which it also created. Then rm *.txt
removed all of the txt files. Thatās vicious.
Ok so this is a fun thing. I just realized I can open tabs in the Terminal! So Iām gonna write this post in one tab and learn things in another. Whoa. Although the switch-tab shortcut is kind of unwieldy (command-shift-] or [). I can learn it.
So I can run a command like grep hello *.txt
and itāll find the string āhelloā in all the txt files and then return a list of all of the lines from all of the txt files that have that string in them. Useful. And if I want, I can pipe that to less
and itāll be the same thing, but easier to flip through (with space and w).
I thought I was done when I reached the section on man
, which looks up the manual for any given command, but thereās a whole nother section. Great. I can handle some more knowledge.
Iām up to chapter 21. Weeks have elapsed. Iām not sure exactly what I just did and I can only hope it wasnāt bad. one of the thrills of using the command line is that I can completely destroy my whole computer at any point by writing the wrong thing.
In this chapter I ālooked at my environmentā and then set a variable and then printed out the variable. But where is this variable? In my environment? I feel like I donāt want it to be there anymore but donāt know how to get it out. Environmentalism! (I actually echo
ād it, not print. For what itās worth.)
Now it wants me to research online āhow you change your PATH for your computerā and do it all in the terminal. I think Iām just gonna skip this one.
Ah! Chapter 22 to the rescue. unset
will flush that variable. Cool. Iām so relieved. I wonder if I can unset
something important. That would be very unpsetting.
āYou have completed the crash course. At this point you should be a barely capable shell user.ā ā Nice. I feel that way. It feels pretty great.
The conclusion links to this official bash reference. Also this cheat sheet. Maybe Iāll tackle those next.