Linux Command Line - Directories and Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we're going to issue commands to do things on our computer and the first basic thing that we're going to do is explore the file system so the file system is in a simple terms is made up of a set of directories and files okay so let's do something with directories and files to get started first command will try is PWD and you type the command and press Enter and it shows some output PWD tells me about my present working directory okay PWD Prime present working directory when I execute commands I am in some directory when I execute them which directory well PWD tells me I am in the directory called slash home slash student that's typical for a user name student to be initially in the directory called slash home and followed by the user name student to view contents of directories to list the files inside a current directory we can use the program LS LS lists the contents of a directory and in this case shows an output showing five different five different entries on your terminal it may be different than mine should be very similar but it may be slightly different we our computers some things have changed on some of them but you should see something like that you need to ask questions as we go otherwise I'll keep going put your hand up or just yell out if there's a problem I cannot see everyone at the back but I'll stay seated so I can type better LS shows the list of files and directories in our current directory we'll look at that in detail a moment a hint in this case the blue ones are directories the other ones are files so there are three directories or sub directories in Windows you call them folders maybe but here we call them directories so there are three sub directories let's change into one the one called IT s33 - so change directory we use CD followed by the directory name CD into IT s33 to change into that directory press Enter PWD now you'll see I'm in slash home slash student slash IDs three three two so this represents your current directory and it's what we call a full path or an absolute path it's saying relative to the the base of the file system or the root of the file system we're inside the IT s33 - directory which is inside the student directory which is inside the home directory and the home directory is inside the root of the filesystem the root directory we'll see some more about the exact structure later but let's just maneuver about the filesystem how do you go back I was in home slash student now I'm in home student IDs 3-3 - how do I go back to home slash student CD we can give this the fool sorry the full path could change to CD into slash home slash student note the difference of our two CD examples the first one we CD into i TS 3 3 2 there is no slash at the front this is a relative path change into the directory i TS 3 3 2 relative to where we are there's no slash here whereas the second case was change into the absolute directory slash home slash student which is relative to the root so here we gave the full path we can use either a relative or an absolute path there's other ways to maneuver about let's go back into IDs 3 3 2 so to get back to student we can think we have a hierarchy of directories and a shortcut to go up in the hierarchy is CD dot dot try it try it and see where you will end up so we have directories and you think of it as a tree there's a root and then there's sub directories and subdirectories and so on so we think that we can move down in the tree and move back up in the tree to move up we use the special directory dot dot means go up a directory you don't need to but just to so that it stays at the top of the screen I'm going to use the command clear you don't need to do that clear we'll just bring me to the top of the screen it will clear what we currently have and I'll do some things again currently so very simple concepts we start in slash home slash student we change into 80s three three two using a relative directory we're in home student IDs three three two then we go back we CD up in the hierarchy which takes us back to slash home slash student let's explore a bit more of the directories what about let's go to the root of the file system which is just slash in windows if you're a Windows user the root you'll often recognizes C Drive like C colon slash or that's the topmost directory in our file system have a look LS so in the root of our file system we have a number of sub directories and a few files even the blue ones are sub directories in boot home media var and others these are storing some of our user files and will note that inside the home directory the user files are stored where most of the others are storing parts of the operating system and applications you don't need to know what all of these sub directories do I'll show you a slide lately later with an explanation but these are just sub directory storing things like the operating system files applications user files go back to your original directory I'm slash student which we call our home directory if I'm the student user my home directory is where I start in the terminal which is typically slash home slash the user name go back there so if I'm in the route to go home different waves CD homes there student because I know that's this the relative directory I want to go to and I'm back in home slash student or slightly different I'm currently in route CD slash home slash student and again gets me back to home what's the difference here between CD home slash student CD / homes there student in this case it has the same effect this one is a relative relative to where we start from what we're starting in the root directory so relative to that go to home and student whereas this a nap is an absolute path it ignores where we start from in this case it gives us the same result let's go to the root directory there are a few shortcuts we can use instead of typing slash home slash student I'll clear I'm in the root directory a shortcut for your home directory is this tilde character the squiggly line tilde I call it that is used to replace your home directory try it ok so there's a shortcut if you ever want to refer to your home directory you can use the tilde character for example we go back to the root CD tilde slash I TS three three two means change into the directory which is my home and the subdirectory IITs three three two so tilde really is replaced with home slash student so that user as a shortcut to go home and the faster way to go home is just type CD CD with no parameters will take you home okay so just an alternative no matter where you are on the file system no matter which directory you're in if you want to get to your home directory just type CD and enter and you'll get there any questions so far most of the commands we'll go through we'll just use a few examples of them there are many more features that most commands have that we will not cover we'll show you how to read about the features later we can move around maybe what does the file system look like we said in the root directory there are some sub directories I've got a slide that tries to capture those sub directories or some of them it's usually common across different UNIX and Linux based operating systems these slides are on your on the website and on actually the IT s33 - directory but I'll go direct to one of them the file system hierarchy usually has these common sub directories of bin home Lib and so on being is short for binary usually stores applications binary applications so if you look in the bin directory you'll find many applications home is where your home is your home directory so there can be many users on a single computer the typical place for their home directories will be home slash username in Windows what's the home directory I don't have Windows can anyone tell me where do you find your home directory in windows or in OSX in in markets slash users slash the username I think I think if you look in a Mac you'll actually see there is a home directory but it doesn't use that it uses slash users in Windows I can't remember is it slash slash users or user files or something yeah document yeah so I think those who use Windows can find their home directory I cannot leave be short for libraries libraries are files which usually applications share in Windows you know um usually is DL DL else okay you'll have an application in Exe and it may use a library of executable code in a DLL file okay in UNIX systems we also have libraries which different applications share usually in the basics we don't deal with the pen and the Lib directory the slash root directory don't confuse that with the slash directory slash root is the home of the root user so there is a root user that is an administrator a called root in Windows usually it's called administrator ok so they have a special home directory called slash root we skipped over some et Cie usually contains configuration files if you want to change something in the operating system in Windows you use something like the registry there are registry settings and you can edit those registry registry settings in most UNIX systems the configuration of the operating system is done via text files and most of them are in the et Cie directory slash user is really repeats what we see here but a different level so we see binaries or applications libraries and some source and other files which again applications and application information var is variable things usually websites if your computer is a web server if your computer is an email server your emails will be stored here there's usually a temp directory and there are others you don't need to know them and it may be different on different systems but often we'll see some of these common directories some of them are explained there or very brief explanation let's go back into our file system let's go home let's make a directory we make a directory using mkdir make mk4 make dir directory and then the directory name whatever you like you don't have to copy me make a directory make sure you do it in your home directory okay for those that come in make sure they have Tmax setup yep team ox you'll help him with team ox and you'll help him with team ox or a cake are in good make a directory doesn't matter what you call it but the output should be nothing see when I read make directory it just returns to what we call the prompt there's no warning message or error message if you see something different from this something went wrong probably if you are in the wrong directory for example I was in the root directory and I tried make directory ABC I get an error message you cannot create that directory permission denied it means you as this as the logged in user do not have the permissions to create a directory called ABC in this location we're not going to talk about permissions but I think you can guess that some users can are restricted in what they could do on the file system you as the normal user the student user cannot delete all files from the filesystem you cannot make directories wherever you like you have limitations usually you do it in your home directory so make sure you're in your home directory LS and you should see ABC you shouldn't see hi someone's logged into my computer and and made a directory okay fine won't hurt me okay make a directory let's move on delete a directory RM RM for remove MK for make RM for remove so delete and it's gone let's move on so make directory and delete a directory now the main things with directories we can change we can view our current directory we can make directories remove directories let's move on to files let's create a file alright let's start by creating a txt file we'll use a text editor there are many text editors on your computer the one we will use is called Nano well I will use Nano is a text editor and you can pass in a file name whatever you like you don't have to use the same file name as mine and press ENTER and it will open up a text editor type some message in your text file and then we'll see how to save it finding a seat and get him on Tmax type in your text editor type whatever you like in your text file so the basic text editor on Windows is no pad but there are other text editors you can install your own same on on our urban to Linux we have Nano as a simple text editor but there are many others it gives us some screen where we can type some text now we're not going to teach how to use nano except how to save files and exit and note down the bottom of the screen it gives you some menu the carrot that hat character means control so control G is get help control X is exit control o is save now the main ones you need for today control oh right right the file means save the file if you want to save the file control oh it says do which file name do you want to save it as well the same one that I gave before just press ENTER and it's saved if you want to exit control X if you try to exit before you save it will give you some prompt do you want to save so create a text file again you can put whatever you like into it you don't have to name it the same as mine let's do something with that text file yep how can I change the directory of the save file you mean put it somewhere else we'll move it okay the question is how do I change the directory let's see where mine is or where yours is let's do LS your file should be there okay again you'll see a different set of directories or files than me but you should see the one you just created let's put it somewhere else so the question was how do I change directories well we move it to a different directory MV my new file move you move something from a source to a destination move MV your file name where do you want to put it well if you want to put it in another directory type the name of the directory you want to put it in move my new file txt into IT s33 to directory that shifts it into a different directory LS it's gone from here but if I change into ATS 3 3 2 & LS it's one of those files amongst that Lister files my new file dot txt so MV move move a file from one location to another all of the commands were going through on the reference card so in a slightly different order but we'll get through some of them while we're on MV MV move we can use it to rename files move my new file before I go who can type as fast as me ok typing on the command line is very time-consuming sometimes there are many shortcuts we can use let's try and introduce a few as we go the first shortcut which I find very useful all right there's a set of files in my directory I want to move my new file to somewhere else so I type MV for move and then I start to type the file name M for my new file and the command line has an autocomplete feature you know when you're searching google you start typing in the keyword it presents options to autocomplete well to get autocomplete in the command line press tab the tab character on your keyboard if it does nothing I just press tab you didn't see it but nothing changed it means I cannot autocomplete that it means there's no unique file that starts with M I type the next letter in the file name my new file my press tab it autocompletes why because in this directory there are two files which starts with M when I press tab after typing M it couldn't determine which one I wanted but I've typed in Y and Y and press tab it autocompletes to the one that matches there's only one file that starts with my so that must be the one I want so use tab start to use it all the time because it saves typing makes your life easier if you try again if you press tab nothing happens if you press tab twice it will list the options okay I've got MV M I press tab two times and the command line shows me the two files that start with M just to remind me that in this directory there are two files that start with M I now type Y press tab and autocompletes so if autocomplete doesn't work press tab again and it will give you options of what starts with those particular letters now what are we doing renaming a file renaming a file is the same as moving a file from one name to another name MV my file name - new name whatever you want to call it which is renamed and now my new file has disappeared I now have a file called new name txt so MV is used for moving a file between directories as well as renaming files while we're there let's copy the file CP cp4 copy so CP the file name the source file the one you want to copy and the destination the new one you want to create which is going to be identical to the source and if I do LS I'll see hopefully that this new one exists now come back to LS we're going all over the place but LS we can pass parameters if I do LS it shows me all the files and subdirectories what if I just want to see the text files we can use a wild card like this list all files which match any character star means any character it's a wild card but finish with finishes with dot txt someone deleted my copy I'm going to find out soon all right we'll try again you can do it better than me I've got a file and I can copy it please don't delete it okay LS as with many many programs would take a parameter to filter out which ones the list and star is what we call wildcard meaning match any value so star dot txt means any value that ends with dot txt you can use star in different ways and there in fact other patterns that we can match LS C star every file that starts with a c and you can have more complex patterns so what have we got we can copy we can move or rename we can delete did we delete RM remove delete a file is to remove a file from the file system so RM is the command to delete a file and it should be gone okay RM removes a file there's no trash there's no recycle bin if you remove it's gone okay gone in that you need special techniques if you want to recover it so it doesn't move into a trash or recycle bin it's deleted so be careful don't try and delete important files don't try and delete the operating system okay we can copy we can move we can remove we can add it with Nano let's go back to some of the commands we know and do a few different things with them what let's come back to LS LS lists the files it has many options and on the command line options are usually given as a character following a dash and we'll see how to find the options sorry I'll just clear and go to the top of the screen again you can do this on your computer it may be different output but just try the different commands LS shows the files LS - l show x' the files in long format and to make it sorry it doesn't look very good on my screen because my terminals not big enough the files wrap around you can make your terminal bigger to make it look nicer on your screen okay but let's try again LS - l show the output in long format give a lot of details star dot txt of the text files just move it up then clearing look at this line here what do you see it gives them some details about that fire so the file name going back from the right back the file name that something about the date and time of the file sorry July 25 1054 now with files there are different dates and times associated there's usually creation time modification time when was it last modified and access time when was it last accessed this is the modification time from memory when it was last modified 26 is the size of the file in bytes this file is 26 bytes student and student is something about who owns a file whose file is it the first shooter on the left means who's the user that owns it and the second one means who's the group but today we're not going to cover permissions so we're not going to explain that in any more detail but the owner of the file when our computer system may have many users we need to indicate whose file it is one I always forget I'm not going to try and remember the first set of characters about permissions something telling us about who can read the file read means open who can write the file means edit delete change and dash means usually the third characters execute so the permissions in UNIX are read write and execute read is opening writing is modifying executing is executing running it okay these ten characters know of the ten characters this the last nine tell us about permissions on this file and again I don't want to spend time on explaining it three characters say what the user can do the next three characters say what the group can and the last three characters say what everyone else can do with this file I'm the user student I can read and write this file I cannot execute anyone in the group student can read and write this file they cannot execute any user who's not student and not in the student group can only read the file that's the way we interpret that you need to go study a little bit more about permissions to go into more detail there that's not for today the first character indicates the type of file the main types of files we have our files and directories I'll go back a directory again very hard to see on my screen but you will see the blue ones the first character is Adi meaning this is a directory and the ones which are not blue the first character is a - meaning this is not a directory it's a file so that's how you really know if it's a file or directory' not by the color but by this first character in the output here directory or file so we can view the long format what else what about hidden files I think you know and say on Windows you can have hidden files runs which are not normally shown but they're actually there with a similar concept we have what have we got copy what did I have so I have a file copy my ABC file from my IT s33 2 directory so this is the source I want to copy abc.txt from IT s33 to directory into another file and I could type ok so create a file called another dot txt to make it hidden put a dot in front of it do an LS and you won't see dot another dot txt okay so the concept of a hidden file in UNIX is a file that's name starts with a dot dot files they're not hidden in terms of security usually you can find them quite easily but they're just hidden in terms of convenience they're not listed when we do LS in a normal operation to list it you need to do LS minus a minus a for all files normal files plus hidden files and you see a bunch of hidden files and directories and one of them should be the one that you just created LS - a list all files questions too fast too slow everything okay easy all right okay we'll move on you can combine options try LS minus a minus L that is these - minus a minus L are just options you can usually combine them in any order not always but in most cases list all files - a in long format alright doesn't look so nice there looks better on your screen and there are many other options where do you find the options to LS so now how do we find help well there are things called manuals or man pages manual pages if you know the command I know the command to list files is LS to read about all the details I do man LS man is the program that will show you the help the manual for this program and you can scroll up and down with your keyboard up and down arrows read through this name of this program or command is LS it lists the directory contents the syntax is here and then you go through and it lists all the options scroll up and down - a do not ignore the entries that start with a dot usually there are different formats for specifying options just a single - and a or two dashes and and a word okay the more verbose description many different options there so if you can remember the command but not all the options you use the man page to find those options scroll down read them all to exit this man page press Q Q to quit almost all commands that will use have a man page man what have we used RM explains the RM command and the many options you have page up and page down will scroll through so really you just need to remember the commands and then if you want to find the options use the man page what if you can't remember the command name what if you don't know CP is for copying or RM is for deleting there's a very basic search feature man - K and then some keyword show me all the man pages which contain in the description delete there's a lot we need to scroll up and down to find it gives you the name of the command and the and the description it's not not the best of search features ok but sometimes it will get you what you're looking for so there are many commands that refer to delete so it's a very primitive search for command really if you want the no-work command to do something probably your best bet is to search on the internet how to delete a file in Linux and will give you the command but over time you'll learn those commands and you'll remember ok we can move about directories we can create files let's create a few for a few more files actually I've got my file what did I call it dot file extensions don't matter in Linux or UNIX up to up until now my examples are called files dot txt it means nothing txt with respect to the operating system the extensions do not indicate what the file type is who did that someone's messing with my terminal it's alright how do I attach and back what was I saying file extensions don't matter the file ABC is a text file just because it's not abc.txt doesn't change the contents or the format of the file all right some other ways to view a file cat-cat displays the contents of a file okay very quick way to display all the file when you have a very long file it just lists all those lines what's a very long file I can remember a file called Services there's a file in the etc' directory called services have a look at it it's just a text file it doesn't matter about the contents but when you type and I'll do it again cat and that long file cat displays everything on the screen it doesn't stop so just scroll through not very convenient in some cases so there's another command called less similar to cat displays the contents of the file but it Scrolls one page at a time and you can use your arrow keys to go up and down and page up page down so that's a nicer way to view the contents of a text file I'll do it again less displays a long file page by page allows you to scroll to exit press Q like a man page cat displays the contents of a file as is just displays all of it at once so we can view files what else you want to see the first line of a file we can use head or the first few lines the head of a file head shows by default I think the first 10 lines of a fire you can specify how many you want to see head -1 show me the first one line of a file head shows the head of a file cat shows the contents less shows the contents page by page head shows this the top of the file using the minus operator you can specify how many lines at the top you want to see and to see the bottom of a file what do we use the tail tail shows the end of the file for example by default the last 10 lines but we can specify how many lines we want to see many things in Linux we use text files the configuration of the operating system and there are many programs to support the processing of text files because it can help when we want to automate things what let's find a file this command called find allows you to search through the file system looking for particular files the syntax is quite complex but will just gives them a simple example is find search in slash home slash student files with name the end with txt here's an example find the first parameter is where you want to search where do you want to start your search with directory normally I want to search in this directory then the next parameters this - name says search for files which have the name and I put it in double quotes - to make sure this stays together that is anything dot txt any file that ends with dot txt do you find any and it searches through the home slash student directory all the sub directories it goes through even the hidden ones and prints out the list of files at n with txt so find it can be very powerful to find files on your operating on your filesystem we could do a shortcut find in our home directory instead of having to type slash home slash jutsu and we get the same result so this is the shortcut for slash home slash student if you want to search in your current directory you're in I'll go into IDs 3-3 to find if on a search in the current directory the shortcut for your current directory is dot this directory find looking in this directory the current one and all its sub directories all the files with name ending with dot PDF and it looks in this directory so dot is a shortcut for this directory two dots is a shortcut for the directory up tilde is a shortcut for your home directory the squiggly line find has many other options you don't have to search by name you can search by size by date and when you search you can do things like delete the files that are finds let's say you want to delete all the files which were created after some date on your in your directory you can use find or not do it there's an option to find based upon date or time and there's an option to once you find them to delete them so it can be very powerful to to manipulate your file system fine looks for any file of any type but our programs that we run LS is an application okay LS there's an executable file which implements LS it's a sometimes we call it a command or an application or a a program where is LS where is the file the binary file for LS different ways to find an application which which LS which tells you which binary file implements this command slash bin slash LS so the LS program is in the /bin directory there are others I think where is LS it's a little bit more complicated that where is it searched for not just the program but also the manual page LS the program is in bin the man page the help is this file you don't need to know about that but where is will give you more information about a program there are other ways to search but that's enough for now what have we missed okay a couple more on text files when we did our cat on slash I'm using slash etc' services is just an example of a text file we don't care about the content why it's called this but I just know it's a long text file how long is it how long is etc' services tell me first how many bytes is that file find the number of bytes in this file called services in the e.t.c directory how big is this services for anyone have an answer well a quick way to find LS - l remember LS lists files - L gives us the long format output and it takes as a parameter we can list all files or list a particular file and again it won't display very nicely but the output this file is 19 thousand 281 bytes in length okay how many lines well there's a program called WC word count this services file contains 605 lines of text 2627 words words are separated by spaces and 1921 1281 characters okay if you look in that text file you'll see 600 lines 2600 words and 19,000 characters and of course one character is one byte when you store that text file so the size is 19,000 281 bytes WC word count it's useful to get information about a text for her let's look at some of this text file just look at the first 15 lines of this Services file to see what's in it contains a lot of different words we don't care about it sometimes we want to search through a file find all the lines in a text file which match some particular string grep is one way to do that grep look for the word echo in the file EDC services it uses regular expressions to search through a particular text file in this case any line that contains the word echo should be displayed of course if you want to you can try other words echo I only did echo because I knew there was some lines in there that contained echo what's missing is me we've done directories files help file searching almost there any questions search for all right there's your tasks find all PDF files on your computer hint use find okay find all the PDF files everything that ends with dot PDF on your computer see if you can find them what about lunch what should we do is what program you use to find all your PDFs okay fine and slash you need to say where to look for where to look remember the second parameter or the our programs take parameters so find is a program the next things we type in other parameters so the first parameter is where to start the search front if I want to look in my current directory I use dot but if I want to look in the entire file system you slash because slashes indicates the root directory and the name ends with PDF okay so find looking in the root directory and any of its sub directories it also goes through sub directories which will be everything any file that ends with dot PDF in the name some you may see errors like permission denied meaning you as the Studer user and nada not allowed to look in a particular directory some directories are restricted it'll go for a long time maybe you'll find them if you want to stop a program what do you do control C control C kills a running program control C for lunch today so we'll go for another 30 or 40 minutes and for lunch we're just eating in the canteen but SI t will pay it's a little bit easier we're just going to write down what you want and then we'll order it so it's there on time a few more shortcuts in the terminal how do you run a command that you've recently executed I just did a find use your up and down arrows on your keyboard if I press the up arrow it goes to the previous command the history of commands keep pressing up and you scroll through the commands up and down will scroll through the history of commands what else if you want to list all the commands type history so the terminal keeps track of the commands that you've been using and it shows your history all the commands you've executed and it gives a number from from the start you want to execute one maybe you typed a real long command you don't want to type it again you want to execute a number command of a particular number for example I want to execute command number one oh four exclamation mark 104 execute that command now you need to be careful you need to know that 104 is the command you want and you get that from the history exclamation mark what's the name its called bang when there's a bang there's an exclamation so it's often called the bang character bang 104 there are many extensions to that to speed things up which will not cover okay what else questions on the commands we've done any problems everything okay you don't you may not remember every command use the reference card you need to practice to learn some of them let's try some different things
Info
Channel: Steven Gordon
Views: 199,056
Rating: undefined out of 5
Keywords: Steven Gordon, Thammasat University (College/University), Sirindhorn International Institute Of Technology (College/University), GNU/Linux (Operating System), Ubuntu (Operating System)
Id: KVH3dMWefWE
Channel Id: undefined
Length: 63min 4sec (3784 seconds)
Published: Mon Jul 28 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.