Вы находитесь на странице: 1из 16

Linux Essentials

LPI Linux Essentials Certification by The Urban Penguin

http://www.theurbanpenguin.com

Linux Essentials

This material was originally produced by The Urban Penguin, http://www.theurbanpenguin.com . Video material supporting you material can be found on this website and the blog site http://wp.theurbanpenguin.com . Andrew Mallett is The Urban Penguin and he holds Linux certification in Red Hat, SUSE, Ubuntu as well as the LPI. For updates you can follow theurbanpenguin: on twitter @theurbanpenguin on Facebook http://www.facebook.com/theurbanpenguin This material has been produced under the Creative Commons share and share alike license. As such it can be freely distributed and shared on the understanding that no charge is made for this training material. This document was created on Ubuntu 13.10 and LibreOffice 4.0. Many of the screen-shots have been captured using Kazam.

http://www.theurbanpenguin.com

Linux Essentials

Topic 2: Finding Your Way on a Linux System (weight: 8)


2.1 Command Line Basics
Weight 2

Description Basics of using the Linux command line. Key Knowledge Areas: Basic shell. Formatting commands. Working With Options. Variables. Globbing. Quoting. echo. history. PATH env variable. export. which.

The following is a partial list of the used files, terms and utilities:

Nice to know: Substitutions ||, && and ; control operators For this LPI Linux Essentials objective we take a look at the Linux command line basics and starting to become accustomed to using the CLI, command line interface, that Linux supplies. Just as many Microsoft Windows users may never use the command line; the same may be said of Linux users. However, even within Windows we are seeing the evolution from GUI to command line management. This is seen clearly with Microsoft Windows Server 2012 and PowerShell 3. It does not matter the OS that you use: the importance of being able to do things repeatedly correct from the command line is always going to have its importance. If you would like to work with Linux servers then the command line has to be mastered and we can begin right here , right now! Looking just at the basics we will see how we can open a shell and exit with the exit command or Ctrl + D. We can see how easy it to adjust font size with Ctrl + Shift + + and reduce with Ctrl + -, to clear the screen it is Ctrl + L.
Note: Ctrl + L is a short-cut key sequence within the bash shell but not other

http://www.theurbanpenguin.com

Linux Essentials
shells so check the shell that you are using. The output from the command echo $SHELL will let you know your current shell.

We can see how to use commands and space out the arguments and options from commands. All of out commands we type are stored in a history file, .bash_history so they persist even after reboots. The command history itself can be used to display and clear the command line history for a given user, each user has their own unique .bash_history file which is created in the user's home directory. history history3 historyc From the command line examples: history without any arguments displays the users' history history 3 show the last 3 lines of the users' history history -c will clear the users' current command line history from RAM

Of course variables come into play to store information about our session, when reading variables they are prefixed with a $ symbol. Typing $ followed by TAB TAB in quick succession will list all variables as will the env (/usr/bin/env) command; additionally the command env displays the variables and their values. As we have seen spacing out options will make them seem as two options ; this means that we can create two directories with the one command mkdirdir1dir2; Should we want a space in the directory name we can use quotes or the \ (backslash) character. mkdirdir1dir1 The above command will create a single directory named dir1[space]dir2. The supporting video will demonstrate the usage. Finally we will look at ANDing and ORing command together to allow for some simple command line flow control. For example: mkdirdir1&&cddir1 The above command will make the directory and if the command succeeds it will enter the directory http://www.theurbanpenguin.com

Linux Essentials with the command cd. idbob||useraddbob The above example using the double vertical bar or pipe, ORs the two commands together. In this way if the first command fails, in the case of there not being a user bob; then, the useradd command will run and we create the user named bob. The second command only runs if the first command fails. The supporting video is just under 30 minutes; however I think you will find it really worthwhile and hopefully entertaining http://www.youtube.com/watch? feature=player_embedded&v=ptk6A-UEjWM

2.2 Using the Command Line to Get Help


Weight 2

Description Running help commands and navigation of the various help systems. Key Knowledge Areas: Man. Info. The following is a partial list of the used files, terms and utilities: man. info. Man pages. /usr/share/doc. locate.

Nice to know: apropos, whatis, whereis. For this LPI objective of the Linux Essentials certification we will take a look at how we can obtain help from the Linux command line, this might be simple help using the --help option to the program, or more detailed help from man or info.

Man Pages
Man or manual pages are broken into sections, these sections help identify how the command may be used, for example as a standard user or as root. Section 1 : For help on if used as a standard user Section 5 : For help on the configuration file Section 8 : For help on using this as root

Other sections exit but these are most common. Using the command whatis (/usr/bin/whatis) it is http://www.theurbanpenguin.com

Linux Essentials easy to find man pages that exist for a given command: whatiscrontab The output of this command shows that there are man page section 1 and 5 that exist for crontab. This can be seen clearly in the following screen-shot:

If we specifically wanted to see the man pages for the crontab configuration files we could issue the command: man5crontab Likewise, specifically to access the help for using crontab as a user we could issue the command: man1crontab Although if no section was specified the lowest man page section is selected, so in this case section 1 is the default. Once in a man page you can navigate using the arrow keys one line at a time or use the page up and page down keys to page up and down a page at a time. Using the / we can search for a string, (text), in the document, for example: /FILES would search for the text FILES in the man page. When finished you can use the letter q to quit.

Info pages
Some commands may have info pages instead of or in addition to man pages. Info pages are accessed with the command info(/usr/bin/info). If a specific info page does not exist for a given command then /usr/bin/info will open the corresponding man page. The idea of the info pages is that they can be hyper-linked to provide for easier reading when the help manual is large. infols The above command will open the info page for the command ls.

Which
The command which(/usr/bin/which) is a really useful command in Linux. Using this command we can search for executable programs or scripts that are located in the PATH variable. For example the using the command: whichls The PATH variable is searched and starting at the first directory found in the PATH we look for the command ls. If it is not found the next directory is searched and so on. Using the -a option which will list all found occurrences of the given command, as such it does not stop the search on the first http://www.theurbanpenguin.com

Linux Essentials match. whichals Either with or without the -a option executables are only searched for within the PATH variable. This the same behaviour of running the program with just the program name, such as: ls If you need to run a program that is not within a directory located in the PATH variable you must include the file path to the program such as: /usr/opt/myprogs/ls

Locating man pages and programs


We have already seen that whatis can be used to locate man pages; similarly we can use programs like man -k or apropos to list where the man pages are located. whereis will list the executables and man pages are for a given command. whereisls

The supporting video is here : http://www.youtube.com/watch? feature=player_embedded&v=LsWWsCva7hY

2.3 Using Directories and Listing Files


Weight 2

Description Navigation of home and system directories and listing files in various locations. Key Knowledge Areas: Files, directories. Hidden files and directories. Home. Absolute and relative paths.

The following is a partial list of the used files, terms and utilities: Common options for ls. Recursive listings. cd . http://www.theurbanpenguin.com

Linux Essentials . and ... home and ~.

Files and directories


Now we will shall delve a little into using directories and listing files. The first point to understand is that within Linux everything is a file; directories are just a special type of file. We also have files that point to devices such as USB ports and hard drives, these files are located within the /dev directory. Using the command file(/usr/bin/file) we can determine the file type for a given file or files. In the following example we use file to query /dev/sda1 /etc and /etc/hosts file/dev/sda1/etc/etc/hosts The response should be similar to the output shown in the following graphic showing the files to be a block device, directory and then a plain file.:

In Linux, hidden files are denoted by those whose file names begin with a dot. Using the -a option with ls(/bin/ls) we can list all files including hidden files. To become a little clever with our command and piping we could try this: lsa|grep'^\.'

Step by step 1. ls -a : list all files in the current directory 2. | : pipe or redirect the output of ls to the next command, in this case grep 3. grep : used to search text 4. '^\.' : ^ lines that start with, \. means literally a dot. The backslash ensure that grep reads the dot as a dot and removes any special meaning that the dot character may have.

http://www.theurbanpenguin.com

Linux Essentials

There is no place like $HOME


A users home directory will normally be /home/USERNAME. This is where they can store their own files, additionally personal login scripts can be store in the users home directory as is their bash history. Typically a user will be taken to their home directory upon log-on to a text terminal or remote SSH, (Secure Shell) session. The environment variable $HOME will list the path to a users home directory. We can print to the screen the contents of a variable using the echo command, echo is a shell built-in or a command that is part of bash itself. echo$HOME

Users may return to their home directory using the bash builtin-in cd with no arguments, alternatively using: cd ~ . The tilde (~) becomes more useful though when you want to reference another users home directory: cd~bob The above command would take you to Bobs home directory, assuming there was a user named bob. Another useful option with cd is the option: cd The option takes you to your previous directory so it becomes very easy to toggle between two directories just using cd - . This is maintained by the variable $OLDPWD. Looking at the following series of commands entered from the users home directory: cd/usr/share/doc cd echo$OLDPWD Step by step 1. From the users' home directory we change to the /usr/share/doc directory using the directories full path 2. We then change back to the previous directory using cd 3. On moving to a new directory the variable $OLDPWD is populated with the path to the directory that was left so we see the $OLDPWD now points to /usr/share/doc The commands are captured in the screen-shot that follows:

http://www.theurbanpenguin.com

Linux Essentials

The above screen-shot details how useful cd can be in making use of the variable $OLDPWD

Paths
When referencing files, (of any type) we can use full path or a path relative to where we are; the simplest form of a relative path is just the file-name itself which we can access when we are in the same directory as that file. The . by itself means this directory and .. the directory above or parent directory . This directory .. The parent directory to the current directory ../.. The directory two directories above the current directory. ../etc The etc directory is referenced in the parent directory of the current directory If our current directory is /home/user1, we can refer the file in /etc/hosts in two ways: ../../etc/hosts : a relative path /etc/hosts : a full path, the full path will access the file from any directory and will always start with a forward slash, / , indicationf that we traverse to the file starting at the root of the file-system.

Directory listing using ls


We have already seen that we can use the -a option with ls to display hidden file; you may also know that ls has many more options. Some of these options you may use all of the time others you may never need to use. Options can also be used together, -l provides for a long listing. If you require to see a long listing of all files we can use ls and the options in the following combinations. lsla lsla lsal lsal The long listing provides details of the permissions, ownership, size and last modified time for the given file. Using the -R option a recursive listing is provided, in other words the contents of subdirectories and their subdirectories are all listed. Common options for ls include: -R : recursive listing -l : long listing http://www.theurbanpenguin.com

Linux Essentials -a : include all files including hidden files -r : reverse sort the listing -n : numerically sort on the User ID and the Group ID -h : used with -l and prints the size conveniently in an appropriate unit of measurement KB, MB, GB, for the actual file size.

The supporting video : http://www.youtube.com/watch? feature=player_embedded&v=eW9N_H0vfyA

2.4 Creating, Moving and Deleting Files


Weight 2

Description Create, move and delete files and directories under the home directory. Key Knowledge Areas: Files and directories. Case sensitivity. Simple globbing and quoting. The following is a partial list of the used files, terms and utilities: mv, cp, rm, touch. mkdir, rmdir. If you are ready we can start to look at some of the extensive command line tools within the Linux arsenal. In particular we will concentrate on those that will allow us to manage files and directories; did you know that you can create a directory with mkdir(/bin/mkdir) :-) .

Files in Linux
To begin with, lets remind ourselves that, in Linux everything is a file and file names are case-sensitive. We can have files called test, Test and TEST all in the same directory as the names are not the same, they are in different case! Talking of directories: a directory is a special type of file, as is a symbolic link. We can use the command file (/usr/bin/file) to identify the type of file that we are looking at. We saw this in the previous objective, 2.3 of Linux Essentials. Additionally, ls (/bin/ls) can help with identifying the file type. Consider ls with the --color option enabled: lscolor=auto The above command is often turned on by default with ls by means of an alias. When you type the command ls, the alias is found in RAM before the command. Either way, the color option will display files, directories and links in different colors as can be seen with the following screen-shot.

http://www.theurbanpenguin.com

Linux Essentials

lsF The above option -F is a little redundant with the color option so perhaps is less used; however with this option enable directories are listed with a trailing /, executables with a trailing * , named pipes with a vertical bar etc. This can be seen the the following screen-shot but also remember that the color option is turned on by default making the -F option not so necessary:

The command, ls, does not stop here in identifying file types, a long listing using ls -l will identify the file type as well. There seems to be no end to the ways we can identify Linux files! The very first character before the permissions is an indicator of the file type: d l c b s p Indicates a regular file Indicated a directory Indicates a symbolic link Indicates a character device, a terminal Indicates a block device, a disk drive Indicates a socket, (network connection) Indicates a named pipe see not below on pipes

Note: Pipes are communication processes between applications, the simplest form of piping is with unnamed pipes. See Linux Essentials topic 3.2 for more details but briefly: cat file | less is an example of an unnamed pipe. The out put of cat is sent through to the input of less. A named pipe is similar but we have a file of type pipe that is used as the intermediary: 1. Make the pipe file with mkfifo mypipe 2. From one terminal type the following command from the same directory that the named pipe was created in: ls -l > mypipe 3. From another terminal making sure you are the same directory as the named pipe and then issue the command cat < mypipe There is know requirement to be in the same directory as the named pipe but if you are not just use the full path the the pipe file. Watch the demo on named pipes http://www.youtube.com/watch? feature=player_embedded&v=DFreHo3UCD0

The first character of each line in the output of ls -l identifies the file type as can be seen from the following screen-shot:

http://www.theurbanpenguin.com

Linux Essentials

mkdir
Commonly used in Linux is mkdir (/bin/mkdir), the command to create directories; however I often find people are surprised that the -m option is there and did not know you can create the directory and set the permissions in one. -v : provides positive feedback that the directory as created -m : Sets the permissions of the directory, don't forget the sticky bit that ensures users can delete only the files they own and the group ID bit when set on a directory can control file group ownership . See objective 5.3 of Linux Essentials for more details on Linux file permissions. -p : creates the parent directory if required -Z : set the SELinux context Watch the video man page for mkdir http://www.youtube.com/watch? feature=player_embedded&v=xxkEeu_gdxY

Quoting
If we are creating a directory or a regular file we may need to quote the file name. For example is we have a space in the name Linux will create two directories: mkdirnewdir The above command create two directories one name new and one named dir . We have three types a quoting mechanisms that we could use the help in this and other situations: double quotes single quotes and the backslash mkdirnewdir mkdir'newdir' mkdirnew\dir They do have slightly different purposes and they are described below: http://www.theurbanpenguin.com

In that way these three code snippets would resolve the situation:

Linux Essentials The double quote The double quotes will protect most character from any shell special meaning. So in the example the quotes protect the space from being the argument separator. The single quotes The single quotes work in much the same way as the double quote except that single quotes protect all characters from the shell. This means that should we want to include a variable within the quotes the contents of the variable would not be expanded and the directory name would be named after the variable name. Compare the following: mkdir$USERdir mkdir'$USERdir' With double quotes the directory created would be andrew dir . With single quotes the directory created would be $USER dir . Backslash Finally we have the backslash this escapes the character immediately following the slash. Only that single character is protected. So as before with a variable in the directory name as well as the space we could use the following code the create the andrew dir. mkdir$USER\dir

rmdir
If mkdir makes a directory then its antithesis is rmdir (/bin/rmdir) which can remove a directory. However it can only remove empty directories. In you need to delete a directory and its content then you may use the command rm (/bin/rm): rmrf/home/user1/test The above command we delete the test directory and its content from user1's home directory.

ls -ld
If we want a long listing of a directory rather than the contents of that directory we can use the command: lsld/etc This will provide a long listing of the directory /etc rather than a long listing of all of the files within /etc as seen in the following screen-shot:

http://www.theurbanpenguin.com

Linux Essentials

Globbing
Globbing is a glorious word, isn't? Just take the time to say it a few times. When you are ready we will explain what it does. Ok.... ? Globbing provides a techniques to collectively groups files by using elements in their names as the criteria. Using different globbing techniques we can display different collections of files : ls *.py : show files with the extension py ls *.[!p] : show files where the extension does not start with a p ls ??.* : show files who main name starts with two characters followed by a dot and then any characters The main characters * and ?, their meaning is expanded below: * : refers to zero or more instances of any characters ? : refers to exactly a single character

cp
To copy files we can use the command cp(/bin/cp). In copying a file the original file remains intact and new copy is made in the target directory. Using the -R option we can recursively copy which is including files from subdirectories. Using the -i option, for interactive, we will be prompted before files are copied. Other options include: -a : is probably well known as the archive option, maintaining ownership of the files, but did you know it also turned on the recursion option so that sub-directories are included -b : If a target file is overwritten then back that file up and append a ~ to the end of the name -n : no-clobber, do not overwrite target file -u : update the target file if source file is newer -s : create symbolic links as targets The cp video man page: http://www.youtube.com/watch? feature=player_embedded&v=JxrhWk9xQGw

mv
Using mv(/bin/mv) we can move or rename files. The following command renames the files as the file has not been placed in another directory. The file is renamed from file1 to the new name file1.txt. mvfile1file1.txt If we use the following command then the file is moved from /data to the directory /salesdata, as there is no new base name to the file we can eave that blank in the second argument and specify only the directory name and path to move the file to: mv/data/file/salesdata/

http://www.theurbanpenguin.com

Linux Essentials If the move is on the same hard drive then no new data file is created. A move on the same disk partition amounts only to a change in the file's meta-data and does not create a new file.

touch
We can use the command touch(/usr/bin/touch) to create empty files, but using on an exiting file we can change the date stamps with touch -a or touch -m.

rm
We look at rm(/bin/rm). This is used to delete files and we have already seen that we can delete a directory and its content with rm -rf used against the target directory. Take great care when logged in as root, the Linux super-user, rm -rf can delete all files on your system. Watch the video for this objective: http://www.youtube.com/watch? feature=player_embedded&v=hobvwsqlwis

http://www.theurbanpenguin.com

Вам также может понравиться