18 Commands That Will Change The Way You Use Linux Forever

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again everyone and welcome back to lenode in today's video what i'm going to do is teach you guys some command line tricks that i've picked up over the years some tricks that i wish i knew earlier in my career and these are going to be some command line tricks that'll save you time maybe help you work more efficiently and some are just plain fun so let's go ahead and get into my favorite command line tricks for the linux terminal [Music] all right so here i am on my laptop and i'm going to go over some of my favorite tips and tricks right now now i'm going to go over these in no particular order so just stick with me and hopefully you'll find at least some of these helpful maybe you'll add these to your daily workflow that would be really cool let's go ahead and get started now what i'm going to do is start off with a really easy one so i'm going to go into the etsy directory i'm going to change directory into that directory and now that's my current working directory but what if i want to go back to the previous directory that i was just in well to do that i can actually type cd and then dash and that's it i'm just going to type cd dash just like that i'll press enter and i'm back to my home directory now of course you could go back to your home directory by typing cd and then tilde just like this that does essentially the same thing but the difference with cd dash is that it's not specific to the home directory at all so for example if i go into the etsy directory after that let's just say i go into a different directory cd dash takes me back to the previous directory like i mentioned whereas cd and then tilde takes you specifically to your home directory and i'm sure a lot of people know about cd tilde but i guess if you didn't already know that then well now you do so what i'm going to do right now is just clear my screen so i'll type clear and when i press enter it's going to clear the screen actually i'm not going to press enter but i am going to clear the screen but wait a minute how did i do that so if you have output on your terminal and then you hold ctrl and press l that actually clears your screen it's that easy and yeah you could type clear to clear your screen that does work but when you do that you're using quite a few more keys than when you just simply hold ctrl and press l which i find to be the easiest way to do it so again you have output on the screen just hold ctrl press l and that's it you've cleared the screen now the screen isn't actually completely clear though so for example if i use my mouse and i just scroll up a bit you can see that the output that i had is still there so essentially what control l is going to do is just move the command prompt back to the top of the screen giving you all the space back but the history is actually still there if you use your mouse wheel to scroll up now alternatively you can also type reset as well and that's actually going to reset your shell and i'm scrolling the mouse wheel but i'm not able to scroll why well i reset everything so reset is kind of like clear on steroids it actually empties out everything and legitimately clears your screen in every sense of the word now for me it's just muscle memory to hold ctrl and press l so that's what i do i don't use reset all that often unless there's something wrong with my shell session maybe something isn't acting right if that's the case i might type reset but most of the time i'll hold ctrl and press l that's what i'll do now earlier in the video i showed you cd dash to go back to the previous directory but what i'm going to show you right now is a different way to do the same thing but it's a little bit more flexible what i'll do right now is change directory into the slash var directory but i'm going to go about it a different way instead of typing cd i'm going to type push d and then the directory i want to go into and you can see that i am now in the slash bar directory and that's pretty cool so what i'll do just to illustrate the point is just change directory a few more times now i'm in my home directory and now i'll just go into my.config directory so i've changed directories a few times here it doesn't really matter which directories you change into but what i'm going to do right now is type popd and now back to etsy when you type push d and then give it a directory you'll change into that directory but it's not quite that simple when you change directory into the new directory the previous directory is added to the stack and here you can actually see the stack i'm going into slash var from etsy the push d and poppy commands are actually more involved than this there's more you can do with it but for right now what i recommend that you remember is how to type push d and then pop d and push d remembers the directory that you are in and then pop d will take you back to that directory even if it's not the most recent directory that you are in if you recall cd dash takes you back to the previous directory but push d gives you the ability to remember a specific directory then pop d puts you back into that directory anytime you want to go back there so if you plan on doing some file system navigation and then at the end of that you want to return to a specific directory that's when push d and pop d helps the most so now i'm back into my home directory let's go ahead and continue and check out some more tricks the next trick i'm not going to spend too much time on because it's one of those things that i could actually explain in greater detail but i've already done that i have a whole video about background tasks so i'm going to summarize it here but i'll leave a card right about here if you want to check out my background process video and learn even more about how this works now let's say for example i have a file open i'm going to use vim for some reason this doesn't always work with nano i don't know why but it really shouldn't matter if it's a text editor or h-top or whatever program basically any program that is in the front you know something that takes you away from the command line and what if you want to return to the command line but you don't want to close the program you're in so what i'm going to do is just open up a random file i'm going to choose the etsy ssh sshd config file and let's just say i'm in this file right here and i'm changing some configuration options but i need to work on something else real quick because something came up and i need to go do something else and let's say i've made a bunch of changes to this file i'm not ready to save the file yet because i'm not finished with it but i also don't want to lose it either so how do i get back to the terminal without actually saving the file and also without opening up another terminal window but what we can do is hold ctrl and press z and that effectively minimizes that particular program to the background now it's not quite the same thing as minimizing a program in a desktop environment or a graphical user interface but we're going to pretend it's essentially the same thing because that's the value that i see here you have a program running you don't want to lose it you don't want to lose your work and you do want to return to it but you also don't want to open up a terminal just to get back to the command line this is a great way to do it so now you know how to send things to the background but how do you get it back well that's easy you type fg for foreground and then it comes back it's that simple you hold ctrl z to send something to the background something that's in the front and then fg and that'll bring it back to the foreground so i'll go ahead and exit this and like i mentioned it doesn't really matter if it's a text editor another example of this is h-top and this is a really cool utility for monitoring system resources but the same thing applies here i can hold ctrl and press z it's sent to the background fg is now in the foreground if you want to find out more about how this works then check out the video that i mentioned but at the very least being able to send something to the background and then bring it back to the foreground will probably save you a lot of time just keep in mind that if you close the shell or log out you will lose whatever's running in the background so just make sure you remember that when you send something to the background you should bring it back to the foreground finish what you're doing before you close your terminal window all right so it's time to move on to the next trick and what i'm going to do first is just update my package repository index so i'll type app update just like that and i'll press enter oh wait i forgot to use sudo you know as long as i've been using linux you would think that i'd remember that by now but even after two decades i'm constantly forgetting to run sudo of course i could just run sudo aft update i mean it doesn't take that long to type that command but that's not what i'm going to do what i'm going to do instead is type sudo and then two exclamation marks just like this and then i'll press enter and it worked now check this out when you run sudo exclamation mark exclamation mark the two exclamation marks actually refer to the previous command that you just ran so by running sudo exclamation mark exclamation mark i'm telling the command shell to repeat the most recent command but put sudo in front of it so that way i don't have to worry about retyping the entire command just to run it to sudo this is a great way to repeat the last command but make sure that you have the proper privileges and run it with sudo and again i find myself forgetting to use sudo quite often so this trick right here is something i use at least once a week now another trick is going back through the command history by holding ctrl and pressing r now this is kind of confusing to explain so i think showing you will make more sense so i'm going to hold ctrl i'll press r and now the command prompt is changed to a search field here so what do i do here well what i do is i start typing part of a command that i remember part of any command that i would have ran in the past maybe i want to bring back a long command that i ran in the past but i don't remember the syntax and i just don't want to go through the history file to find it i'll just type what i know of the command so we just ran apt update for example now notice i just typed apt and it's already showing sudo apt update if i hold ctrl and press r again it'll go to the next thing in the search history that meets that criteria and control r will just keep going back through the command history and then once i find the command that i was looking for i rerun it by simply pressing enter in the sudo apt-install h-top command that's what i ran off-camera to install h-top on this computer so there's pop d we ran that one we also ran push d any command that i've ran in the past i could easily run it again with this trick i'll hold ctrl and press c to break out of this and what i'm going to do is show you yet another way to run previously run commands that i think is even better what i'm going to do right now is run history just like this and we see all of my command history which is very useful in and of itself especially if you're a new linux administrator for a company if you're working with a server that has a particular problem maybe it's a problem that's happened in the past and as a new linux administrator it's always a good idea to check the command history because you could find out what people before you have done in similar situations and like i said this is a cool trick in and of itself but that's not actually the trick that i want to show you what i'm going to do is show you how to rerun any command from your history notice that there's a number on the left side of each of these commands so for example i typed this command right here off camera to see whether or not h top was installed on this computer it wasn't so then after that i installed it but what if i want to run this command right here yet again i could just right click and copy it and then i could right click and paste but that's not what i'm going to do what i'm going to do instead is refer to the command by its number but first i'll type an exclamation mark and then directly after that i'll type the number no space or anything like that and us linux people we often refer to the exclamation mark as bang so essentially i'm typing bang 102. and what happens is that it actually shows me the command that's associated with that number and then it runs it so right there i was editing the ssh config file so i could do that again by typing bang and then the number just like before and now i'm back into that config file i think this is a very useful way to use the history but i'm going to make it even better one thing that i think is missing here is the date and time i feel like it's really helpful to have the date and time that every command was run in so that way if i'm looking for a specific command during a specific time period i could find it and have a better idea about the actual command history and when the commands are executed but the problem is we don't have that information here so how do we get it for that we have a very special variable in its hist time format and what i'm going to do is set this equal to a very specific string so i'm going to type percent capital y dash percent lowercase m dash percent lowercase d space percent uppercase t and then another space and adding another space here is really important i'll explain why in a moment but anyway i'll press enter and now that we've done that let's run the history command again now we have a date next to each of the commands right here depending on how your shell is configured it might actually show the same date for everything because maybe a date wasn't recorded when it was first created different distributions set this up differently so this may or may not be a problem for you but at least going forward it's going to have the correct date and time for every command so if you're looking for a specific command from a specific time period you could just scroll through your history and as you can see it shows that information here now one problem with this though is that when you close your shell then the his time format variable is going to go away we set the variable here in this session so even if i opened up a new terminal window it's not going to apply to that particular terminal window it's only for this session so what i want to do is actually make this permanent and the way to do that is we just open up a special file with an editor we can use nano vim it doesn't matter and what we're going to do is open up the dot bash rc file in our home directory and right here we have hist control and that's actually another trick that wasn't on my list but it may as well have been hist control being set to ignore both means it's going to ignore lines that include a space at the beginning so if you want to type a command and you don't want it to show up in the history if this is set you could type a space in front of the command and it won't show in the history so if you are running a command that contains something sensitive it might be a great way to hide that from the history but that's not what we're here for what i'm going to do instead is add the his time format variable right here in the bashrc file so that way every single shell i open will automatically have the settings just like that so i'll hold ctrl and press o to save the file enter to confirm it ctrl x to exit out so from this point forward every terminal window that i open should have the his time format variable set giving me the date and time for each command now earlier i mentioned to not forget to type the space at the end when you're setting the variable and the reason for that is because if you don't then there's not going to be a space in between the date and time and the command so this space right here would not be present because you added a space at the end of that string it's putting that space right here in between the date and the time and the command so if you don't include that things get you know confusing now another command that i want to show you guys is completely useless i mean it's cool but it gives you no practical capabilities whatsoever doesn't enhance your workflow so technically it doesn't even belong in this video but i figured it might be fun to show you and that command is c matrix you have to have this package installed most of the time this will not be installed so you could use your distributions package manager to install this package so i'll press enter and you'll see immediately what it does does that look familiar if you recall in the background i often have a laptop that has this as the screen saver but it's not actually a screen saver it's just a full screen terminal window that's running c matrix i just think it looks really cool in the background again it has no practical use whatsoever it's not going to enhance your productivity but it's cool now as an aside what i often do is press f11 to make the terminal full screen and that makes it look even cooler but f 11 is not actually specific to c matrix that's a trick in and of itself so i'll press f11 again and that undoes the full screen and then to get out of c matrix i hold ctrl and press c and now i've broken out a c matrix so again i'm going to press f11 and notice that the terminal is now full screen so f11 is a terminal shortcut and it's recognized by quite a few desktop terminal emulators there's some that don't understand f11 but most do and that makes it full screen and i mean full screen to the point that you won't see any window controls at all you won't see your panel it's literally taking up your entire screen so i'll often do this if i want my terminal to basically cover up everything else if i don't want any distractions and i just want to work on something without seeing anything in my desktop environment i don't want to see email alerts i don't want to see anything but what i'm working on i'll just press f11 and make the terminal the only thing that's visible and when you press f11 again it returns it back to normal in addition most desktop terminal emulators will allow you to increase and decrease the font and this is something that i often do in my videos so what i'm going to do is hold ctrl and shift at the same time and while i'm holding those down i'll press plus notice that every time i press plus the font will get larger if i hold control not control and shift but just control and press minus as you can see the font is shrinking now often when i do this it kind of plays around with the sizing of the terminal so i'll often press reset to make sure that it's going back to the proper size otherwise i might have some blank space up here but anyway that's how i actually adjust the size of the font in my videos i often crank up the font size to make sure that you guys are able to see what i'm doing and sometimes i have to shrink the font size if something i'm doing is taking up too much space i've had some people in my comments ask me how i do that and if that's you well now you know now what i'm going to do is just press the up arrow a few times i'm going to just bring up a previous command i think this one right here is good enough and what i'm going to do is hold ctrl and press u and it just deletes everything on that line literally everything and depending on your computer it could take some time to actually backspace everything out i mean that didn't take too long but if you had a complete wall of text ctrl u will simply just delete everything that's pretty cool now similar to that i'll bring up another command here maybe something a little bit longer i guess that's fine since we're on the subject of control u to delete everything on the prompt there's a few other shortcuts that also involve holding down control so what i'll do is hold ctrl and press a and that puts the cursor all the way to the front of the line and then ctrl e takes you to the end of the line now suppose that i want to run this command right here but before i press enter i realize that i probably should have included sudo at the beginning now i could hold down the left arrow go all the way to the beginning which might take more time if the command is even longer and then i could type sudo and i'm fine but what i think is more practical is that you can hold ctrl and press a and that immediately brings you to the front of the line and then control e takes you to the end of the line so that way i'm able to jump to the beginning add sudo jump back to the end that'll save some time maybe not the most useful thing in the world but you never know maybe this is something you'll find helpful at one point or another in the future so i'll clear the screen this time i'll just hold ctrl and press u to delete that command and now i'm back to an empty prompt now what if i wanted to run two commands one after another maybe i want to run sudo apt update and then sudo apt dist upgrade the first command will update my package repository index whereas the second command that i mentioned will make sure that all available updates are installed what i could do is run sudo apt update and then once it's done i can run sudo apt disk upgrade to run the second command but that's not what i'm going to do instead i'm going to set up the command just like this and watch what happens it immediately changed the command into the dist upgrade command so if i was to press enter it's going to install all of the updates now of course apt is actually specific to debian and ubuntu but the command itself doesn't really matter i'll just say no for now because i don't need to update anything i'm just using a temporary installation on my studio laptop i don't really care to update it right now anyway so what i'll do right now is just chain two random commands together so ls-l and then i'll run echo hello world so the first command produced this output right here because i ran ls dash l and the second command echoed hello world to the screen which we see right here so now what i'm going to do is show you another variation of this same idea so i'll recall this command and i'm going to take away the semicolon here and instead i'll type to ampersands what do you think is going to happen let's find out the same thing but wait a minute if it does the same thing if this method changed two commands together as well then why would i want to use the semicolon versus the two ampersands to demonstrate the difference i'm going to bring back this command right here but what i'm going to do is make the first command completely invalid so i'm going to list the storage of a directory that doesn't exist this will produce an error and then i'm going to chain the command into echo hello world just like before but the command stopped me though it's telling me that this directory does not exist which i knew but it didn't run the second command that's interesting let's bring that back take away the ampersands and i'll replace them with a semicolon so we get the same message as before it's telling me that the directory does not exist but it still ran the second command so now you can see the difference if you chain commands together with semicolons in between it will actually run the next command if the previous command failed but on the other hand if you chain commands together with ampersands instead of the semicolon if it encounters an error it will not run subsequent commands and that's the difference depending on what you want maybe you want the second command to go through if the first one fails or maybe you don't and that'll determine which one of these you actually use now another command that i want to show you guys is tail dash f and i feel like this is a command that a lot of people know already but for whatever reason i didn't find this out until much later in my career so i want to make sure that i let you guys know about this as early as i can so if you didn't already know about this well you're about to know about it so what we're going to do is just tail a log file i just chose the var log syslog file randomly you could give it any file that actually exists now when i press enter it's going to show me the contents of var log syslog or at least the last portion of it and you can't really see it now but if anything was to happen on this system then this is going to update in real time so i can literally watch the log as content is being added to it so that way if i'm troubleshooting something i could keep my eye on what's happening on the server maybe the other person that's telling me about a problem can try to reproduce the problem and while they're doing that i'll just watch the log and as they're trying i'll see new entries being added to the log file it's a very cool thing to do and to break out of this you hold ctrl and press c and now you're back on the command line now for the next trick if i list the storage of my current working directory i actually have this file right here called hello.txt that i actually created off-camera if i check the contents of that file you can see that all it contains is hello world now we're going to pretend that this is a very long file maybe it's a log file for example and we want to empty it out now most of the time you can actually delete a log file and then the service will recreate it but that's not always the case it's actually easier to truncate a file especially if it's a log file it's a safer thing to do than outright deleting a file maybe there's still an open file handle to that file and if you go ahead and delete a log file maybe a new log file will not be created and that wouldn't be good so what you could do is run truncate we could set the size that's what the dash s stands for we'll set the size to zero be very careful with this by the way and then we'll give it a file name and that's going to make the file size of that file exactly zero thereby deleting everything it contains and that's why i mentioned you should be careful with it i kept the contents of hello.txt one more time it's an empty file that makes sense we set the size to exactly zero it's now an empty file so now i'm going to show you guys one more command that i like and that's the column command but it's actually more useful when you chain commands into it for example if i run mount it's going to show me a list of all the file systems that i have mounted on my computer or server or whatever happens to be but it's all kind of jumbled together and yes part of that is because of my font size but even if i shrink the font size and then run it again it's not all that much better so let's try this a different way i'm going to run mount and then i'm going to pipe that into column dash t and i might need to make the font size really small so you can see the difference it's going to make sure that all the output is shown in columns which makes it a lot easier to read and it's not specific to the mount command any command that produces output that's a little jumbled might actually benefit from being piped into the column command like i'm doing here if nothing else piping mount into column t is useful oven by itself so if that's all you take away from this then i guess that's fine too so hopefully you've learned something new with today's video go ahead and let me know in the comments down below what your favorite command line trick or time saving shortcut happens to be i look forward to reading what you guys have to say and maybe i'll learn something new myself anyway as always thank you so much for watching and i'll see you again very soon [Music] you
Info
Channel: Akamai Developer
Views: 1,264,705
Rating: undefined out of 5
Keywords: cloud computing, linode cli, linode server setup, cloud hosting, linode tutorial, open source, cloud computing tutorial for beginners, linode, linux, alternative cloud, vps automation, access linode through command line, linux server CLI, server cli, command line shortcuts, trucate files linux, chain commands linux, adjust font size terminal, repeat commands terminal, navigate directories linux, minimize app linux, restore app terminal, review command history linux, chmatrix
Id: AVXYq8aL47Q
Channel Id: undefined
Length: 29min 50sec (1790 seconds)
Published: Wed Mar 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.