Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linux Unhatched

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 24

Network Development Group (NDG) Linux Unhatched

Basic Command Syntax


CLI or Command Line Interface

- the ls command displays a listing of information about files; a list of files contained
within the current directory

- options alter the behavior of the command


- arguments are items or values for the command to act upon.
- a command is a software program that when executed on the CLI (command line
interface), performs an action on the computer

Arguments
- used to specify something for the command to act upon

eg. (ls=command,Documents=name of a directory and argument) =>


it will list the contents of that directory

Options
- used to alter the behavior of a command
- the -l option is provided to the ls command, which results in a "long display" output,
meaning the output gives more information about each of the files listed
- the -r option will print the results in reverse alphabetical order

Printing Working Directory

- pwd command prints the working directory, your current location within the filesystem

Changing Directories

- directories are a type of file used to store other files, they provide a hierarchical
organization structure
- the cd (change directory) command is used to navigate the filesystem structure; to
change directories
1
eg. To move to the Documents directory, use it as argument to the cd command

- the / character=the root directory


- The argument to the cd command is more than just the name of a directory, it is
actually a path. A path is a list of directories separated by the / character

Absolute Paths
- start at the root of the filesystem (directory) therefore it always begins with
the / character
- it allows you to specify the exact location of a directory

eg.

confirm this using the pwd command:

Relative Paths
- start from your current location; gives directions to a file relative to your current
location in the filesystem
- they start with the name of a directory

eg.

Documents=relative path

2
To move to the Art directory -> cd School/Art (relative path used as an argument)

Shortcuts
The .. Characters - represents one directory higher relative to the current

eg. To move from the Artdirectory back to the School directory:

The . Character - represents your current directory

The ~ Character – represents the home directory of the current

Listing Files

- the ls command is used to list the contents of a directory


- to learn the details about a file, such as the type of file, the permissions, ownerships or
the timestamp, perform a long listing using the -l option to the ls command

eg.

Each line corresponds to a file contained within the directory. The information can be
broken down into fields separated by spaces.

File Type - the first field actually contains ten characters, where the first character indicates
the type of file and the next nine specify permissions

3
Permissions - how certain users can access a file

Hard Link Count - how many hard links point to this file

User Owner - user syslog owns this file. Every time a file is created, the ownership is
automatically assigned to the user who created it

Group Owner - Indicates which group owns this file

File Size - the size of the file in bytes

Timestamp - indicates the time that the file's contents were last modified

Filename - name of the file or directory

4
Sorting
- by default the output of the ls command is sorted alphabetically by filename
- the following options combind with the -l option will give relevant details of the files
- -t option will sort the files by timestamp

eg.

- -S option will sort the files by file size

eg.

- -r option will reverse the order of any type of sort

eg.

! Used alone the -r option with list the files in reverse alphabetical order:

eg.

Administrative Access
- su Command - allows you to temporarily act as a different user. It does this
by creating a new shell. By default, if a user account is not specified, the su command
will open a new shell as the root user, which provides administrative privileges

- after executing the su command, a password is required; as a security measure, the


password will not be visible as it is typed
- the command prompt has changed to reflect that you are now logged in as the root
user. To logout and return to the sysadmin account, use the exit command

eg.

5
- sudo command - allows a user to execute a command as another user
without creating a new shell

cd ~/ - switch to a diretory

Permissions
- determine the ways different users can interact with a file or directory. When listing a
file with the ls -l command, the output includes permission information

- after the file type character, the permissions are displayed. The permissions are
broken into three sets of three characters

- the first set is for the user who owns the file
- the user who owns the file, and who these permissions apply to, can be determined by
the user owner field

- the second set is for the group that owns the file
- the group for this file can be determined by the group owner field

- the last set is for everyone else, any one who that first two sets of permissions do not
apply to. If you are not the user who owns the file or a member of the group that owns
the file, the third set of permissions applies to you

Permission Types
- read, write, and execute. The manner in which these permissions apply differs for files
and directories

6
Changing File Permissions
- the chmod command is used to change the permissions of a file or directory. Only the
root user or the user who owns the file is able to change the permissions of a file
- there are two techniques of changing permissions with
the chmod command:symbolic and octal. The symbolic method is good for changing one
set of permissions at a time. The octal or numeric method requires knowledge of the
octal value of each of the permissions and requires all three sets of permissions (user,
group, other) to be specified every time

- first indicate which set of permissions is being changed

- next, specify an action symbol

7
- after an action symbol, specify one or more permissions to be acted upon

- finally, a space and the pathnames for the files to assign those permissions

eg. The file hello.sh used in the examples on the previous page is a script. A script is a file
that can be executed, similar to a command:

However currently, the execute permission is not set for any of the permission groups:

Attempting to execute this script using the following syntax fails:

Since the system is currently logged in as the sysadmin user, and sysadmin is the owner of
this file, giving the user owner the execute permission should allow you to execute this
script. Using the chmod command with the u character to represent the user owner permission
set, the + character to indicate a permission is being added, and the x character to represent
the execute permission, the command should be executed as follows:

8
No output indicates the command succeeded. Confirm by checking the permissions using
the ls -l command:

The user owner now has the execute permission listed:

Changing File Ownership


- the owner of a file is the user who creates it. The chown command is used to change the
ownership of files and directories. Changing the user owner require administrative
access

- the chown command also permits changing group ownership, which can be
accomplished by either root or the owner of the file

- to change the user owner of a file, the following syntax can be used. The first
argument [OWNER] specifies which user is to be the new owner. The second
argument FILE specifies of which file the ownership is changing

eg. Currently all the files in the Documents directory are owned by the sysadminuser. This can
be verified by using the ls -l command. Recall the third column indicates the user owner.

To switch the owner of the hello.sh script to the root user, use root as the first argument
and hello.sh as the second argument. Don't forget to use the sudo command in order to gain
the necessary administrative privileges. Use password netlab123 when prompted:

9
Confirm the user owner has changed by executing the ls -l command. Use the filename as
an argument to limit the output:

Moving Files

- the mv command is used to move a file from one location in the filesystem to another

- the mv command requires at least two arguments: source (the files to be copied) and
destination ( the place to where the copies are placed)

eg. To move the people.csv file into the Work directory, use the filename as the source, and the
directory name as the destination:

If a file is moved from one directory to another and without specifying a new name for the
file, it will retain its original name. The move above can be confirmed using the ls command
on the Work directory:

Copying Files

- the cp command is used to copy files; it requires at least two arguments: a source and
a destination

eg.

The result of executing the previous command would create a copy of the contents of
the /etc/passwd file in the Documents directory, since that is our current directory. This can be
confirmed using the ls command

- the dd command is a utility for copying files or entire partitions at the bit level.
10
eg. Let's examine the following example, the dd command creates a file
named /tmp/swapexwith 50 blocks of zeros that are one megabyte in size:

Removing Files
- the rm command is used to delete permanently files and directories

- the rm command is typically used to remove regular files

11
eg.

The rm command will ignore directories that it's asked to remove; to delete a directory, use
the recursive option, either the -r or -R options. Just be careful since this will delete all files
and all subdirectories:

Filtering Input
- the grep command is a text filter that will search input and return lines, which contain
a match to a given pattern

eg. For example, the passwd file we previously copied into the Documents directory contains
the details of special system accounts and user accounts on the system. This file can be
very large, however the grep command can be used filter out information about a specific
user, like the sysadmin user. Use sysadmin as the pattern argument and passwd as the file
argument:

The command above returned the line from the passwd which contains the pattern sysadmin.

Regular Expressions
- regular expressions have two common forms: basic and extended

12
- the following table summarizes the extended regular expressions, which must be used
with either the egrep command or the -E option with the grep command

- the simplest of all regular expressions use only literal characters, like the example
from the previous page:

Anchor Characters
- anchor characters are one of the ways regular expressions can be used to narrow
down search results. For example, the pattern root appears many times in
the /etc/passwd file:

eg.

13
- the first anchor character ^ is used to ensure that a pattern appears at
the beginning of the line. For example, to find all lines
in /etc/passwd that start with root use the pattern ^root. Note that ^ must be
the first character in the pattern to be effective

eg.

- the second anchor character $ can be used to ensure a pattern appears at the end of
the line, thereby effectively reducing the search results. To find the lines that end with
an r in the alpha-first.txt file, use the pattern r$:

eg.

- again, the position of this character is important, the $ must be the last character in
the pattern in order to be effective as an anchor

Match a Single Character With .


- the cat command can be used to print the contents of a file

- one of the most useful expressions is . It will match any character except for the new
line character. The pattern r..f would find any line that contained the letter r followed
by exactly two characters (which can be any character except a newline) and then the
letter f

eg.

- this character can be used any number of times. To find all words that have at least
four characters the following pattern can be used

eg.

Match a Single Character With [ ]


- the square brackets [ ] match a single character from the list or range of possible
characters contained within the brackets

14
eg.

To find all the lines in the profile.txt which have a number in them, use the
pattern [0123456789] or [0-9]:

On the other hand, to find all the lines which contain any non-numeric characters, insert
a ^ as the first character inside the brackets. This character negates the characters listed:

Match a Repeated Character Or Patterns With *


- the regular expression character * is used to match zero or more occurrences of a
character or pattern preceding it. For example e* would match zero or more
occurrences of the letter e

eg.

It is also possible to match zero or more occurrences of a list of characters by utilizing


the square brackets. The pattern [oe]* used in the following example will match zero or more
occurrences of the o character or the e character:

15
Shutting Down

- the shutdown command arranges for the system to be brought down in a safe way. All
logged-in users are notified that the system is going down and within the last five
minutes leading up to the shutdown, new logins are prevented

eg.

- unlike other commands used to bring the system down, the shutdown command requires
a time argument specifying when the shutdown should begin. Formats of this time
argument can be the word now, a time of day in the format hh:mm or the number of
minutes to delay in the format +minutes

Network Configuration

- the ifconfig command stands for "interface configuration" and is used to display
network configuration information

- the ifconfig command can also be used to temporarily modify network settings

Viewing Processes
- running a command results in something called a process; generally the operating
system will differentiate users based upon whether they are the administrator.
Typically regular users, like the sysadmin user, cannot control another user's

16
processes. Users who have administrative privileges, like the rootaccount, can control
any user processes, including stopping any user process.

- the ps command can be used to list processes

- the ps command will display the processes that are running; In the example, the
bottom line is the process created by the execution of the ps command. The output
includes the following

- PID: The process identifier, which is unique to the process. This information is useful to
control the process by its ID number.

- TTY: The name of the terminal where the process is running. This information is useful
to distinguish between different processes that have the same name.

- TIME: The total amount of processor time used by the process. Typically, this
information isn't used by regular users.

- CMD: The command that started the process.

Instead of viewing just the processes running in the current terminal, users may want to view
every process running on the system. The -e option will display every process

eg.

Package Management
- package management is a system by which software can be installed, updated,
queried or removed from a filesystem. In Linux there the two most popular are those
from Debian (Ubuntu) and Red Hat

- the Advanced Package Tool, apt-get, a front-end program to the dpkg tool, makes
management of packages even easier

17
Installing Packages
- package files are commonly installed by downloading them directly from repositories
located on Internet servers; . Before installing a package, it is good practice to use the
refresh the list of available packages using the apt-get update command

eg.

To search for keywords within these packages, you can use the apt-cache
search command.

The keyword that is used should match part of the name or description of the package
that is to be located.

Once you've found the package that you want to install, you can install it with the apt-get
install command

Updating Packages
- the apt-get install command can also update a package, if that package is installed
and a newer version is available; Updating all packages of the system should be done
in two steps. First, update the cache of all packages available with apt-get update.
Second, execute the apt-get upgrade command and all packages and dependencies will
be updated

Removing Packages
- the apt-get command is able to either remove or purge a package. The difference
between the two is that purging deletes all package files, while removing deletes all
but the configuration files for the package.

18
- an administrator can execute the apt-get remove command to remove a package or
the apt-get purge command to purge a package completely from the system

Updating User Passwords


- the passwd command is used to update a user’s password. Users can only change their
own passwords, whereas the root user can update the password for any user

eg.

If the user wants to view status information about their password, they can use the -
S option:

The output fields are explained below:

su root -switch to root account

19
Text Editor
- the premier text editor for Linux and UNIX is a program called vi

 The vi editor is available on every Linux distribution in the world. This is not true of any
other editor.

 The vi editor can be executed both in a CLI (command line interface) and a GUI
(graphical user interface).

 While new features have been added to the vi editor, the core functions have been
around for decades.

- In reality, most Linux systems don't include the original vi, but an improved version of
it known as vim, for vi improved

To get started using vi, simply type the command followed by the pathname to the file to
edit or create:

- There are three modes used in vi: command mode, insert mode, and ex mode.

Command Mode Movement


- initially, the program starts in command mode. Command mode is used to type
commands, such as those used to move around a document, manipulate text, and
access the other two modes. To return to command mode at any time, press
the Esc key.

- once some text has been added into a document, to perform actions like moving the
cursor, the Esc key needs to be pressed first to return to command mode

- Movement commands in vi have two aspects, a motion and an optional number prefix,
which indicates how many times to repeat that motion. The general format is as
follows:

20
- these motions can be prefixed with a number to indicate how many times to perform
the movement. For example, 5h would move the cursor five characters to the
left,3w would move the cursor three words to the right.

- to move the cursor to a specific line number, type that line number followed by
the Gcharacter. For example, to get to the fifth line of the file type 5G. 1G or gg can be
used to go to the first line of the file, while a lone G will take you to the last line. To find
out which line the cursor is currently on, use CTRL-G.

Command Mode Actions

Delete
- delete removes the indicated text from the page and saves it into the buffer, the buffer
being the equivalent of the "clipboard" used in Windows or Mac OSX

21
Change
- change is very similar to delete, the text is removed and saved into the buffer, however
the program is switched to insert mode to allow immediate changes to the text

Yank
- yank places content into the buffer without deleting it

Put
- put places the text saved in the buffer either before or after the cursor position

Searching in vi
- to search forward from the current position of the cursor, use the / to start the search,
type a search term, and then press the Enter key to begin the search. The cursor will
22
move to the first match that is found.To proceed to the next match using the same
pattern, press the n key. To go back to a previous match, press the N key

Insert Mode
- Insert mode is used to add text to the document

Ex Mode
Originally, the vi editor was called the ex editor. The name vi was the abbreviation of
the visual command in the ex editor that switched the editor to "visual" mode.

In the original normal mode, the ex editor only allowed users to see and modify one line at a
time. In the visual mode, users could see as much of the document that will fit on the screen.
Since most users preferred the visual mode to the line editing mode, the ex program file was
linked to a vi file, so that users could start ex directly in visual mode when they ran
the vilink.

Eventually, the actual program file was renamed vi and the ex editor became a link that
pointed the vi editor.

When the ex mode of the vi editor is being used, it is possible to view or change settings, as
well as carry out file-related commands like opening, saving or aborting changes to a file. In
order to get to the ex mode, type a : character in command mode. The following table lists
some common actions performed in ex mode:

23
-

24

You might also like