In this lesson,
we're going to discuss how you conduct operations
on different files and directories
within the Linux operating system
using some basic commands.
First, we're going to talk about cat.
Now, cat is short for concatenate.
This can actually display, combine,
or create text files for you.
Cat is most frequently used to display the contents
of small text files.
Now, keep in mind,
cat does not have a screen scrolling capability.
Instead, it's just going to keep going over and over again
until it shows all of the file on the screen
and so things can go way off your screen pretty quickly.
Now there are some options available to use
with the cat command.
If you use dash N,
this will actually show the line numbers
in front of the output.
If you use dash B,
this will be actually used to number the lines,
excluding any blank lines.
If you use dash S,
this will suppress output of any repeated empty lines.
And option dash V is used to display
non-printable characters,
such as visible characters other than tabs,
new lines, and form feeds.
If you use the option dash E,
you're going to actually create a dollar sign character printed
at the end of each line.
And if you use dash T,
it's going to print out the tabs
either as a carrot I or a carrot L if it's a form feed.
To run the cat command,
simply type in cat, the options, and the file name.
Now, the next command we're going to talk about is head.
Head is a command that displays
only the first 10 lines of each file.
Similarly, we have a command known as tail,
and tail will show us the last 10 lines of each file.
These commands are really useful
if you only need to see the beginning or ending of a file.
Now, why might you do this?
Well, let's say you want to check a recent log entry
by viewing the last 10 lines of the log file.
You can simply run the head or tail command
by typing in head or tail, the options, and the file name.
The tail command has two commonly used options.
The first one is dash F.
Dash F will dynamically watch a file
and it'll output will automatically update on the screen
whenever that file changes.
The dash N option will allow us to control
how many lines are actually being displayed.
So if I type in dash N five,
I'll only see the last five lines
of a file instead of the 10 that's used by default.
Now, the dash F option only works in tail,
but the dash N option where you can specify the number
of lines to display, will work in both head and tail.
Now, there's two more commands that I want to talk
about here when it comes to displaying files to your screen
and these are known as less and more.
Both the less and more commands are similar
in a way that they enable you to display the contents
of a file and page through those one screen at a time.
The less command has some additional features
that more doesn't,
but newer versions of more have added
a lot of those features too, making them pretty similar.
While you're free to use either command you want,
the less command is generally used by most people.
You can run the less or more command
by simply typing less or more,
the options, and the file name.
There's also some common less command options
that you might want to be familiar with.
Things like dash E,
which is used to exit the program
the second time it reaches the end of the file.
The dash capital E option will be used to exit the program
the first time it reaches the end of the file.
If you use dash capital I,
it'll ignore case inside of your searches.
And if you use dash N,
it'll suppress any line numbers in the file.
Navigation and less uses many
of the same commands you've seen before.
things like the arrow keys to scroll up one line
at a time or down one line at a time,
or page up or page down to scroll a page at a time.
You can also use the forward slash character to
search a file for a particular text string.
And if you press N or capital N,
you can move to the next or previous instance
of a search string respectively.
When you're done, just press Q to quit the program.
Another command that's really important is the copy command.
The CP command enables you to copy
and then paste a file or directory someplace else.
The initial object, that file or directory,
will be left where it is,
but an exact duplicate of that object,
that file or directory,
will be created at the destination you specify.
When you're copying directories,
you may want to specify the dash capital R option.
This will copy the directory recursively with everything
underneath it going as well.
To run this command, simply type CP, your options,
and the file or directory name that you're going to copy
and the file or directory name you are going to put it to.
For example,
let's say I wanted to copy the my files directory
and put its contents into the
slash OPT slash my files directory.
I would do this by typing CP dash capital R
tilde slash my files slash OPT slash my files.
If you want to move files
or directories to another location instead of copying them,
you can do this using the move command,
which is written as MV.
MV is similar to the copy command,
but it doesn't leave the initial object,
that file or directory, in place.
Instead, move is more like a cut and paste operation
instead of a copy and paste operation.
The bash shell itself does not have
a dedicated rename command, but instead,
we use the move command to do that function.
The act of moving a file or directory and
supplying a new name is essentially the same as renaming
that file or directory.
The proper way to run the move command is to type move,
the options, the filer directory name to move,
and the file or directory name's destination.
For example, let's say I wanted to move something
from tilde slash file one to slash OPT slash file one.
I can do this by typing MV tilde slash file one
slash OPT slash file one.
When you wish to rename a file,
you can do this by simply typing MV, your options,
the old name, and then the new name.
So, if I wanted to change file one to file two,
I can just type MV file one file two
and it will change file one to become file two.
The next command we're going to talk about
is the touch command.
Now, the touch command is used when you need to
test permissions or simply create files that are
going to be processed by some application.
The touch command is available
in most distributions in Linux.
Touch is going to change the time of access
or the modification time of a file to the current time
or you can use it to actually specify
the time inside of an argument.
Touch is also used to create an empty file
with a specified file name,
assuming that that file doesn't already exist.
To do this, we just type touch and the file name.
When you do this, if the file doesn't exist,
a new one will be created.
If one already exists,
it will update its last access time to the current time.
Another command we have is the remove command.
The remove command is written as RM.
The remove command will remove files and directories.
If you want to do this recursively,
you're going to use the dash capital R option
and this will recursively remove files, sub directories,
and the parent directory itself.
The proper way to run the remove command is to type
remove, the options, and the file or directory name.
So let's say I wanted to remove the
tilde slash my files directory and all of its contents.
I can do that by typing RM dash capital R
tilde slash my files.
There's also going to be times when you
need to remove files but not directories
and you can do this using the unlink command.
When you use the unlink command,
it's very similar to using the remove command.
Now, at this point,
you have seen much of the power
of the LS or list command that we've done to
list the contents of directories
as well as their permissions information
of directories and files when we talked about users, groups,
and permissions earlier in the course.
Now, there are a lot of useful options
when you're using the LS command.
Let's cover a few of these key options.
The first one is dash L.
If you use dash L, this is going to display a long list,
including the permissions, the number of hard links,
the owner, the groups, the size, the date,
and the file name all for you right there on the screen.
If you use dash F,
this is going to be used to display the nature of a file,
basically telling you is it an executable file
and slash if it's going to be a directory.
If you have the dash A option,
this is going to display all the files
in the present directory,
including the files whose names begin with a period,
which is a hidden file.
If you use the dash capital R option,
this'll recursively display all the sub directories
as well as the present directory.
If you use dash D,
it'll display information
about symbolic links or directories
rather than the link's target
or the contents of that directory.
And if you use the dash capital L,
it'll display all the files in the directory,
including your symbolic links.
To run the LS command,
you'll type LS, the options, and the file or directory name.
Normally, when I execute the LS command,
I like to use LS dash LA.
By doing that,
I'm going to list out all the files and directories,
including their permissions,
and the A tells me to show me everything
including those hidden files.
If you're in the bash shell
and you execute the LS command,
you may notice that the results sometimes appear
in different colors.
These colors are used to distinguish the
different file types.
The different colors distinguish normal text files.
while blue is going to be used for directories.
Sky blue will be used for symbolic links and audio files.
And if you're using green, that shows an executable file.
If you have yellow with a black background, that's a device.
If you have pink, that's an image file.
If you have red, that's an archive file.
And if you have red with a black background,
that distinguishes it as a broken link.
These colors can be very useful when you're looking
at this to figure out exactly what you're looking at.
Now, another command we need to talk about
is how we make directories,
and this is going to be done using the MKDIR command.
This is the make directory command.
It's used to create or make a directory.
To use it, you're simply going to give the name
of the directory you want as an argument.
So, I would type MKDIR Jason to create the Jason directory.
If I want to remove that directory,
I can use the remove directory command, which is RMDIR.
Now, when you're using RMDIR,
it will only delete a directory if that directory is empty.
If you want to delete a directory that has things
in it like files or other directories,
you're going to have to use the remove command,
RM with the recursion.
So it's RM dash R and the name of the directory.
To use either make directory or remove directory,
simply type in make directory and the directory name
or remove directory and the directory name.
So, as you can see,
there are lots of different commands
that you need to be familiar with
inside the Linux operating system to get
around in the command line.
I recommend you take some time to practice
each of the ones we covered
in this lesson and get familiar with them.