Linux Command Line Full course: Beginners to Experts. Bash Command Line Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's take a look at command-line basics displaying a calendar of the current month at the command line there is a simple command that you can use to display a calendar of the current month Cal let's take a look at command-line basics displaying the current date and time at the command line there is a simple command that you can use to display the current date and time the date command let's take a look at command-line basics PWD PWD is one of the simple commands that you need to learn PWD stands for print working directory this command gives us the location where we're currently located in the directory structure let's take a look at the manual page PWD prints the name of the current working directory there are a couple of options which you usually won't need to use when a run PWD currently we see that I'm in my home directory you can explore other places in the file system and use PWD to identify where we are let's use the CD command to go somewhere else let's try a few more you'll see that wherever I go I can use PWD to find out the location where I am let's take a look at coming online basics using exit to end a terminal session when you are done at the command line it is good practice to explicitly terminate your bash terminal session this is done using the command exit after you execute this command your bash session ends if you were connected to a remote server via SSH as I was here executing the exit command also terminates your SSH session let's take a look at coming online basics using up and down arrow keys to navigate the command history let's begin by running a few simple commands we'll start by running a command to see the current date and time next we'll print a simple message and next let's display a calendar of the current month when working at the command line it is likely that you may want to run a command again Bosch provides a simple medicham mechanism for navigating the commands that you've used recently the up arrow and the down arrow pressing the up arrow will instruct Bosch to recall the previous command from the list of recently used commands let's try pressing the up arrow as you can see the most recently used command now appears let's try pressing the up arrow again the next most recently used command which in this case was echo now appears let's try pressing the up arrow again the next most recently used command which in this case was date now appears just as we can use the up arrow to navigate backwards in time through the command history we can also use the down arrow to navigate forwards in time through the command history let's try pressing the down arrow doing so takes us forward one command to the echo command let's try pressing the down arrow again doing so again takes us forward one command this time to the Cal command and finally let's press the down arrow one more time doing so takes us back to an empty command line this corresponds with where we were before we first press the up arrow let's take a look at command-line basics using left and right arrow keys to navigate within the current command let's begin with a command that displays a simple message after entering this command what can I do if I want to change what I just typed one option would be to delete what I typed using the backspace key and then type something else however there is a more efficient option by using the left and right arrow keys I can navigate backwards and forwards within the command as I type let's take a look at command-line basics make Durr make Durr is used to create directories after using make Durr to create a directory we can use the LS command verify that that directory was successfully created let's try again this time molesta ATS let's ask make Durr to be verbose by calling make dirt with the - to feed flag make two prints a message for each created directory we can of course still use LS to verify that the directory was created make Durr can also be used to create multiple directories at one time if you attempt to create a directory in a directory with that name already exists maker will produce an error message in the previous examples we created directories directly within the current working directory it is also possible to use make dirt to create directories elsewhere this command creates a subdirectory called edu inside the SRC directory this behaviour can be extremely useful however be advised that if the parent directory does not exist make Durr will produce an error message and will fail to create the subdirectory this command failed because the examples directory doesn't exist there's a solution let's take a look at the manual for make Durr to find it the - PFLAG instructs maker to make parent directories as needed the - PFLAG can also be used so that maker doesn't complain if the directory already exists recall that we already created a directory called bin as we saw earlier if we try to create a directory that already exists Viktor will normally fail but by calling maker with the - PFLAG Viktor will silently succeed in all of the examples so far we have asked nature to create directories within the current working directory we can alternatively call make dirt using absolute paths maker is an essential Linux command that you will use frequently you should practice using it to create directories in your current working directory and elsewhere while you may never use the dash Z or dash M flags you should learn to use the dash PFLAG let's take a look at can we outline basics using LS to list the contents of the current working directory LS is one of the most commonly used commands let's take a look if I run LS with no arguments it will display the contents of the current working directory we can now see that this directory contains five other directories Baz foo homework kennel m and Zork let's navigate to another directory and use LS to view its contents we can now see that the current working directory Ken LM contains a number of files and directories let's use CD to change to a different directory now if I run LS with no parameters it's going to print the contents of the current working directory which is slash user in conclusion invoking LS with no parameters will display the contents of the current working directory let's take a look at command-line basics using less to view text files when working at the command-line it is likely that you will commonly want to view the contents of various files you were working with there are a number of programs you can use to accomplish this goal one of the most widely used programs for viewing text files is less like many programs you will encounter the name les is a play on words les was developed as a more fully featured replacement for an earlier text viewing application whose name was more less can be used to view the contents of text files notice that unlike on Windows on Linux file extensions such as MD txt zip and dot PI are not meaningful even though readme MD has an extension it is still a plain text file and can be opened by less let's can be navigated page up and page down using various key shortcuts including space to page forward and B to move backwards to exit less press q les has lots of options one helpful option is the display of line numbers this option can be enabled with the dash and flag notice that if necessary les will rap lines if the line is too wide to be viewed on the current display line 10 in this file begins here but continues through here this line is marked as line 10 throughout let's take a look at some other files here is George Washington's inaugural address and here is Johnson's and here is Obama's the text files don't have to be in English les is capable of displaying text in other languages for example here's a Swadesh list for Russian les can also open more than one file at a time when using less to view multiple files the command : n is used to navigate to the next file and the command : P is used to navigate to the previous file is one of the most commonly used programs you will encounter when using the command line it is well worth your while to learn how to use it and to learn some of its more commonly used shortcuts let's take a look at command line basics using LS to list the contents of an on current directory using a relative path let's take a look if I run LS with no arguments it will display the contents of the current working directory LS also accepts an argument when I provide an argument to LS it will list the contents of that directory rather than the current directory so here we see that there are a number of files and directories within the can LM directory if we want to see the contents of one of these subdirectories one approach would be to use CD to navigate to that subdirectory and then use LS to view its contents however there is a faster way let's go back to the home directory and try the faster way rather than calling CD and then calling LS we can directly call LS providing the directory as an argument in the above invocation of LS we provided a relative path Ken LM / include as an argument to LS after doing so LS listed the contents of the specified directory we can also call LS using other relative paths including those that make use of dot and dot dot we call that dot dot refers to the parent of the current working directory in this case the parent of the current working directory is Ken L M let's try another in this case dot dot refers to the parent of the current working directory Ken LM and dot dot slash util refers to the utill directory that is within the Ken LM directory let's take a look at command-line basics using LS to list the contents of a non current directory using an absolute path let's take a look if I run LS with no arguments it will display the contents of the current working directory LS also accepts an argument when I provide an argument - LS it will list the contents of that directory rather than the current directory so here we see that there are a number of files and directories within the kennel m directory if we want to see the contents of one of these subdirectories one approach would be to see use CD to navigate to that subdirectory and then use LS to view its contents however there is a faster way let's go back to the home directory and try the faster way rather than calling CD and then calling LS we can call LS directly providing the directory as an argument in the above invocation of LS we provided an absolute path slash home slash Lina's / kennel m / include as an argument to LS let's take a look at coming online basics identifying and understanding symbolic links let's take a look so far everything looks normal user bin Python is a file more specifically we can use this file to invoke the Python interpreter let's look more closely at this file in the above invocation of LS we used the - L flag to direct LS to use the long listing format notice that in this long listing there is an arrow the arrow means that user bin Python is not a regular file instead it is a symbolic link that points to another file in this case user bin Python points to the file Python 2.7 we can confirm that user bin Python is a symbolic link by invoking the program file file can be used to determine the type of a file this confirms that user bin Python is a symbolic link let's take a look at user in Python 2.7 we see now that user bin Python 2.7 is an actual executable file so when we call Python the system redirects our call so that Python 2.7 is called instead now let's look at a slightly more complex example we see that user bin Emacs is a symbolic link to Etsy alternatives Emacs okay that's a little strange normally executable files are located in user bin not in Etsy let's take a look at Etsy alternatives Emacs we see now that we have another symbolic link that's the alternatives Emacs is actually a symbolic link to user bin Emacs 24x so when we run user bin Emacs the system redirects our call twice first to Etsy alternatives Emacs and then finally to user bin Emacs 24 X in conclusion symbolic links like shortcuts in Windows and aliases in the Mac OS provide a mechanism for referring to another file symbolic links can be easily identified by using LS with the dash L flag and also by using the file command let's take a look at command-line basics using LS to view hidden files if I run LS with no arguments it will display the contents of the current working directory we can see now that this directory contains five other directories as foo homework can LM and Zork however this directory actually contains a number of other hidden files by default LS ignores any file whose name begins with dot we can force LS to display these otherwise hidden files by using the - a flag as you can see when we invoke LS with the - a flag in the home directory a number of hidden files can be seen take a look at command-line basics using LS with additional options if I run LS with no arguments it will display the contents of the current working directory we can now see that this directory contains five other directories as foo homework Ken LM and Zork there are a number of additional options we can use to configure LS one commonly used option is the - el flag the - el flag causes LS to use a long listing format using the long listing format shows us additional information for each file this information includes the file type file permissions the owner and group associated with the file the file size the modification date and the file name file sizes reported by LS dash L aren't particularly readable by using the - H flag LS will report file sizes in a more human readable format notice that when we specify multiple flags we can either list each flag separately or list the flags together the above two commands are equivalent there are a number of other useful flags in LS which can be found by reading through the LS man page let's take a look at command-line basics using RM to delete files and directories let's take a look the RM command can be used to delete files let's use RM to delete the file license in the current directory after using RM the specified file has been deleted note that by default RM does not prompt you for confirmation it simply deletes the file if you use the - I flag RM will prompt you for confirmation before it begins deleting files let's use RM to delete a file in the food directory because we specified the - I flag RM prompted us for confirmation before it deleted the file let's see what happens if I say no RM did not delete the file let's try again and this time we'll say yes to the confirmation we can also use the - V flag to force RM to explain what is being done let's try removing another file this time using the dash of the flag by invoking RM with the dash V flag RM printed a message confirming that the file was deleted we can also invoke RM with an absolute path now let's try to use RM to delete the food directory what just happened we called RM providing Foo as the argument for what to delete and RM failed to delete the directory let's look at the man page for RM to see if we can figure out what just happened by default RM does not remove directories to force RM to remove a directory and any contents it might have we can invoke RM with the - our flag in many cases especially when you are new to the command line it is advisable to use the - I or - capital I flag when invoking RM recursively here we'll use the - capital I flag also use the - V flag so that RM will confirm what files it deletes let's take a look at command-line basics using CD to navigate to a directory using a relative path let's take a look at where we are the current working directory now let's use LS to view the contents of the current working directory we can use the bash built in command CD to change directories to use CD we provide the name of the directory into which we wish to navigate let's use PWD to verify that we have changed directories notice that in the above indication of CD we only specified the name bass we did not type slash home slash Lane ass slash bass this style of referring to a directory by only part of its name shows the use of CD using a relative path the absolute path in this case would be slash home slash Lane ass slash bass the relative path that we used was simply bass because we were located in the directory slash home slash Lane ass at the time we invoked CD the part of the path corresponding to the current directory can be omitted through the use of a relative path there are two special relative path components dot and dot dot dot refers to the current working directory dot dot refers to the parent of the current working directory let's recall where we are the current working directory Baz is located within the directory lain s we could refer to the parent by its absolute path slash home slash Lane s or we could refer to it as dot dot let's use CD and dot dot to navigate to the parent of the current working directory let's practice some more what just happened we told CD to navigate to dot dot is another name for the current working directory consequently we stayed in the same current working directory in other words we didn't go anywhere what just happened we were in slash home slash Lane ass / - we used dot dot twice from /home slash Lane ass / Fuu dot dot would refer to the parent directory slash home slash Lane ass but we used the relative path dot dot slash dot dot if dot dot is the parent then dot dot slash dot dot is the parent of the parent in this case the parent is slash home slash Lane ass and its parent is slash home so invoking CD dot dot slash dot dot from home - foo takes us to home notice what we just did we used a relative path lane s slash Ken LM to navigate from /home to slash home slash Lane ass / Ken om we could have called CD twice but this way was faster let's go back and see the slower way we used Siddhi here to navigate one directory at a time just as we can use LS to look into sub directories we can also use CD to navigate directly into sub directories let's take a look at command line concepts understanding complex relative paths using dot and dot dot now let's take a look at one example of a relative path that uses both dot and dot dot multiple times now let's go back and take a closer look at this complex relative path will begin by visualizing the directory tree in which we're operating the directory tree looks like this for convenience in keeping track of sub directories I have color-coded the sub directories will be discussing the current working directory is slash home slash lanús slash Ken LM slash util slash stream notice that I've color-coded the names in this path to match the corresponding sub directories in the directory tree now let's look at our complex relative path again notice that I've color-coded the names in this relative path to match the corresponding sub directories in the directory tree dot dot which you see here in red refers to the parent of the current working directory our case the parent is the util directory which is also drawn in red here in the tree double conversion is a subdirectory of util thus given that stream is the current working directory dot dot slash double conversion refers to the subdirectory colored in gold in the tree slash home slash lanús slash ken om / Yuto / double conversion dot is another name for the current directory this means that dot dot slash double conversion slash dot means exactly the same thing as dot dot slash double conversion the parent of double conversion is you tell thus dot dot slash double conversion slash dot slash dot dot refers to the red node in the tree whose absolute path his slash home slash Lane LS Lane ass / can om / you tell parent of Yuto is can-am so dot dot slash double conversion slash dot slash dot dot slash dot dot refers to slash home slash lame-ass slash klm which we see colored orange in the tree LM which you see in blue is a subdirectory of Ken LM so dot dot slash double conversion slash dot slash dot dot slash dot dot slash LM refers to slash home slash lanús / kennel m / LM and because dot which we see here in blue refers to the current directory dot dot slash double conversion slash dot slash dot dot slash dot dot slash LM slash dot refers also to LM rappers is a sub-directory of LM so home dot dot slash double conversion slash dot slash dot dot slash dot dot slash LM slash dot slash rappers refers to slash home slash Lena slash Ken LM / LM / rappers dot dot which we see here in blue refers to the parent of the current working directory so this path refers to LM and finally builder is a sub-directory of LM and so dot dot slash double conversion slash dot slash dot dot slash dot dot slash LM slash dot slash rappers slash dot dot slash builder refers to home lane as Ken om LM Builder let's take a look at command line concepts understanding complex relative paths using dot and dot dot now let's take a look at one example of a relative path that uses both dot and dot dot multiple times note that the following example is intentionally complex deliberately made convoluted to illustrate how dot and dot dot can be used within other paths it may be a while before you encounter an example that's this complex in real life but it is good to understand what's going on now let's go back and unpack that messy path you dot dot refers to the parent of the current working directory in our case the parent is the you tell directory conversion is a sub-directory of you tell it's absolute path is slash home slash Lane us / can L m / Yuto / double conversion dot is another name for the current directory that means dot dot slash double conversion slash dot means exactly the same thing as dot dot slash double conversion the parent of double conversion is you tell the parent of you tell is can LM so dot dot slash double conversion slash dot slash dot dot slash dot dot refers to home lane as Ken LM is the subdirectory of kennel n so dot dot slash double conversion slash dot slash dot dot slash dot dot slash a LAN refers to home Lane ass ken LM LM and because dot refers to the current directory dot dot slash double conversion slash dot slash dot dot slash dot dot slash LM slash dot he's the same as dot dot slash double conversion slash dot slash dot dot slash dot dot slash LM also referring to home Lane ass Ken om LM rappers is a sub-directory of LM so dot dot slash double conversion slash dot slash dot dot slash dot dot slash LM slash dot slash rappers refers to home lanús ken LM LM rappers dot refers to the parent of the current working directory so dot dot slash double conversion slash dot slash dot dot slash dot dot slash LM slash dot slash rappers slash dot dot refers to home lane as Ken LM LM and finally builder is a sub-directory of LM so dot dot slash double conversion slash dot slash dot dot slash dot dot slash LM slash dot slash rappers slash dot dot slash builder refers to home lane ass Ken LM LM Builder let's take a look at command-line basics using CD to navigate to a directory using an absolute path let's take a look at where we are the current working directory now let's use LS to view the contents of the current working directory we can use the bash built-in command CD to change directories to use CD we provide the name of the directory into which we wish to navigate let's use PWD to verify that we have changed directories notice that in the above invocation of CD we specified the full path slash home slash Lane ass slash bass this style of referring to a directory by its full name shows the use of CD using an absolute path let's navigate somewhere else in the above command the absolute path that we used was slash user slash local notice that an absolute path always begins with a slash the layout of the Linux directory structure is a tree there is a single unique root of the file system tree the notation slash represents the root of the file system all directories have slash as their ultimate ancestor every absolute path begins with slash every absolute path specifies a sequence of contains relations beginning with the current file or directory and ending at the root of the file system let's look at another absolute path we used Siddhi to navigate to / user / share / man / user / share / man is an absolute path let's decompose this absolute path to understand it better man is a directory we can use LS to view its contents the parent directory of man is share in other words the share directory contains the man directory let's verify this by using LS to view the share directory the share directory contains the man directory the parent directory of share is user in other words the user directory contains the shared directory let's verify this by using LS to view the user directory the user directory contains the shared directory and finally the parent of user is root in other words the root directory contains the user directory let's verify this by using LS to view the contents of the root directory the root directory contains the user directory let's take a look at command-line basics using CD without options to navigate to the home directory let's took a take a look at where we are the current working directory we are currently located in the home directory each user has a unique home directory the path of the home directory can also be observed by printing the value stored in the home environment variable now let's navigate to a different directory let's use PWD to verify that we have changed directories now let's call CD again this time we're going to call CD without providing a directory path what just happened normally when we call CD we provide a relative or absolute path to a directory and CD then takes us to that specified directory in this case we didn't provide a directory so where did we go let's use PWD and the documentation for CD to figure out what just happened by running PWD we can see that CD took us to the home directory each user's home directory is different my home directory is slash home slash lain s we invoked CD without any options doing so took us to the home directory let's took take a look at the documentation for CD to find out why recall that CD is a bash built in command documentation for bash built-in commands can be accessed using the help command the documentation states that CD is used to change the shell working directory specifically CD is used to change the current directory Tudor normally dur is provided by the user however the square brackets in the usage means that providing this value is optional in other words users are allowed to call CD without providing a directory when this happens CD falls back to a default value for dur the default dur is the value of the home shell variable as we saw earlier the home shell variable stores the value of the user's home directory thus calling CD without any options takes us to the home directory let's take a look at command-line basics using MV to move files the MV command can be used to move files MV takes two mandatory arguments when moving a single file the first argument is the path to the file you want to move the second argument is the path where you want the file move to in the above invocation of evan in the above invocation of MV i moved the license file from the can-am directory the result is a file in my current working directory called ABC another way to invoke MV is by giving an v a destination directory for example let's say we wanted to move the file ABC from the current directory into the directory as to do so we call em V using ABC as the source file and bass as the destination directory as you can see after the above invocation of the MB the bass directory now contains ABC usually it is not necessary to supply any options to mV however if you want confirmation of what MV is doing invoking MV with the - the flag will explain what is being done by calling MV with the - V flag and the confirms that it moved the file bath / ABC - kennel m / licence MV can also be used to move multiple files to a common destination directory let's use touch to create several empty files now let's use MV to move all four of these files into the food directory the dot-dot-dot no notation here indicates that MV allows us to specify multiple source files as you can see all four files have been moved to the food directory let's take a look at command-line basics using CD to navigate using tilde let's take a look at where we are the current working directory we are currently located in the home directory each user has a unique home directory the path of the home directory can also be observed by printing the value stored in the home environment variable let's look and see what other users have home directories on this system navigating to or referring to a user's home directory is so common there is a special shorthand syntax for doing so tilde the notation tilde user name refers to the home directory for username for example let's try navigating to the home directory for the user price - we can use PWD to verify that we have successfully navigated to the home directory for the user price - let's try again with another user I can also use this notation to refer to my own home directory my username on this system is lanús so I can refer to my own home directory using tilde plainness it turns out that referring to your own home directory is even more common than referring to someone else's home directory consequently there's an even shorter shorthand notation for referring to your own home directory tilde when used as a directory a bear tilde without an accompanying user name refers to the users own home directory you can also use this notation to refer to files and directories within your home directory notice that in the above invocation of CD tilde slash kennel m refers to the kennel M directory that is in my home directory in inclusion the notation tilde username can be used to refer to the home directory associated with the specified username for example and the notation tilde can be used to refer to your own home directory let's take a look at command-line basics using Ln to create symbolic links the Ln command when used with the - s flag can be used to create symbolic links symbolic links like shortcuts and windows and aliases in the mac OS provide a mechanism for referring to another file in its first form Ellen takes two mandatory arguments the first argument is the path to a file that already exists the second argument is the path where you want the new symbolic link created in the above invocation of Ln I created a new symbolic link called ABC that points to the license file in the KLM directory ABC is not a copy of license it is a symbolic link to license we can verify this by using the file command to ascertain the type of ABC we can also verify this by calling LS with the - L flag the long file format shows that ABC is a link and that it points to kennel m / license now let's take a look at the second form of invoking Ln in the second form we only provide the path to the file that already exists when we invoke Ln in this manner providing only a single file as an argument ellen creates a symbolic link to that file with the same name in the current working directory the third form of invoking Ln is more advanced and can involve some additional subtleties so we'll cover that in a separate video let's take a look at command-line concepts using Ln to create symbolic links in another directory the Elland command when used with the - s flag can be used to create symbolic links symbolic links like shortcuts in windows and aliases in the Mac OS provide a mechanism for referring to another file we're going to look at the third form of invoking Ln the first argument is the path to a file that already exists the second argument is a directory in which you want the new symbolic link created mmm something's not right here the symbolic link is broken let's remove the broken link and try again in the above invocation of elan we attempted to create a symbolic link in another directory when we provided the first argument it was a relative link this use of a relative link in the third form of Ln is the cause of our broken link there are a couple of possible solutions the first solution is simply to use an absolute path instead of a relative path now let's remove the symbolic link and figure out how to make a symbolic link in another directory using a relative path let's try to figure out why this link is broken within the baz directory license is a symbolic link pointing to kennel am slash licensed kennel m / license is a relative path within the baz directory what does the relative path kennel m / licence mean well it would mean that there should be a directory a subdirectory called kennel am within the current directory paths and within that subdirectory there should be a file called license there's the problem is there a kennel m directory inside Baz no there's not we just ran LS from within baths and there is no kennel m subdirectory the broken link is looking for something that doesn't exist that's why it's broken we can confirm this by using read link to determine the absolute path that the symbolic link is attempting to point to this confirms that the symbolic link is looking for a file that doesn't exist slash home slash Lina's slash Baz slash kennel am slash license so what would happen if the missing file were to be created let's find out to do so let's create a directory called can om within the Baz directory now let's use the touch command to create an empty file called license within our newly created can-am directory finally let's see if our symbolic link is still broken the symbolic link license in the Bowser directory is no longer broken why because the file it was looking for now exists so our link is no longer broken but it's pointing to the wrong file specifically it's pointing to the empty file we just created that's not what we want at all let's remove these dummy files and try again in order to use the third form of ln2 correctly create a link it's helpful to realize two things first Ln interprets this first argument as a string and second if that string represents a relative path the file system will interpret that relative path relative to the directory in which the symbolic link is to be created recall from above that are broken symbolic link with looking for a KLM directory within the baz directory so let's think about things relative to the baz directory from within the bass directory what relative path can we use to refer to slash home slash lame-ass /k om / license well Ken LM and bass are both in the home directory in other words they have a common parent we can refer to the parent of the current working directory as dot dot so from the bar direct from the Baz directory dot dot slash Ken L M refers to slash home slash lanús slash Ken L m and dot dot cannot / kennel am / license refers to the file that we want armed with that information let's try making that symbolic link again it worked we can use read link to confirm that the symbolic link is now pointing to the correct file we have succeeded in creating a symbolic link in another directory using a relative path to do so we had to think about the relative path from the perspective of the directory in which the link is being created it turns out that we can ask Ln to do this change of perspective for us when invoked with the dash our flag Ln will create the symbolic link relative to the link location let's remove the existing link and give this a try by running LS dash L we can observe that when the dash our flag was passed to Ln Ln correctly translated the relative path we provided kennel m / license into a relative path that is correct in the context of the Baths directory dot dot slash Ken LM slash license calling read link confirms that the symbolic link in the bass directory is indeed pointing to the correct file slash home slash Lane ass / Ken om / license let's take a look at command-line concepts using file to determine a files type let's take a look the file command accepts a file name as an argument the command attempts to determine the type of the specified file using file we observe that user local is a directory great that's exactly what we would expect let's look at some more files and use the file command to determine their respective types the above invocation of file tells us that mail queue 1gz is comprised of data that was compressed using gzip here we see that license is a plain text file encoded using ASCII let's take a look at command-line basics using CP to copy a single file the CP command can be used to copy files CP takes two mandatory arguments when copying a single file the first argument is the path to the file you want to copy the second argument is the path where you want the file copy to in the above invocation of CP I copied the license file from the Ken LM directory the resulting copy is a file in my current working directory called ABC ABC is a copy of license we can use the diff command to verify that the two files are identical another way to invoke CP is by giving CP a destination directory for example let's say we wanted to copy the file ABC into the directory path to do so we can call CP using ABC as the source file and Baz as the destination directory as you can see after the above invocation of CP the bass directory now contains a copy of ABC let me add lying basics using CP to copy a single directory the CP command can be used to copy files and directories let's say we want to make a copy of the can-am directory the first thing we might try is a simple invocation of CP what just happened we tried making a copy of the can-am directory called WXYZ if we run LS we see that our attempts to copy the can-am directory failed by default CP will not copy directories to get CP to copy a directory we must add an additional option when invoking CP if CP is invoked with the - our flag it will copy directories including their contents as you can see after the above invocation of CP with - are the WXYZ directory now exists and is a copy of the KLM directory let's take a look at Kamiya blind basics using echo to print simple strings echo is typically used to display a single line of text here the message was hello world echo prints this message to standard output notice that the message was not printed until after the Enter key was pressed while it may not always be necessary to enclose the message within quotation marks doing so is good practice let's use echo to display another simple message as you can see the message we gave to echo is printed to standard output it is possible to redirect the standard output stream to a file or use a pipe to send it as input to another process in this case we aren't doing either of those things as a result we see that the default behavior where messages sent to standard output are displayed on the terminal let's use echo to display some more simple messages let's take a look at coming online basics using echo to display multi-line messages echo is typically used to display a single line of text however echo can be used to print messages that span multiple lines of text let's start by using echo to print a simple message consisting of a single line of text now let's try again but this time we're going to make a mistake notice what happened we typed the opening quotation mark then we type the message then hit the enter key instead of printing our message we see something that looks like a greater than sign notice that we forgot to type the closing quotation mark let's add the closing quotation mark then hit enter again notice what just happened our message was printed but in addition an extra blank line was printed do you see the extra blank line it's there let's try another example in our next example the second line will contain more of the message here it should be clear that echo printed a message that consisted of two lines of text when entering a multi-line message in this manner were not limited to just two lines the contents of our message we'll continue until we enter a closing quotation mark after we enter the closing quotation mark and then press the Enter key the entire multi-line message is printed to standard output let's take a look at command-line basics using echo to display messages with escape sequences echo is typically used to display plaintext messages however echo can be used to print messages that include special characters let's start by using echo to print a message consisting of a single line of text now let's try again but this time we're going to include something new and to do so we're going to take a look at the documentation for echo the documentation for most commands is found by accessing the commands manual pages using the command man but echo is special echo is a bash built-in command the documentation for bash built-in commands is obtained using the command help by reading the Bosch help page we can observe that echo is capable of reading a number of backslash escaped characters a backslash escaped character is just a character that is immediately preceded by a backslash character of the backslash escapes sequences you see here it's likely that you'll only ever need to use one or two of them the two that we'll examine here are the escape sequences for newline backslash N and for horizontal tab backslash T now let's try printing our message again however this time instead of placing a space between the two sentences we're going to try to place a tab character between the two sentences recall that the escape sequence for a horizontal tab character is backslash T what just happened the escape sequence backslash T represents a horizontal tab character we wanted echo to replace backslash T with an actual tab character let's take another look at the help page for echo in the above invocation of echo we did not use any options notice that echo has an option - e that does exactly what we want if echo is called with the - II option then echo will replace these escape sequence with its corresponding literal character let's try again this time using the - 'flag it looks like there's a space character between the sentences but it's actually a tab to see the difference more clearly let's try again but this time let's add another tab now we can see a difference let's try again with three tabs compare that to the same thing but with three spaces between the sentences now let's try again using the escape sequence for a newline backslash n whoops I did backslash T again let's try again with backslash n now let's add another new line and now again with three newline characters by using escape sequences we can use echo to print a multi-line message even while we enter the command using a single line let's take a look at command-line basics grep the grep command finds text patterns in files it prints out all the lines containing a matching pattern grep can match very complex patterns with the use of regular expressions let's take a look at graphs man page grep searches the named input file or files or standard input if no file is named let's use grep to find all the files that have the word zip in the name in the directories bin and user bin to do this we first use LS to list the files in those directories then pipe the result to grab to search for the pattern zip a list of file names containing zip is displayed grep pattern matching is case sensitive by default let's try to list the file names in bin and user bin that contain the word zip in uppercase we get zero matches to allow grep to ignore case when matching the pattern we can use the - I flag with grep the - I flag specifies that grap should ignore case distinctions if instead of a list of lines that match the pattern we want a list of lines that do not match the pattern we can use the dash V flag with grep to invert the match we now get a list of file names in bin and user bin that do not contain the word zip to output the number of matches instead of the actual matching lines we can use the dash C option with grep here we observe that there were 19 items which matched the pattern zip you let's take a look at command-line basics head and tail head and tail are commands that print the first and last parts of a file respectively by default head prints the first 10 lines of a file and tail prints the last 10 lines let's take a look at their man pages by default head prints the first 10 lines of a file to standard output this can be changed using the dash and flag to specify the number of lines instead of assuming the default of 10 tail behaves very similarly let's create a text file and use head and tail to examine its first and last lines we observe that foo text now contains a sorted list of the files in user been we can use head to display the first 10 lines of the file we can use tail to display the last 10 lines of the file to view a different number of lines say the first or last 5 lines of a file we can use the dash n option with head and with tail these are the first 5 lines of food text and these are the last 5 lines of food text head and tail can also be used in a pipeline let's list the last 7 items of the contents of the directory user bin to do this we will use LS to list the contents sort to sort them and pipe the output to tail with the appropriate option listing the last 7 items in the directory user bin you let's take a look at command-line basics echo let's take a look at echos main page the command echo accepts 0 or more strings provided by the user and prints those strings to standard output if the standard output of echo has not been otherwise redirected then those strings will be displayed to the screen separated by spaces as we can see echo printed the string hello if we provide more than one string to echo it will print each string and we'll put a single space between each pair of strings here's the first string followed by a space followed by the next string if we provide more than one space between each string echo ignores the extra spaces and only a single space is printed if we want to print a string preserving all spacing and other formatting we can enclose this string in quotation marks when we do this echo will treat everything within the quotation marks as a single string let's take a look at command-line basics wildcard expansion let's try to use echo to display a single line of text see that the line we typed is displayed just as we typed it extra whitespace in the arguments of echo will be removed in the output here we typed extra spaces but the output in those locations only included a single space when there are special characters in the line of text we see something else to check this lets CD to a different directory and look at the contents of that directory using LS now let's try to use echo with a wild-card character as the argument the star or asterisk here is treated as a wild-card character notice echo did not display the character star itself instead it displayed a listing of the files in the current working directory notice that this output of echo is the same in terms of the files listed as that we saw with LS this is because before the echo command is executed the shell itself has expanded the wild-card character into a list of file names in the current working directory it is very important to understand how the shell carries out expansion for different kinds of characters echo is a good way to observe the effects of different special characters and their respective expansions before we use these special characters with other commands let's take a look at Kemah online basics path name expansion - is capable of performing certain types of expansions let's try to use LS to see how path name expansion works in this command the wildcard character has been expanded the result is a listing of all file names in the specified directory whose file names end in dot H let's try another example here the wild card expanded again resulting in a list of all files in the user bin directory whose file names begin with the characters py let's take a look at command-line basics tilde expansion tilde expansion is a special expansion involving the tilde character when the tilde character is used at the beginning of a user name it expands into the name of the home directory for that user let's try to use echo to see how tilde expansion works here we see an expansion resulting in the full path to this users home directory if no username is supplied after the tilde character the result expands into the home directory of the current user which in this case is Lane s observe here that the tilde character has been expanded into the home directory of the current user let's take a look at command-line basics arithmetic expansion arithmetic expansion allows the shell to perform calculations before passing the results to a command as arguments arithmetic expansion uses the dollar sign character and double parentheses around an arithmetic expression let's use echo to see how arithmetic expansion works see that this arithmetic expression seven plus three has been calculated with the result ten note that arithmetic expansion only supports integers for whole numbers the expansion would result in an error if decimals are present in the expression see that Bosch has displayed an error message for the decimal invalid arithmetic operator indicating that the decimal point caused the error arithmetic expansion supports addition subtraction multiplication integer division modulo or remainder and exponentiation arithmetic expansion may also be used alongside text let's try to use echo to see how the integer division and modulo operators work if the result of the division is not an integer integer division will cause the result to be truncated to the integer part and will not be rounded so using integer division 7 divided by 2 is 3 with a remainder of 1 the 1 was calculated using the modulo operator the 3 was calculated using integer division now let's use echo to observe how the exponentiation operator works this calculation has raised v to the exponent of 2 or 5 squared which is 25 inside the double parentheses of an arithmetic expression it is possible to nest another arithmetic expression here we have nested an arithmetic expression 1+1 within a larger expression 5 to the power of 1 plus 1 here the inner expression calculates 1+1 resulting in a value of to the outer expression performs the exponentiation 5 ^ 2 resulting in 25 we can also use single parentheses to group the order of operations without actually causing a second expansion of the arithmetic expression to occur this will calculate the same result of 25 using only one arithmetic expansion instead of two let's take a look at command-line basics brace expansion brace expansion creates multiple text strings from a pattern the pattern takes the form of an unchanging preamble followed by a variable expansion component followed by an unchanging PostScript the varying part of the pattern is enclosed by a pair of curly braces constant part before the opening brace is called a preamble and the constant part trailing after the closing brace is a postscript let's try to use echo to see how braze expansion works this pattern has expanded into three text strings each string has the same preamble at the beginning and the same PostScript at the end the middle part of each string varies and is present in the same order as specified in the pattern Brahms Beethoven Bach Brahms Beethoven bah if we had changed the order in the pattern the order would be correspondingly different in the created strings both the preamble and the PostScript are optional here we expand a pattern with a preamble but no PostScript here we expand a pattern with a postscript but no preamble and finally we expand a pattern with no preamble and with no postscript now let's try to use brace expansion with a preamble that includes white space here Bosch considers I love and Brahms Beethoven Bach to be separate tokens as a result the brace expansion is performed but I love is not considered a preamble to the brace expansion to get around this problem let's try and closing I love in quotes this is slightly better now Bosch sees two tokens instead of three the first token is the quoted string I love and the second token is Brahms Beethoven Bach but Bosch still in this case does not consider I love to be a preamble to the brace expansion we can fix this by placing the quoted string immediately adjacent to the Bray's expansion with no intervening spaces now in this case bosch sees only one token the entire string where I love is taken to be a preamble because it immediately precedes the opening brace with no intervening whitespace finally let's add an additional space but place it inside the quoted preamble and also add a postscript I'm adding a space but placing it inside the quoted preamble and leaving no intervening space between the preamble and the start of the pattern inside the braces finally I'm going to place a postscript a single period a brace expansion can contain a comma-separated list of strings as we have just seen it can also contain a range of integers or characters using the operator dot dot let's try to use echo to see how to specify a range in brace expansion notice that I am NOT placing a space between the preamble and the pattern to be expanded an underscore is fine but a space is not this is expanded to five strings number one number two number three number four number five we can also use a sequence of characters with the dot dot operator here the pattern has expanded to a sequence of lowercase letters integers within a brace expansion can be 0 padded here we have 0 padded numbers 1 through 9 and here we have 0 padded numbers 90 through 100 brace expansion's can also specify numbers in reverse order here we have something expanded to strings where the numerical part is in reverse order from 5 through 1 brace expansions can specify a range of characters as we have seen above and character ranges can also be reversed we observed that this character range has expanded to lowercase characters in reverse order brace expansions can also be nested let's work through this expansion part by part in the expansion the preamble lowercase a and the PostScript lowercase B are constant in the variable part of the expression the brace expression is a list of items separated by comma so Bosch starts with the first item a1 - this is a brace expansion as well this expands to two strings upper case a one and upper case a two so the first two strings produced for the entire expansion are a a one B and a a to be now the first nested brace expansion is complete bash now starts with the second item b3 for the next two strings produced are the expansions b3 which is then prepended with the preamble a and post pended with the suffix B followed by the expansion b4 ultimately resulting in a B for B brace expansions are particularly useful for making lists of files or directories where the names are of a fixed format for example imagine that we want to organize a sequence of photos by month and year we may wish then to create a series of directories in numeric format in chronological order instead of creating the directories one by one using the command make tur by hand we can use a command using brace expansion now using a single make dur command we can create a series of directories with this one command we have created directories for all of the months over a range of 10 years let's take a look at Kemah online basics path name expansion bash is capable of performing certain types of expansions let's try to use LS to see how path name expansion works in this command the wildcard character has been expanded the result is a listing of all file names in the specified directory whose file names end in dot H let's try another example here the wild card expanded again resulting in a list of all files in the user bin directory whose file names begin with the characters py let's take a look at command-line basics commands substitution command substitution allows us to use the output of a command as an argument of another command this is done by using the dollar sign character followed by a pair of single parentheses the command whose output we want to use goes inside the parentheses let's begin by using LS to list the contents of the XCX 11 directory now let's enclose this LS command using the command substitution syntax [Applause] here we have first executed the command LS to output a list of items in the XCX 11 directory command substitution then causes this output to be used as the arguments to echo echo finally displays a list of those items note the command substitution is very different from piping piping allows us to redirect the output of one command to the standard input of another command for example a pipeline could be used to pipe the output from LS to the input of sort if we were to reverse that pipeline in attempt to pipe the output of sort to the input of LS this wouldn't work and it would have no effect on LS because LS does not make use of standard input on the other hand command substitution allows us to use the output of one command as the argument or arguments of another command therefore if we wish to configure the behavior of a command such as LS with the output of another command we would use command substitution not piping let's try to use command substitution to provide arguments to LS [Applause] in the outer command we are attempting to use LS with the - L flag to create a long listing in this format we're using the command which with an argument of CP to determine the full directory the full absolute path to the CP command the result of which is then CP then CP is then substituted as the argument to LS dash L resulting in the output that we see here there is also an older syntax for command substitution that we do not recommend that you use however you may occasionally see it that older syntax uses backticks instead of the dollar sign parentheses syntax again we do not recommend using this back text syntax however it's important to be able to recognize it if you see it in someone else's scripts let's take a look at command-line basics escaping characters by default the dollar sign has special significance to the shell use of the dollar sign here tells bash that we want to refer to the variable called a user bash then looks for a variable called user gets the contents of that variable and replaces user with the contents of the user variable if we want to print a string containing an actual dollar sign we can precede the dollar sign with a backslash the character we wish to quote in this case a dollar sign is then proceeded with a backslash which prevents this particular dollar sign from being interpreted as a special character if that dollar sign is followed by text then the quoting of the dollar sign prevents expansion by treating the dollar sign as an actual literal dollar sign rather than a special character which in normal cases would indicate that expansion is to be done this type of quoting is used to selectively suppress expansions it is handy when we occasionally want a special characters such as the dollar sign to appear as itself let's see the effect of this type of character escaping in commands in this command there are two expansions that could take place here we have a variable that is expanded to the value of that variable here Lane s secondly escaping transforms backslash dollar sign into the literal dollar sign character and prevents the prevents dollar 5 from being treated as a variable expansion sign is a character with special significance to bash there are other characters that also have special significance to bash these include the dollar sign which we just discussed the exclamation point the ampersand the backslash itself and the space character which bash uses to delimit tokens any of these special characters can be escaped using a backslash since the backslash is itself a special character if we want a backslash character to appear as ordinary text we would also need to escape it with a preceding backslash character here the first back slash character escapes the second back slash character resulting in a literal back slash character because this second character was escaped it does not in turn escape the dollar sign and the dollar sign here normally acts as normal to cause variable expansion see that we get this back slash character in front of the username let's see an example using multiple other escaped characters here we have escaped a literal space & ampersand another literal space in an exclamation point the ampersand & exclamation point have special significance to bash and can be explored in other situations needless to say without this escaping we would not get this exact output another way that we can confirm that special treatment is occurring here is by using brace expansion recall from our discussion of brace expansion that normally brace expansion with a preamble needs the preamble to not be separated by a space the reason is that a preamble is only considered if it is part of the same token as the token containing the braces here we're going to see that because we are using the backslash to escape the spaces this entire string is taken to be a single token by bash meaning that we will get greetings and salutations taken as the preamble to this brace expansion we confirm this here we get greetings and salutations Jo greetings and salutations Jane let's take at command line basics double-quoted strings many times when passing arguments to a command it is desirable for a string containing spaces to be treated by bash as a single token as we previously discussed one mechanism for obtaining this behavior is to escape the relevant space characters using the backslash character here we have created a file with literal spaces in the file name alternative method to using escaped spaces is to use double quoted strings to enclose the entire file name by first quoting the string my first file dot txt - treats this entire string as a single token within double quotes some characters which normally have special significance to bash are treated as normal characters without special meaning normally the ampersand character has special meaning to bash inside the quotes that special meeting is lost while called wildcard characters including the star the tilde character curly braces will all be treated as ordinary text and not special characters within double quotes therefore the following operations are suppressed word splitting path name expansion tilde expansion and brace expansion let's observe the effects of double quotes in some commands in this command the argument to echo includes extra spaces between two of the words the output of echo does not have these extra spaces between the word a and the word test this is because without double quotes space is interpreted as a special character for the purposes of performing word splitting and the extra spaces are simply ignored if we put this argument in double quotes the spaces within the string are no longer interpreted as special characters rather the entire quoted string is treated as a single token for the purposes of word splitting all of these spaces remain in the output as ordinary text let's try a command where double quotes will suppress expansions we observe in these examples that when the tilde is used outside of double quotes the tilde expands to the full path name of the current user we also observed that when the star wildcard character is used outside quotes it is expanded here to bar and here to Foo however when the same string is enclosed within double quotes the tilde expansion and the wildcard expansion are both suppressed the tilde is treated simply as a tilde character the star character is treated simply as a star character let's look at another example here we observe race expansion in action the preamble color followed by red or green or blue color red color green color blue this is the result of the expansion when we enclose this same string in double quotes the brace expansion does not take place and the curly braces are treated simply as literal curly brace characters our other special characters that retain their special meaning inside double quotes these exceptions include the dollar sign character curly braces when we when used with the dollar sign character single parentheses when used with the dollar sign character double parentheses when used with the dollar sign character backticks and the backslash character when used to escape these other special characters this allows the following operations to be carried out within double quotes parameter expansion using the dollar sign arithmetic expansion using the dollar sign in parentheses and command substitution let's try a command that is not affected by these double quotes this command contains two examples of parameter substitution here using a dollar sign and curly braces and here using simply a dollar sign the result of that expansion is observed here for this first case where the variable user is replaced with its value lane s which is the name of the current user and again the variable user is replaced with the value of that variable Lane s here arithmetic expansion is performed the dollar sign double parenthesis indicates that arithmetic expansion will be performed the arithmetic expression two plus two is replaced with the result of that arithmetic operation the integer four and finally command substitution the command Cal is run and the output is substituted for this expression here all of these operations expanded as expected despite being within double-quotes parameter expansion gives us the value of the variable named user arithmetic expansion calculated the result of two plus two and command substitution has displayed the output of the command Cal let's take a look at command-line basics single quoted strings many times when passing arguments to a command it is desirable for a string containing spaces to be treated by bash as a single token as we previously discussed one mechanism for obtaining this behavior is to escape the relevant space characters using the backslash character another alternative is to enclose the string' within double quotes a third alternative which we will discuss now is the use of single quotes just as when double quotes are used to enclose a string when a string is enclosed in single quotes that string will be treated by bash as a single token recall that when using double quoted strings word splitting parameter expansion tilde expansion and brace expansion are suppressed but parameter expansion arithmetic expansion and command substitution are still performed when a string is enclosed within single quotes none of these expansion operations are performed let's look at an example but first let's create a new directory containing a couple of empty text files now let's try a command involving tilde expansion brace expansion command substitution arithmetic expansion and parameter expansion [Applause] - begins by tokenizing the argument line resulting in seven tokens which are passed as arguments to echo the first argument is the plain text string hello comma second argument is the plain text string world the third argument is the string tilde / example / star dot txt this argument is expanded using tilde expansion and wildcard expansion into home lanús example a dot text and home lane s example b dot text the fourth argument is 3.7 within curly braces this is expanded using brace expansion in two three four five six seven the fifth argument is file Etsy crontab within dollar parentheses this expands be a command substitution into the result of performing the file command with the argument Etsy crontab resulting in etsy crontab : ASCII text just as if we had typed it at the command line the sixth argument is the arithmetic operation two plus two which is expanded using arithmetic expansion into the result for the seventh argument is the variable user which is expanded using parameter expansion into lanús the echo command prints each of these results separated by a single space character each now let's see what changes when we enclose this argument list within double quotes first of all we can see that the literal space characters in the string are preserved this is because the entire quoted string is treated by Bosch as a single token when the string is enclosed in double quotes tilde expansion is suppressed wildcard expansion is suppressed raise expansion is suppressed but command substitution still takes place arithmetic expansion still takes place and parameter expansion still takes place now let's see what happens when we enclose the argument within single quotes instead of double quotes again we first see that the literal space characters are again preserved this is because as before the entire quoted string is treated by bash as a single token when the string is enclosed in single quotes all forms of expansion are suppressed till the expansion is depressed wildcard expansion is depressed race expansion is suppressed the tuition is depressed arithmetic expansion is suppressed and parameter expansion is suppressed instead we get the exact literal string that we typed in being returned by the echo command
Info
Channel: Geek's Lesson
Views: 1,354,744
Rating: undefined out of 5
Keywords: bash, BASH, Bash command line, Bash command basics, bash command for beginners, bash command tutorials, Bash command advance, linux command line tutorial
Id: 2PGnYjbYuUo
Channel Id: undefined
Length: 203min 8sec (12188 seconds)
Published: Thu Aug 08 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.