Hardscrabble šŸ«

By Max Jacobson

See also: the archives and an RSS feed

Introducing myself to the command line

February 26, 2012

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 pushds.

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 catting 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.

Note: I don't have comments or analytics on this website, so it's hard to tell if people are reading or enjoying it. Please feel free to share any feedback or thoughts by shooting me an email or tagging me in a post on Mastodon @maxjacobson@mastodon.online.