Certification Objective 1.03: Basic Commands

 < Day Day Up > 

Linux was developed as a clone of Unix, which means that it has the same functionality with different source code. And the essence of both operating systems is at the command line. Basic commands for file manipulation and filters are available to help you do more with a file.

Basic File Operations

Two basic groups of commands are used to manage Linux files. One group helps you get around Linux files and directories. The other group actually does something creative with the files. Remember, in any Linux file operation, you can take advantage of the HISTORY (this is capitalized because it's a standard environment variable) of previous commands, as well as the characteristics of command completion, which allow you to use the TAB key almost as a wildcard to complete a command or a filename, or give you the options available in terms of the absolute path.

Almost all Linux commands include switches, options that allow you to do more. Few are covered in this chapter. If you're less familiar with any of these commands, use their man pages. Study the switches. Try them out! Only with practice, practice, and more practice can you really understand the power behind some of these commands.

Basic Navigation

Everything in Linux can be reduced to a file. Directories are special types of files that serve as containers for other files. Drivers are files. As discussed earlier, devices are special types of files. The nodes associated with USB hardware are just files. And so on. To navigate around these files, you need some basic commands to tell you where you are, what is there with you, and how to move around.

The Tilde (~)

But first, every Linux user has a home directory. You can use the tilde (~) to represent the home directory of any currently logged on user. For example, if your username is tb, your home directory is /home/tb. If you've logged in as the root user, your home directory is /root. Thus, the effect of the cd ~ command depends on your username. For example, if you've logged in as user mj, the cd ~ command brings you to the /home/mj directory. If you've logged in as the root user, this command brings you to the /root directory.


There are two path concepts you need to know when you work with Linux directories: absolute paths and relative paths. An absolute path describes the complete directory structure based on the top level directory, root (/). A relative path is based on the current directory, also known as the present working directory. Relative paths do not include the slash in front.

The difference between an absolute path and a relative one is important. Especially when you're creating a script, absolute paths are essential. Otherwise, scripts executed from other directories may lead to unintended consequences.


In many configurations, you may not know where you are relative to the root (/) directory. The pwd command, which is short for present working directory, can tell you, relative to root (/). Once you know where you are, you can know if you need to move to a different directory.


It's easy to change directories in Linux. Just use cd and cite the absolute path of the desired directory. If you use the relative path, just remember that your final destination depends on the present working directory.


The most basic of commands is to list the files in the current directory. But the Linux ls command, with the right switches, can be quite powerful. The right kind of ls can tell you everything about a file, such as creation date, last access date, and size. It can help you organize the listing of files in just about any desired order. Important variations on this command include ls -a to reveal hidden files, ls -l for long listings, and ls -i for inode numbers.

Looking for Files

There are two basic commands for file searches: find and locate.


The find command searches through directories and subdirectories for a desired file. For example, if you wanted to find the directory with the XF86Config GUI configuration file, you could use the following command, which would start the search in the root directory:

# find / -name XF86Config

But this search on my older laptop computer with a 200 MHz CPU took several minutes. Alternatively, if you know that this file is located in the /etc subdirectory tree, you could start in that directory with the following command:

# find /etc -name XF86Config 


If this is all too time-consuming, RHEL 3 includes a default database of all files and directories. Searches with the locate command are almost instantaneous. And locate searches don't require the full filename. The drawback is that the locate command database is normally updated only once each day, as documented in the /etc/cron.daily/slocate.cron script.

Exam Watch 

The first time I started Linux during the RHCE exam, I ran this script. I could then use the locate command to quickly find the files that I needed.

Getting into the Files

Now that you see how to find and get around different files, it's time to start reading, copying, and moving the files around. Most Linux configuration files are text files. Linux editors are text editors. Linux commands are designed to read text files. If in doubt, you can check the file types in the current directory with the file * command.


The most basic command for reading files is cat. The cat filename command scrolls the text within the filename file. It also works with multiple filenames; it concatenates the filenames that you might list as one continuous output to your screen.

less and more

Larger files demand a command that can help you scroll through the file text at your leisure. Linux has two of these commands: more and less. With the more filename command, you can scroll through the text of a file, from start to finish, one screen at a time. With the less filename command, you can scroll in both directions through the same text with the PAGE UP and PAGE DOWN keys. Both commands support vi-style searches.

head and tail

The head and tail commands are separate commands that work in essentially the same way. By default, the head filename command looks at the first 10 lines of a file; the tail filename command looks at the last 10 lines of a file. You can specify the number of lines shown with the -nxy switch. Just remember to avoid the space when specifying the number of lines; for example, the tail -n15 /etc/passwd command lists the last 15 lines of the /etc/passwd file.

Creating Files

A number of commands are used to create new files. Alternatively, you can let a text editor such as vi create a new file for you.


The cp (copy) command allows you to take the contents of one file and place a copy with the same or different name in the directory of your choice. For example, the cp file1 file2 command takes the contents of file1 and saves the contents in file2. One of the dangers of cp is that it can easily overwrite files in different directories, without prompting you to make sure that's what you really wanted to do.


While you can't rename a file in Linux, you can move it. The mv command essentially puts a different label on a file. For example, the mv file1 file2 command changes the name of file1 to file2. Unless you're moving the file to a different partition, everything about the file, including the inode number, remains the same.


You can create a linked file. As discussed earlier, linked files are common with device files such as /dev/modem and /dev/mouse. They're also useful to make sure that multiple users have a copy of the same file in their directories. Hard links include a copy of the file. As long as the hard link is made within the same partition, the inode numbers are identical. You could delete a hard-linked file in one directory, and it would still exist in the other directory. For example, the following command creates a hard link from the actual Samba configuration file to smb.conf in the local directory:

# ln smb.conf /etc/samba/smb.conf

On the other hand, a soft link serves as a redirect; when you open up a file created with a soft link, you're directed to the original file. If you delete the original file, the file is lost. While the soft link is still there, it has nowhere to go. The following command is an example of how you can create a soft link:

# ln -s smb.conf /etc/samba/smb.conf

File Filters

Linux is rich in commands that can help you filter the contents of a file. There are simple commands to help you search, check, or sort the contents of a file. And there are special files that contain others; these container files are known colloquially as 'tarballs,' which is an alternative to the Red Hat Package Manager.

On The Job 

Tarballs are a common way to distribute Linux packages. They are normally distributed in a compressed format, with a .tar.gz or .tgz file extension, consolidated as a package in a single file. In this respect, they are similar to Microsoft-style compressed zip files.


You can sort the contents of a file in a number of ways. By default, the sort command sorts the contents in alphabetical order depending on the first letter in each line. For example, the sort /etc/passwd command would sort all users (including those associated with specific services and such) by username.

grep and egrep

The grep command uses a search term to look through a file. It returns the full line that contains the search term. For example, grep ‘Michael Jang' /etc/passwd looks for the name of this author in the /etc/passwd file.

The egrep command is more forgiving; it allows you to use some unusual characters in your search, including +, ?, |, (, and ). While it's possible to set up grep to search for these characters with the help of the backslash, the command can be awkward.

On The Job 

The locate command is essentially a specialized version of the grep command, which uses the slocate command-based database of files on your Linux computer.


The wc command, short for word count, can return the number of lines, words, and characters in a file. The wc options are straightforward; for example, wc -w filename returns the number of words in that file.


The sed command, short for stream editor, allows you to search for and change specified words or even text streams in a file. For example, the following command changes the first instance of the word 'Windows' with 'Linux' in each line of the file opsys, and writes the result to the file newopsys:

# sed 's/Windows/Linux' opsys > newopsys

However, this may not be enough. If there's more than one instance of 'Windows' in a line in the opsys file, it does not change the second instance of that word. But you can fix this by adding a 'global' suffix:

# sed 's/Windows/Linux/g' opsys > newopsys


The awk command, named for its developers (Aho, Weinberger, and Kerrigan), is more of a database management command. It can identify lines with a key word, and read out the text from a specified column in that line. A common example is with the /etc/passwd file. For example, the following command will read out the username of every user with a 'Mike' in the comment column:

# awk '/Mike/ {print $1}' /etc/passwd 

Administrative Commands

You'll work with a number of administrative commands in this book. But every budding Linux administrator should be familiar with at least two basic administrative commands: ps and who.


It's important to know what's running on your Linux computer. The ps command has a number of critical switches. When trying to diagnose a problem, it's common to get the fullest possible list of running processes, then look for a specific program. For example, if the Mozilla Web browser were to suddenly crash, you'd want to kill any associated processes. The ps aux | grep mozilla command could then help you identify the process(es) that you need to kill.

who and w

If you want to know what users are currently logged into your system, use the who command or the w command. This can help you identify the usernames of those who are logged in, their terminal connections, their times of login, and the processes that they are running.

On The Job 

If you suspect that a username has been compromised, use the w command to check currently logged-on users. Look at the terminal. If the user is in the office but the terminal indicates a remote dial-in connection, be suspicious. The w command can also identify the current process being run by that user.


Sometimes you may not know the exact name of the file or the exact search term. That is when a wildcard is handy. The basic wildcards are shown in Table 1-4.

Table 1-4: Wildcards in the Shell




Any number of alphanumeric characters (or no characters at all). For example, the ls ab* command would return the following filenames, assuming they exist in the current directory: ab, abc, abcd.


One single alphanumeric character: For example, the ls ab? command would return the following filenames, assuming they exist in the current directory: abc, abd, abe.


A range of options. For example, the ls ab[123] command would return the following filenames, assuming they exist in the current directory: ab1, ab2, ab3. Alternatively, the ls ab[X-Z] command would return the following filenames, assuming they exist in the current directory: abX, abY, abZ.

On The Job 

Wildcards are sometimes known in the Linux world as globbing.

 < Day Day Up > 

RCHE Red Hat Certified Engineer Linux Study Guide[c] Exam (Rh302)
RCHE Red Hat Certified Engineer Linux Study Guide[c] Exam (Rh302)
ISBN: 71765654
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net