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