Transcript from the "CLI Search" Lesson
>> So let's get on to some common tips and tricks of command like going around the command line. You've probably already see me use some of these because they're so ingrained in my usage of the command line that I literally cannot separate them from my fingers. So, let's just yeah, let's get into it.
[00:00:18] So right now I'm inside of Ubuntu. Let's say I'm in the root directory, right, which I'm in the root directory now, pwd, you can see I'm in the root directory. Let's say I wanted to get to Ubuntu's home directory, which is /home/Ubuntu. That's the directory that belongs just to Ubuntu and not anyone else on the system, right?
[00:00:37] So if I have like ten users, each one of those users will have their own home directory. So you can say, hey, take me to my home directory by saying cd and then ~. So that's the tilda key, which is next to your 1 key. It's also on the same key as the backtick.
[00:00:54] So if I say cd ~ and I hit pwd, I'm now in my home directory, right? So that's what tilda represents. It represents your own home directory. And that's just really useful to make sure that you're always going to your home directory. And you can also do relative paths off of it.
[00:01:14] So if I say cd ~/snap, right, you'll see here that it takes me to home Ubuntu snap, right? And that's another thing to worth noting that I haven't really called out yet. So I have this, it's called a prompt here, this little thing that comes before the dollar sign.
[00:01:35] So after the colon here, it's actually showing me all the time my current path, where I am in the file system. But it's using tilda right? So if I see tilda here, this means I'm in my home directory, but if I go up one, it will give me /home, /~, ~/snap, right?
[00:01:56] So it's actually changing that every single time inside of my prompt, and I'll show you at the end how to set your own prompts. You can mess with that however you want. Okay, so that's tilda. We talked about slashes well already, so if I just say cd/, that's going to take me all the way to my home directory, or not my home directory but the root directory of the project.
[00:02:17] So that's what slash does for you. So some people get nitpicky about this. People want to call this forward slash because this is backslash. It's actually technically just slash and backslash. I don't know why people get all bent out of shape about it. If you call it a forward slash, I know exactly what you're talking about.
[00:02:43] I just hate people that are nitpicky for dumb reasons. So yes, it is redundant if you call it forward slash and no, no one should care. So that's my mini forward slash rant. In particular, computer scientists are the people that just love to be just retentive about the dumbest things.
[00:03:05] And no, I'm not upset about it. Okay, so I'm gonna go back to my home directory. The next thing I want to show you again, this is one that I use all the time is your up and down keys. So if I start hitting up on my keyboard, it's gonna start cycling back through my history of keys of commands that I've run.
[00:03:21] So this is really useful if I wanna go back and say, what did I run ls with, right? So if I go back here it's like, I always run it with lsah, right? So I can kind of like scroll backwards through my history and I could run it again if I wanted to.
[00:03:36] And up will take you previous commands, down will take you forward in commands. But another thing to keep in mind is that means that all of your commands are being stored on your computer, right? So it actually keeps track of your bash history. So if you're putting passwords into your command line, those will get stored in your bash history if you're not careful about it.
[00:04:03] Which is a bad thing cuz that means anyone that can see your bash history can see your password. And if you're on a multi user server, that's a bad thing cuz people will be able to see your secrets. So I call that out just so you're aware that whatever you put in the command line is gonna be saved in your bash history, and you can go modify your bash history, just be aware that it's there.
[00:04:28] Okay, so let's do the next one. We're gonna do tab completions, you probably see me do this because I literally cannot, I physically cannot stop myself from doing this. So I'm gonna type cd and I wanna go into the home directory. So if I type cd and then I type h, then I hit tab.
[00:04:49] [SOUND] I'm not in the root directory, I wanna be in the root directory. And I type h and I hit tab. Notice it just finished it for me. So again, I'm gonna hit cd and then h and then tab. And it finishes out home for me. The way it does that is it looks and says, all right, what could you possibly be talking about, right?
[00:05:12] So you've kind of limited down, there's only one directory in here that starts with h and it's like, there's only one possibility here, I'm gonna finish it for you. So that's called tab completion, and I literally cannot stop doing it. And then now, it knows that I'm inside of this directory.
[00:05:29] And let's say I don't remember what's actually in here. If I hit tab twice. Actually, there's only one directory in here, so I don't have to type anything. So it already knows you could only possibly go to one place, I'm just gonna finish that for you. And then here if I hit tab twice, it'll show me all of the possibilities.
[00:05:48] So those are the only directories that exists in this and it's actually smart enough to know it's like files versus directories. It's like okay, I wanna go to the snap directory. There's only one thing in there and now there's three things in here, so you can keep doing this so on and so forth, right?
[00:06:07] So that's very useful as well. This will also work for things like ls, so a cd, it's smart enough to know just directories. But if I do ls, notice it's showing me files as well. And that's because ls can be used just with files and not just with directories as well.
[00:06:28] So it actually in that case, it's not filtering out all the directories. Cool. So that's part of tab completion, that's tab completions for paths. But let's say I wanted to do it with programs. If I type py, and then I hit tab, it'll show me actually all of the potential programs that are available to me that I can run with py.
[00:06:52] So in this particular case, I can run python3, right? So let's say that's what I wanted to write. So I put pyt, and then tab, and then I get python3. And it stops there because it knows like, alright, if you put pyt, you could be running Python 3, you could be running or Python 3.6 or Python 3.6m, right?
[00:07:15] It knows that there's a couple possibilities there. So it'll complete as much as it can as well. Cool. So that's tab completions. Some programs are smart enough to have their own tab completions as well, Git is one of them. Some programs have their own tab completions as well and Git is a good example of that.
[00:07:41] I'm not gonna be going into Git at all in this particular course. The first reason being is there's already a fantastic in depth course for the masters on Git from Nina Zakharchenko. So you should watch that if you want to get into Git. But I do want to show you an example that if I say git, and then Git has a bunch of sub commands, right?
[00:08:00] So one of them is describe. If I type des and I type tab, it actually knows that Git has another thing called describe that I can put in there. So not everything has tabs completions, not all programs have tab completions. The program has to provide those tab completions to bash before those work.
[00:08:22] So I'll just tell you that I almost never use them for programs because I can never remember which one has tab completion and which one does not. I will use it for Git because I'm positive that Git has it but I would venture to say that most programs don't have tab completions.
[00:08:45] Cool. So let's talk about reverse search. This is another one that when my coworker at Reddit showed me, it actually might have even been Nina when we worked together at Reddit. Showed me how to search my bash history quickly. If you hit Ctrl+R. It does this reverse I search here and I started typing, let's say I wanted to find the last ls command that I ran.
[00:09:14] I just start typing something and it's going to start completing based on things in my bash history So you can see here, I've typed in ls space, that's what I've put in there. And this is the last thing that I ran that matches that. And so if I just hit Enter now, it'll run that from my Bash history.
[00:09:34] I use that constantly as well, because my Bash history is 10 million miles long, because I've been running the command line forever. And this will allow me to search back through and find really long complicated commands that I've run that I don't want to just hit up to scroll through until I eventually find.
[00:09:53] So another thing to know about the reverse search, so if I hit Ctrl+R again, ls space and let's say, it was another thing that was ls space and wasn't this one, so I wanna go further back in the history. You just hit Ctrl+R again and you'll keep going back through your history as long as you keep hitting Ctrl+R.
[00:10:15] If you just hit like right or left on your keyboard, it'll just drop you into the normal terminal with that pasted in. So another one that I just use absolutely constantly.