Module 3 - Linux Cli - LFCP
Module 3 - Linux Cli - LFCP
Module 3
Linux CLI
Thu Ya
The Shell
• The shell is basically a program that takes your commands from the
keyboard and sends them to the operating system to perform. If
you’ve ever used a GUI, you’ve probably seen programs such as
“Terminal” or “Console” these are just programs that launch a shell
for you.
• In this course we will use the shell program bash (Bourne Again shell),
almost all Linux distributions will default to the bash shell. There are
other shells available such as ksh, zsh, tsch, but we won’t get into any
of those.
The Shell
1
10/2/2024
cd (Change Directory)
ls (List Directories)
• How do we figure out what is available to us? Right now it’s like we
are moving around in the dark. Well, we can use the wonderful ls
command to list directory contents. The ls command will list
directories and files in the current directory by default, however you
can specify which path you want to list the directories of.
2
10/2/2024
cat
• We’re almost done navigating files, but first let’s learn how to read a
file. A simple command to use is the cat command, short for
concatenate, it not only displays file contents but it can combine
multiple files and show you the output of them.
• It’s not great for viewing large files and it’s only meant for short
content. There are many other tools that we use to view larger text
files that we’ll discuss in the next lesson.
history
• In your shell, there is a history of the commands that you previously
entered, you can actually look through these commands. This is quite
useful when you want to find and run a command you used
previously without actually typing it again.
• Want to run the previous command without typing it again? Use !!. If
you typed cat file1 and want to run it again, you can actually just go !!
and it will run the last command you ran.
Tab
• While we are talking about useful things, one of the most useful
features in any command-line environment is tab completion. If you
start typing the beginning of a command, file, directory, etc and hit
the Tab key, it will autocomplete based on what it finds in the
directory you are searching as long as you don’t have any other files
that start with those letters. For example if you were trying to run the
command chrome, you can type chr and press Tab and it will
autocomplete chrome.
3
10/2/2024
cp (Copy)
• Let’s start making some copies of these files. Much like copy and
pasting files in other operating systems, the shell gives us an even
simpler way of doing that.
• You can copy multiple files and directories as well as use wildcards. A
wildcard is a character that can be substituted for a pattern based
selection, giving you more flexibility with searches. You can use
wildcards in every command for more flexibility.
• $ cp *.txt /home/maung/documents
cp (Copy a directory)
• Try to do a cp on a directory that contains a couple of files to your
Documents directory. Didn’t work did it? Well that’s because you’ll
need to copy over the files and directories inside as well with -r
command.
• $ cp -r Pumpkin/ /home/pete/Documents
4
10/2/2024
mv (Move)
• Used for moving files and also renaming them. Quite similar to the cp
command in terms of flags and functionality.
rm (Remove)
• To remove files you can use the rm command. The rm (remove)
command is used to delete files and directories.
• $ rm file1
• $ rm -r directory1
help
• Linux has some great built-in tools to help you how to use a
command or check what flags are available for a command. One tool,
help, is a built-in bash command that provides help for other bash
commands (echo, logout, pwd, etc).
• $ help echo
5
10/2/2024
man
• Manual of cli programs
• Man pages are manuals that are by default built into most Linux
operating systems. They provide documentation about commands
and other aspects of the system.
• Try it out on a few commands to get more information about them.
• $ man ls
whatis
• Whew, we’ve learned quite a bit of commands so far, if you are ever
feeling doubtful about what a command does, you can use the whatis
command. The whatis command provides a brief description of
command line programs.
• $ whatis cat
less
• If you are viewing text files larger than a simple output, less is more. (There is
actually a command called more that does something similar, so this is ironic.)
The text is displayed in a paged manner, so you can navigate through a text file
page by page.
• Use the following command to navigate through less:
• q - Used to quit out of less and go back to your shell.
• Page up, Page down, Up and Down - Navigate using the arrow keys and page keys.
• g - Moves to beginning of the text file.
• G - Moves to the end of the text file.
• /search - You can search for specific text inside the text document. Prefacing the
words you want to search with /
• h - If you need a little help about how to use less while you’re in less, use help.
6
10/2/2024
Nano or vim
• Text editor
file
• In Linux, did you notice that the filename didn’t conform to standard
naming like you’ve probably seen with other operating systems like
Windows? Normally you would expect a file called banana.jpeg and
expect a JPEG picture file.
• In Linux, filenames aren’t required to represent the contents of the
file. You can create a file called funny.gif that isn’t actually a GIF.
• To find out what kind of file a file is, you can use the file command. It
will show you a description of the file’s contents.
exit
• To exit from the shell, you can use the exit command
7
10/2/2024