In this lesson the student will become familiar with a wide variety
of Linux commands. These commands will include a coverage of some
basic or common commands which include reading, commands, navigation
commands,
manipulating commands, and searching commands. The commands will
be introduced to the student with examples that provide excellent
insight.
Of course, this document doesn't cover all the options available for
these
commands. The use of the manual pages, either with man and/or
xman,
will provide a complete coverage of additional options.
Before looking at specific commands, one needs to understand exactly what is meant by the term 'command'. Users coming from the DOS environment are probably familiar with the concept of commands that encompass core features of the operating system, such as DIR, COPY, and ATTRIB. These commands provide the basis on which more complicated actions could be built and from which sophisticated batch files could be written.
The fact remains in the DOS world, as in the world of any operating systems, the number of available commands is limited and generally static. This means that users don't have the luxury to create new commands in the DOS environment.
In the Unix world, and by extension in Linux, the concept is different is much different. A command is actually any executable file. That is, a command consists of any file that is designed to be run, as opposed to files containing data or configuration information. This means that any executable file added to a system becomes a new command on the system.
The execution of a command is very simple. From the command prompt, simply type the name of the command:
Or, if the command is not on one of your defined paths, you type the complete path and name of the command, such as:$ command
$ /usr/bin/command
| Note | Whenever a user logs in, they have a default path defined. The default path is a list of directories separated by colons. If a command is typed without a path indicated, then all the directories in the default path are checked in the order they appear to find the file associated with the command. Later, the student will learn how to alter the default path to meet specific needs for ones work environment. |
What is the current path for the workstation you are using
?
Hint: Check the current path on your Linux workstation. This
is performed by typing the command "echo $PATH" at the
command
prompt and press the Enter key.
The number of Linux commands that are available in a common Linux distribution such as Red Hat 6.0 is quite large. This can be verified by typing the command "xman" at the command prompt. Once this command executes a small dialog box is opened. Click the mouse arrow on "Manual Pages". The next box that appears has two menu items at the top. Click and hold the mouse on the "Options" and without releasing the mouse drag the arrow to "Display Directory" and release. All of the available Linux commands will be displayed. To scroll down, click the left mouse button while having the mouse arrow over the scroll bar on the left. To scroll up, click the right mouse button while having the mouse arrow over the scroll bar on the left. To view the manual page for a particular command, simply click the mouse arrow on the command. Scrolling is conducted using the same process on the scroll bar to the left.
As you will probably agree, there are quite a few commands. In our study of Linux commands, we will be examining the most common commands needed for the Systems Administration tasks to be utilized in this course. These commands cover a range of tasks that include but not limited to, moving around your directories, identifying what is running on your system, and finding that file that is lost. The commands to be discussed are:
The su command is on of the most basic, and is useful in many different tasks. The su command is generally used to switch between different users.
Consider the following example: you are logged in as user1 and need to switch to user2 to perform some work and then switch to working as user1. You could log out as user1 and then log in as user2, do the work, log out again, and then log back in as user1. This seems a bit time consuming. Using the virtual console feature of Linux, you could log in as user1 in one virtual console and as user2 in another and switch back and forth. The problem with this approach is that you need to work with two different screens. A third option is to use the 'su' command. If you are logged in as user1 and need to become user2, you simply type
$ su user2and you are prompted for user2's password:
$su usersWhen finished, enter the exit command to return to user1:
password:
$exitPut together, a complete session should appear as:
[user1@linux.cxm user1] $ su user2A common us of the su command is when you need to become the root user, commonly referred to as the superuser, to perform adminstrative tasks. These tasks include creating and managing user accounts, performing network configuration, and configuring printers.
Password:
[user2@linux.cxm user1] $ some commands
[user2@linux.cxm user1] $ exit
exit
[user1@linux.cxm user1] $
If you issue the su command with no username, you are prompted for the root password and once this provided, you are switched to working as the root user:
[user1@linux.cxm user1] $ suIf you logged in as the root user, you can use su to become any user on the system without a password (hence the importance of keeping the root password safe from prying eyes). This is particularly useful for the system adminstrator who may need to become different users to debug problems but won't necessarily know other users' passwords. Note in the following example how using su to become user1 doesn't cause a prompt to display when the root user issues the command
Password:
[root@linux.cxm user1] $
[root@linux.cxm /root] $ su user1The su command offers many other powerful features often used in advanced system adminstration tasks. You can learn about these from su man page. To use the manual pages for this command or any other type man followed by the command
[user1@linux.cxm /root] $
[user1@linux.cxm user1]$ man commandBack to Top
The pwd command stands for 'present working directory'. It is the most basic of these three commands. By typing the command and pressing Enter, the user will be told which directory they are currently located in;
$ pwdIn this example, the pwd command returns /home/aross as its answer. This tells you that you are in the home directory of the user, aross (that's me).
/home/aross
The cd command does more than simply look at the current state of things: it actually changes the state. The cd command allows you to change your current directory to any accessible directory on the system.
For instance, consider the previous example where the current directory is /home/aross. Using the cd command, you could change to a subdirectory of the aross home directory called stuff:
$ cd My_DocumentsTyping the pwd command after you change directories confirms that you ended up where you wanted to be.
$ pwd
/home/aross/My_Documents
Similarly, you could change to the system's temporary directory, /etc, with the following command:
$ cd /etcFinally, the ls command can be used to view the contents of the current directory. For example, if you use, the ls command to view the contents of the aross home directory, the result looks like this:
$ pwd
/etc
Notice how the files and/or directory names are displayed in multiple columns and the width of the columns is determined by the width of the longest name.
In addition to listing the contents of the current directory, it is possible to list the contents of any accessible directory on the system. For example, to list the contents of the directory /usr, you enter the command ls /usr:
As mentioned above, notice how the number and width of columns in the listing depends on the length of the largest name being displayed.
Of course, you probably are wondering what use a list like this really is. There is no information to tell you which names indicate files and which names indicate subdirectories, or what size the files are.
This information can be determined by using an extension of the ls command: ls -l. Using this in my home directory of aross, ls -l produces these results:
Note the reference information reported for each file and directory. Starting from the left and scanning to the right, the information reported is identifying it as a file or directory, the permissions, the owner, the group, the size, the date of the last modification of the file or directory. For some entries the first letter that appears is a d. This letter identifies the item to be a directory. If a - appears the item is simply a file.
The more and less commands are closely related and provide similar functionality, and the great irony is that less provides more capabilities than more.
The basic purpose of both commands is to display long files or lists of text one screen or window at a time, allowing users to page down through the text and, in some cases, move back up through the text. Both commands also provide capabilities to search text being displayed.
This is useful in many instances, including when you want to look quickly at a long text file without having to open it in an editor like xedit and to view particularly long directory listings.
Let's starat with the more command. The more command is fairly basic, allowing ussers to move forward a line or a screen at a time through a large body of text as well as search that text.
For example, if you have a large text file called bigstuff, you could view it a page at a time with the following command:
$ more textfileAfter pressing Enter, you see the first screen of the file with the text -More- displayed on the last line of the screen. Pressing the space bar jumps forward a full screen length, while pressing the Enter key moves forward one line at a time. When you reach the end of the body of text, you are returned to the command prompt.
To search forward through the file, enter a slash (/) followed by the word or phrase you want to search for and then press Enter. The display jumps forward to the first occurrence of the word or phrase being searched for and displays the occureence near the top of the screen. You can repeat the same search by entering n after the first search, avoiding the need to type the same word or phrase repeatedly.
In addition to using more to view the contents of a file, you can pass along the results of another command to more using piping. For example, on my system, using ls -l to view the contents of the /tmp directory produces results that are several screens long. This is many times larger than my largest xterm window can display. To be able to view the results of this listing ls -l /tmp command a window at a time, I need to pass the results to more:
$ ls -l /tmp | moreThis command connects the ls -l command to the more command with a vertical pipe. The use of the pipe character is the source of the term "piping". The result of this piping is that more is used to display the results of the ls -l command, which means you can move down a screen or line at a time or search the results, just as you did earlier with the contents of a file.
|
Note: |
Complete directions for using the more command are found in the manual pages. Using the man page can be conducted by typing, man more. |
For all intents, the less command is a vastly improved version of the more command. In addition to the basic functions described previously (moving forward a screen or line at a time and searching), the following are some of the other actions that can be performed on a body of text:
It is easy to move backward through a file using less. The up arrow moves up one line of text at a time and the down arrow moves down one line at a time. You can use Ctrl+B to jump backward one screen at a time.
Finally, entering a slash (/) followed by a word or phrase and then pressing Enter will search forward through the text being displayred, and entering a question mark (?) followed by a word or phrase and then pressing Enter will search backward through the text.
As with the more command, you can look at the less man page for details on all the features of the command. Using the man pages is discussed later in this chapter in the section on the man command.
The find and grep commands are powerful tools for searching for files. This section only discusses their most basic uses because it would be easy to devote a full chapter to using these commands. If you want a full discussion of the commands, check their man pages.
While both commands are used for searching, their purposes differ: find is used to search for files by any number of criteria, including name or date of creation. Grep is used to search the contents of files.
If you've ever created a file but can't remember where you put it. The find command is the Unix answer to this situation. The command can be used to search for files by name, date of creation or modification, owner (usually the user who created the file), size of the file, and even the type of file. All of these capabilities will be explained here.
The basic structure of the find command is
$ find starting-directory parameters actionsThe starting directory specifies where to begin searching. For example, specifying /home means only subdirectories of /home will be searched while specifying / means everything will be searched.
The parameters are where you specify the criteria by which to search. In this case, you use the command -name filename to specify the file you are searching for.
The actions section indicates what action to take on found files. Generally, you will want to use the -print action, which indicates that the full name and path of the file should be displayed. Without this, the find command will perform the search indicated but will not display the results, which defeats the purpose.
Putting this together, if you want to search for all files named stuff on your system, you could use the command
$ find / -name foo -printNotice there are three occurrences of the file named foo located by the find command.
/tmp/foo
/home/aross/foo
/home/kross/foo
| Tip | Notice that you attempted to search the entire system in the
previous
command. To do this effectively, you first need to log in as the
root user so that you can access all the directories on the
system.
If you don't, your search will yield permission denied errors every
time
find tries to search a directory for which you don't have
permission.
This can be done with the following command line.
su -c "find / -name foo -print" When this command line is executed the user will be requested to provide the root password. Once the correct root password is provided the entire file system will be searched for all occurrences of the file foo. |
It is also possible to search for partial filenames. For example, if you know that the file you are looking for begins with fo, then you can use the expression fo* to indicate all files beginning with fo and ending with any combination:
$ find / -name 'fo*' -printNotice the use of the single quotation marks around fo*. When you use the * character, it is important to place the single quote marks around the entire expression. Otherwise, find will give you an error:
/tmp/foo
/var/lib/texmf/fonts
/usr/bin/font2c
/usr/bin/mh/folders
/usr/bin/mh/folder
/usr/bin/mh/forw
/usr/bin/formail
/usr/bin/fontexport
/usr/bin/fontimport
/usr/bin/fold
$ find / -name fo* -print -mountIf the results being produced by the find command are too numerous to fit in one screen, you can use piping and the more command just like you did earlier with the ls -l command:
find: paths must precede expression
Usage: find [path...] [expression]
$ find / -name 'fo*' -print | moreBack to Top
Grep is used to look inside the contents of one or more files in an attempt to find the occurrence of a specific pattern of text inside the files where the find command searches for files by its name, type, or date.
Consider the following situation. You created a text file that contains the word "radio" and stored it in your home directory. Unfortunately, you forgot the name of the file and want to quickly check which files contain "radio". This is where the grep command comes in handy.
For a first try, let's assume that we are in our home directory. The following command searches for the word "radio" in each file in your home directory and produces results as follows:
$ grep radio *Notice that one return was made for each occurrence of the word "radio" in a file. The name of the file is shown followed by a colon, which is followed by the complete text of the line where the word has appeared.
mystuff.txt:This is a very easy thing to see especially when you hear it on the radio.
hadeha.txt:On a radio station, she heard the statements of the player,
In general, the pattern for the grep command is
$ grep text-pattern file-listThe text pattern can be a simple word, as in the previous example, or a phrase or a more complicated regular expression. (The use of regular expressions -- a powerful method for searching for text patterns -- with grep can be found in the manual pages. Type man grep to research this command.)
Generally, though, you want to check either the contents of a single file, which takes the form
$ grep text-pattern file-nameor check the contents of all files in a directory using the command:
$ grep text-pattern *wher the * is an expression indicating that all files in the current directory should be searched.
In its simplest form, the text pattern is a single word or part of a word containign no spaces. If you want to search for a phrase, such as "thing to see", you need to enclose the text pattern in quotation marks, as in the following example:
$ grep "thing to see" *Just as it is sometimes useful to pipe the results of a command through the more or less commands, the same is true for grep. Consider the situation where you want a listing of all files in the current directory with the modification dates containing Dec 1. You can find this information by piping ls -la through a grep command:
mystuff.txt:This is a very easy thing to see especially when you hear it on the radio.
$ ls -la | grep "Dec 1"Notice the output generated a list of files having modification dates containing "Dec 1". This includes files with modification dates of Dec 1, and Dec 10 through Dec 19.
The whereis command is quickly find files, and it also shows you where the file's binary, source, and manual pages reside. For example, the following command shows that the find command is in the /usr/bin directory, and its man page is in the /usr/man/man1 directory:
$ whereis findYou can also use whereis to find only the binary version of the program with
find: /usr/bin/find /usr/bin/man/man1/find.1
$ whereis -b findIf whereis cannot find your request, you'll get an empty return string, for example:
find: /usr/bin/find
$ whereis fooPart of the problem could also be that the file is not in any of the directories the whereis command searches. The directories whereis looks in are hard-coded into the program. Although this may seem like a drawback, limiting searches to known directories such as /usr/man, /usr/bin, or /usr/sbin can speed up the task of finding files.
foo:
Although whereis is faster than using find to locate programs or manual pages, there's an even faster search utility you can use, called locate.
One way to speed up searches for files is not to search your file directories! You can do this by using a program like locate, which uses a single database of filenames and locations, and which saves time by looking in a single file instead of traversing your hard drive. Finding a file using locate is much faster than the find command because locate will go directly to the database file, find any matching filenames, and print its results.
Locate is easy to use. For example, to find all the PostScript files on your system, you can enter
$ locate *.psAlmost instantly, the files are printed to your screen. You may even find the locate command line a little easier to use than the find command. However, there is a catch: find will work "right out of the box", whereas with locate, you need to first build a database of all the files on your system. But don't worry, because the procedure is almost automatic.
After you install Linux, locate can't show any search results because it can't locate its database. To create this database, you'll need to use the updatedb command. Make sure you're logged in as the root operator or use the su command. At the prompt, enter
$ updatedbIt may take a minute or so for the updatedb command to finish its work, but when it's done, locate's database, locatedb (about 300,000 characters in size of for 400MB worth of files), will reside in the /var/lib directory. The only downside to using the locate command is that after time, its database could become out of date as you add or delete files to your system. However, you can have its database updated automatically.
As you begin exploring the Linux system, you may come across programs whose functions are not clear. Many Linux programs are designed to give at least a little hlep with a ? or - help option on the command line, but you generally shouldn't run a program without knowing what it does.
The whatis command may be able to help you quickly find out what a program is with a summary line derived from the program's manual page. For example, to find out what is whereis (not whereis whatis!), you can enter
$ whatis whereisThe makewhatis command, like the updatedb command, will take a few minutes to build the whatis database, which, unlike locate's, is called whatis and is found under the /usr/man/man1 directory. The makewhatis command has several options, but it does not have a manual page. To see a quick summary, use
whereis (1) - locate the binary, source, and manual page files for a command
$ makewhatis -?As with the locate command database, you'll need to periodically update the whatis database to keep track of any newly installed programs.
So far, you've seen how whereis and whatis can help you find programs or figure out what they do. But what if you want to do something and can't remember which program does what? In this case, you can turn to the apropos command.
For example, if you can't remember which command searches for files, you can enter
$ apropos searchYou'll see a list of programs from the whatis database on your screen. The apropos command uses this database to search for the keyword you entered. If you keep your manual pages and whatis database up-to-date, you'll be able to use apropos to help you find the program you need.
If you just can't get the hang of using the ls command to list directories, you can use the dir and vdir commands. These commands have only about 45 command-line options, compared to ls's over 75, but they're just as capable. The work like ls, but with certain defaults.
The dir command works like the default ls command, listing the files in sorted columns, for example:
$ dirThe vdir command works like the ls -l option, and presents a long format listing by default, for example:
$ vdirAlthough you won't find separate manual pages for dir or vdir (they're mentioned in the ls man page), you can get help with each command by using the -- help option.
You now know how to list the contents of your directories, but you may also be interested in the directory structure of your system, or the directory structure of a particular tree of your system (such as /usr/X11R6). For example, ls -R will recursively print out your directories, but how are these directories related to each other? If you would like a more direct, graphical view of your directories, a directory listing utility can help.
Steve Baker's tree utility will print a graphic view of any desired structure, and it has several handy features. Firs tree's syntax, or command-line, options are similar to several of those for the ls command. Wildcards or expressions are supported. The tree command also supports color in its listings, lik ls. Finally, tree has a -x option similar to the find command's -xdev option, so you don't have to get a directory picture of operating systems if you choose to start your listing with the root or / directory.
The tree command is easy to use. For example, if you would like to see the contents of the /home/aross/Desktop directory, along with all files, try the following:
$ treeThe tree command also has a handy -d option to list only directories, and not files (unlike ls). This is one of the best ways to get an idea of what your Linux file system looks like from a directory standpoint. You can also use it to view the directory structure of installed software. For example, to see what is on your system after installing the Amaya Web browser, try
$ tree -d /usr/local/AmayaIf you're interested in this utility (or other file utilities), you'll find an rpm versions of these utilities containing its source, manual page, and ready-to-run binary at the RedHat ftp site.
ftp://ftp.redhat.com/pub/redhat/redhat-6.1/i386/RedHat/RPMS/
The cat (concatenate file) command is used to send the contents of files to your screen. This command may also be sued to send files' contents into other files.
Although cat may be useful for reading short files, it is usually used to either combine, create, overwrite, or append files. To use cat to look at a short file, you can enter
$ cat test.txtThe cat command also has a number of options. If you'd like to see your file with line numbers, perhaps to note a specific phrase, you can use the -n option:
This text file was created by the cat command.
Cat could be the world's simplest text editor.
If you thoroughly study this quarter, you'll learn how to use cat.
This is the last line of text in this file.
$ cat -n test.txtYou can also use cat to look at several files at once, because cat accepts wildcards, for example:
1 This text file was created by the cat command.
2 Cat could be the world's simplest text editor.
3 If you thoroughly study this quarter, you'll learn how to use cat.
4 This is the last line of text in this file.
$ cat -n test*As you can see, cat has also included a second file in its output, and has numbered each line of the output, not each file. Note that you could also see both files with
1 This text file was created by the cat command.
2 Cat could be the world's simplest text editor.
3 If you thoroughly study this quarter, you'll learn how to use cat.
4 This is the last line of text in this file.
5 This is the first line of test2.txt.
6 This file was also create by cat.
7 This is the last line of test2.txt.
$ cat test.txt test2.txtThe output will be exactly the same as if you'd used a wildcard. But looking at several files is only one way to use cat. You can also use the cat command with the redirection operator > to combine files. For example, if you would like to combine test.txt and test2.txt into a third file called test3.txt, you can use
$ cat test* > test3.txtThis will create a file test3.txt which will contain the contents of test.txt and test2.txt. If you want to combine test.txt and test2.txt without creating a third larger file, you'd first decide whether you want the contents of test.txt to go into test2.txt or the contents of test2.txt to go into test.txt. Once you've decided, using cat with the >> redirection operator, you might type
$ cat test.txt >> test2.txtThis appends the contents of test.txt to the end of the test2.txt. To check the results, use cat again:
$ cat test2.txtNote that if you had entered the command
This is the first line of test2.txt.
This file was also created by cat.
This is the last line of test2.txt.
This text file was created by the cat command.
Cat could be the world's simplest text editor.
If you thoroughly study this quarter, you'll learn how to use cat.
This is the last line of text in this file.
$ cat -n test.txt >> test2.txtThe test2.txt file would look like
$ cat test2.txtFinally, here's a trick you can use if you want to create a short text file without running a word processor or text editor. Because the cat command can read the standard input you can make the cat command create a file and fill it with your keystrokes. Here's how:
This is the first line of test2.txt.
This file was also created by cat.
This is the last line of test2.txt.
1 This text file was created by the cat command.
2 Cat could be the world's simplest text editor.
3 If you thoroughly study this quarter, you'll learn how to use cat.
4 This is the last line of text in this file.
$ cat > myfile.txtYou should also know that the cat command will print out the contents of any file, and not just text files. Although cat my be useful to look at one or several short files, you'll need to use the more and less commands discussed earlier to view files larger than the screen size.Now, enter some text:
$ cat > myfile.txt
This is the cat word processor.
This is the end of the file.Then, when you're done typing, press Ctrl+D to close the file. To see if this works, try
$ ls -l myfile.txt
-rw-rw-r-- 1 aross users 61 Mar 25 22:06 myfile.txt$ cat myfile.txt
This is the cat word processor.
This is the end of the file.
Although the head and tail commands are not pagers as the more and less commands, they can make life a lot easier when all you want to do is read the beginning or end of a file. These programs, like most Linux commands, are designed to do oe or two things, but they do these tasks well.
The head command has a number of options besides the traditional -n, which prints the first n lines of a file. You'll find that the head command in your Linux distribution, which is part of the GNU text utilities, will also print any number of 512-character, 1 -kilobyte (1024 bytes), or megabyte-sized blocks from the beginning of a file. Like the cat command, head can also handle binary files.
If you use head in the traditional way, you strip off lines from the beginning of one or several files. For example, if you'd like to do a quick check of the formatting of all the manual pages for programs with names beginning with "wait" under the /usr/man/man2 directory, you can use the following command:
$ head -5 /usr/man/man2/wait*.2The tail command is especially useful when you're faced with the task of reading through large files where the most useful information is at the end of the file. One example task is if you want to look at the system messages for errors. One message file, located in /var/log, contains details of system operations, but the log is updated at the end of the message file (in other words, text is appended, so the most recent messages are at the end of the file). To look at the last 12 lines in the message file using tail, make sure you're logged in as root, and type
![]()
Note that the default output from the head command is to include the filename. If you'd prefer just to have the information, use the -q option, for example:
$ head -5 -q /usr/man/man2/wait*.2
$ tail -12 /var/log/messagesBeing able to read large files in this way is convenient, considering that the system messages can grow to more than a million characters.
The touch command is easy to use, and generally, there are two reasons to use it. The first reason is to create a file, and the second is to update a file's modification date. The touch command is part of the GNU file utilities package, and has several options.
To create a file with touch, use
$ touch newfileAs you can see, touch created a file with a length, or size, of zero. You can also use
$ ls -l newfile
-rw-rw-r-- 1 aross users 0 Mar 25 20:03 newfile
$ > newfile2Like touch, this creates a file with a length of zero. So why use touch, if you can do this at the command line? Because touch will update a file's date or time. You can even use touch to change a file's date or time to the past or the future, for example:
$ ls -l new*
-rw-rw-r-- 1 aross users 0 Mar 25 20:03 newfile
-rw-rw-r-- 1 aross users 0 Mar 25 20:05 newfile2
$ touch newfile2As you can see, the file newfile2 now has timestamp 10 minutes younger. You can also set the time and date of a file to an arbitrary date, for example:
$ ls -l newfile2
-rw-rw-r-- 1 aross users 0 Mar 25 20:13 newfile2
$ touch -t 1225110099 newfile2Using the --full-time option and long format listing of the ls command shows that the file newfile2 now has a timestamp of 11:00am, Christmas Day, 1999 (which appears to be a Saturday).
$ ls -l --full-time new*
-rw-rw-r-- 1 aross users 0 Sat Mar 25 20:03 2000 newfile
-rw-rw-r-- 1 aross users 0 Sat Dec 25 11:00 1999 newfile2
One use for touch is during backup operations. Either before or after backing up a series of files or directories, you can use touch to update the timestamps of your files so that the backup program has a reference time for the next backup session. Another use for touch is to control deletion or retention of log files during the next automated file cleanup by scheduled programs managed by cron. If you make a log file old enough, it will be deleted. If you update it, the file will be retained.
The rm command deletes files. This command has several simple options, but should be used cautiously. Why? Because when rm deletes a file, it is gone (you may be able to recover portions of text files, though; see the mc command).
Always running Linux while logged in as the root operator and using the rm command has caused many untold tales of woe and grief. Why? Because with one simple command you can wipe out not only your Linux system, but also any mounted filesystems, including DOS partitions, flash RAM cards, or removable hard drives, as follows:
$ rm -fr /*This command removes all files and directories recursively (with the -r option), starting at the root or / directory. If you must run Linux as root, make sure to back up your system, and implement a sound archiving plan.
The rm command will delete one or several files from the command line. You can use any of the following:
$ rm fileOne of the safer ways to use rm is through the -i or interactive option, where you'll be asked if you want to delete the file, for example:
$ rm file1 file2 file3
$ rm file*
$ rm -i new*You can also force file deletion by using the -f option, as in
rm: remove 'newfile'? y
rm: remove 'newfile2'? y
$ rm -f new*However, if rm finds a directory, even if it is empty, it will not delete the directory, and complains, even if you use -f, as in the following:
$ rm -f temp*When you combine -f and -r, the recursive option, you can delete directories and all files or directories found within (if you own them), as in the following example:
rm: temp: is a directory
rm: temp2: is a directory
$ rm -fr temp*The -fr option also makes rm act like the rmdir command (which will be discussed later in this document). Use this option with caution!
Some X Window managers, such as CDE, or utilities, such as TkDesk, offer "trash can" approaches to deleting files, but the files are not really deleted, ust moved to a temporary directory. This is a safe, but not fail-safe, approach to deleting or recovering files. You may also be able to use the mc command, or Midnight Commander.
The mkdir command can create one or several directories with a single command line. You may also be surprised to know that mkdir can also create a whole heirarchy of directories, which includes parent and children, with a single command line.
This command is one of the basic tools (along with cp and mv) you'll use to organize your information. Now, take a look at some examples. The following simple command line creates a single directory:
$ mkdir tempBut you can also create multiple directories with
$ mkdir temp2 temp3 temp4You'd think that you could also type the following to make a directory named child under temp:
$ makd temp/childAnd you can, because the temp directory exists (you just created it). But, suppose you type
$ mkdir temp5/childAs you can see, mkdir complained because the temp5 directory did not exist. To build a hierarchy of directories with mkdir, you must use the -p, or parent option, for example:
mkdir: cnannot make directory 'temp5/child': No such file or directory
$ mkdir -p temp5/parent/childAs you can see, mkdir created not only the temp5 directory, but also a subdirectory called parent, and a subdirectory under parent called child.
$ tree temp5
temp5
'--parent
'--child2 directories, 0 files
The rmdir directory is used to remove directories. To remove a directory, all you have to do is type
$ rmdir tempdirectoryBut there's a catch: the directory must be empty first! If you try to delete a directory with any files, you'll get an error message like this:
$ rmdir temp5In this example, temp5 also contains other directories. The rmdir command would also complain if a directory contains only files and not directories. You can use the rm command to remove the files first (remember to be careful if you use the -fr option), or you can move the files somehwere else, or rename the directory, with the mv command, discussed next.
rmdir: temp5: Directory not empty
The rmdir command, like mkdir, also has a -p, or parent, option. You can use this option to remove directory hierarchies, for example:
$ rmdir -p temp5Hmm. That didn't work! How about
rmdir: temp5: Directory not empty
$ rmdir -p temp5/parentHey! That didn't work either. Now try
rmdir: temp5/parent: Directory not empty
$ rmdir -p temp5/*This is getting frustrating! Try it one more time:
rmdir: temp5/parent: Directory not empty
$ rmdir -p temp5/parent/childFinally! As you can see, you must specify the complete directory tree to delete it. If you use the same command line, but without the -p option, only the child directory would be deleted. But what if there are two or more subdirectories, for example:
$ mkdir -p temp5/parent/childIn order to delete the entire directory system of temp5, you'd need to use
$ mkdir temp5/parent/child2
$ tree temp5
temp5
'-- parent
'-- child
'-- child23 directories, 0 files
$ rmdir temp5/parent/*So far, you've seen how to create and remove directories. Next, you'll learn about the mv command, which you can use to move or rename files and directories.
The mv command, called a rename command but known to many as a move command, will indeed rename files or directories, but it will also move them around your file system.
Actually, in the technical sense, the files or directories are not
really
moved. If you insist on knowing all the gory details, read the
"Linux
System Administrator's Guide", available at the Linux
Documentation Project.
In its simplest form, mv can rename files, for example:
$ touch file1This command renames file1 and file2. However, besides renaming files, mv can rename directories, whether empty or not, for example:
$ mv file1 file2
$ mkdir -p tem/temp2/temp3Although mv has nine different options, this section concentrates on the two most commonly used. These options, -b and -i, allow you to use mv in a fairly safe way, because mv will not only rename, but overwrite silently and quickly! The first option, -b, creates a backup of any file or directory you rename to an existing name, for example:
$ mv tem newtemp
$ touch file1 file2 file3As you can see, without using -b, mv not only renamed file1 to file2, but deleted file2 in the process. Is this dangerous? You bet! Now, try the -b option:
$ ls file*
file1 file2 file3
$ mv file1 file2
$ ls file*
file1 file2
$ touch file1This example shows that although file1 has been renamed, replacing file2, a backup of file2 with a default extension of the tilde (~) has been created.
$ ls file*
file1 file2 file3
$ mv -b file1 file2
$ ls file*
file2 file2~ file3
The mv command can work silently, or as with rm, you can use the -i (interactive) option, for example:
$ mv -i file2 file3Here, the mv command asks if you want to overwrite the file (but will keep quiet if no ovewriting takes, place, even if you us -i). You can also combine the -b and -i options with
mv: replace 'file3'? y
$ mv -bi file2 file3Now that you've seen how to delete, rename, or move your files, how do you copy files?
The cp, or copy, command is used to copy files or directories. This command has nearly 40 command-line options. They won't all be covered here, but you'll learn about some of the most commonly used options, which will save you time and trouble.
You'll most likely first use cp in its simplest form, for example:
$ cp file1 file2This creates file2, and unlike mv, leaves file1 in place. But you must be careful when using cp, because you can copy a file onto a second file, effectively replacing it! In this regard, cp can act just like mv. To show you how this can happen, try creating three files, each with a line of text, using the cat command:
$ cat > file1Now, copy a file onto another file, then check the file sizes and the contents of the new file:
this is file1
$ cat > file2
this is file2
$ cat > file4
this is the third file
$ ls -l
-rw-rw-r-- 1 aross users 14 Mar 25 21:45 file1
-rw-rw-r-- 1 aross users 14 Mar 25 21:46 file2
-rw-rw-r-- 1 aross users 23 Mar 25 21:47 file3
$ cp file1 file2IT should be obvious that file1 has replaced file2. To avoid this problem (unless you really want to overwrite the file), you can use the -b or -i options, which work just like mv. Here's an example:
$ ls -l file*
-rw-rw-r-- 1 aross users 14 Mar 25 21:45 file1
-rw-rw-r-- 1 aross users 14 Mar 25 21:51 file2
-rw-rw-r-- 1 aross users 23 Mar 25 21:47 file3
$ cat file2
this is file1
$ cp -i file1 file2Note that file2, which was overwritten, was backed up. The cp command may also be used to copy a numbe of files at one time. The following example shows how to copy all of the files in directory tempdir1 to directory tempdir2:
cp: overwrite 'file2'? n
$ cp -bi file1 file2
cp: overwrite 'file2'? y
$ ls file*
file1 file2 file2~ file3
$ cp tempdir1/* tempdir2Like the rm command, cp also has a -r, or recursive, option. You can use this option to copy one directory into another. For example, to copy the tempdir1 directory and its files into tempdir2, use this syntax:
$ tree tempdir2
tempdir2
'-- temp1file1
'-- temp1file2
'-- temp1file3
0 directories, 3 files
$ cp -r tempdir1 tempdir2Finally, the cp command has the -p option, which is similar to mkdir's -p option. Normally, when you copy a file inside several directories into another directory, only the file is copied. The following example will ony copy temp1file1 into tempdir3:
$ tree tempdir2
tempdir2
'-- temp1file1
'-- temp1file2
'-- temp1file3
'-- tempdir1
'-- temp1file1
'-- temp1file2
'-- temp1file31 directory, 6 files
$ tree tempdir2However, what if you'd like a file, along with its directory structure, copied? To do this, you can use the -P option:
tempdir2
'-- temp1file1
'-- temp1file2
'-- temp1file3
'-- tempdir1
'-- temp1file1
'-- temp1file2
'-- temp1file31 directory, 6 files
$ cp tempdir2/tempdir1/temp1file1 tempdir3
$ cp -P tempdir2/tempdir1/temp1file1 tempdir3Not only has cp copied a single file, but it has also created the subdirectory structure.
$ tree tempdir3
tempdir3
'-- tempdir2
'-- tempdir1
'-- temp1file12 directories, 1 file
Linux supports both hard and symbolic links. Although it is not important that you understand how links work in Linux, you should understand the difference between these two types of links and how to use links while you use Linux. To create hard or symbolic links, you use the ln, or link, command.
The ln command creates both types of links. If you use the ln command to create a hard link, you specify a second file on the command line you can use to reference the original file, for example:
$ cat > file1You can see that file2 is exactly the same as file1. If you delete file1, file2 will remain. If you make changes to file1, such as adding text, these changes will appear in file2, and if you make changes to file2, file1 will also be updated. You should also know that although you can see two files, each 14 characters in size, only 14 characters of hard dirve space are used.
This is file1
$ ln file1 file2
$ ls -l file*
-rw-rw-r-- 2 aross users 14 Mar 25 22:12 file1
-rw-rw-r-- 2 aross users 14 Mar 25 22:12 file2
$ cat file2
This is file1
On the other hand, although a symbolic link can be useful, it also has a drawback. The next examples show you why. First, to create a symbolic link, use the ln command -s option:
$ ln -s file1 file2Note the arrow pointing from file2 to file1. This tells you that file2 is a symbolic link to file1. Also note that file2 is shorter than file1. Symbolic links are different from hard links in that a symbolic link is merely a pathname, or alias, to the original file. Nothing happens to the original file if you delete the symbolic link. However, if you delete the original file, your symbolic link won't help you at all:
$ ls -l file*
-rw-rw-r-- 1 aross users 14 Mar 25 22:12 file1
lrwxrwxrwx 1 aross users 5 Mar 25 22:26 file2 -> file1
$ rm -f file1Because the original file, file1, no longer exists, you can't access its contents through the symbolic link, file2. However, symbolic links do have an advantage over hard links. You can use a symbolic link to point to a directory on your file system. In the following example, if you try to create a hard link to the /usr/local/games directory, the ln command will complain and quit:
cat file2
cat: file2: No such file or directory
$ ln /usr/local/games playBut you can use a symbolic link with
ln: /usr/local/games: hard link not allowed for directory
$ ln -s /usr/local/games playNow, instead of typing a long command like
$ ls -l play
lrwxrwxrwx 1 aross users 16 Mar 25 22:36 play -> /usr/local/games
$ cd /usr/local/gamesyou can use
$ cd playBack to Top
Users of other operating systems such as Windows 95 and MacOS, are familiar with the concept of compressed archives. A compressed archive is a single file that contains one or more files in a compressed form.
Compressed archives are often used to distribute software on the Internet. This is true of the ZIP files commonly found everywhere on the Internet. While Linux provides the unzip command to access the contents of ZIP archives, in the world of Unix, tar archives are generally used to distribute software. These archives are then compressed using the gzip compression program, which compresses individual files.
The tar program was originally used to create system backups on tapes (or tape archives, hence "tar"). In its current form, it is widely used for creating archives of files for distribution.
Creating a tar archive is as easy as the following command line:
$ tar cvf tar-file-name file-listsThis command creates a new archive specified by the filename tar-file-name (generally, tar files have a .tar extension which must be part of the tar-file-name) and then store all files from the file list in this archive. It is important to remember that this process copies the files into the archive so there is no danger that the original files will be deleted in the process.
Notice that the tar command is immediately followed by a series of options, in this case cvf. Each of these options is used to control different aspects of the behavior of the tar command:
$ tar cvf text.tar *.txtAgain, notice how all of the filenames are listed as they are copied to the archive.
mystuff.txt
hadeha.txt
Now consider the case where you would want to copy the entire contents of a directory into an archive. Fortunately, the tar command copies all files and subdirectories in a directory into an archive if the directory is part of the file list. So if you have a directory called Desktop and you want the entire contents of that directory to be copied to new archive called Desktop.tar, you can accomplish this with the following command:
$ tar cvf Desktop.tar Desktopand get the following results:
Notice the first line of the tar command output indicates the creation of the Desktop directory in the archive and then the copying of the files in that directory into the archive.
If you have an existing archive file, you will generally want to either view a listing of the contents of the archive file or extract the contents of the archive file.
Let's view the contents of the archive file, that was just
created.
To accomplish this replace the c option with a t. This gives us
the
following command line:
The listing of files is the long form which is achieved when listing the contents of a directory with the ls -l command.
To extract the contents of an archive into the current directory, replace the c or t with an x:
Back to Top
While the tar command is useful for archiving files, it doesn't perform any compression. Compression in Linux is generally achieved with the gzip command. Unlike the Windows zip archives, which consist of one or several files compressed into a single compressed archive, gzip simply compresses individual files without compressing them into an archive. Let's investigate this command for the file, entitled, ATIstuff:
$ gzip ATIstuffThis command compresses the file and adds a .gz extension to the end of the filename, changing the name to ATIstuff.gz. To understand the results of this command, a long listing is generated using ls -l before and after the gzip command is conducted.
As with many other Linux commands, you can use any valid shell file expression to list more than one file. For example,
Notice that the file size went from 1115 bytes before the compression to 694 bytes after the compression.
$ gzip *compresses all files in the current directory (but not those in subdirectories).
Uncompressing gzip files can be accomplished with the gzip command if you use the -d options:
$ gzip -d ATIstuff.gzThis will uncompress the file and remove the .gz extension, returning the file to its original uncompressed state with the name ATIstuff.
Using the gunzip command eliminates the need to use the -d option:
$ gunzip ATIstuff.gzThe gzip and tar commands may be combined as was necessary with the earlier version of the tar command because it didn't compress archives. The use of the gzip command had to follow the creation of an archive. Refer to the following example:
$ tar cvf text.tar *.txtTo access the contents of this archive, one must uncompress the archive first then use tar:
mystuff.txt
hadeha.txt
$ gzip text.tar
This creates a compressed archive file called, text.tar.gz.
$ gunzip text.tar.gzThe more recent version of the tar command, which includes those shipped with all the current distributions of Linux, provide a method for directly accessing and creating gzip compresssed tar archives. This is accomplished by simply adding a z option to any of the tar commands discussed earlier. When this is performed a second command is not necessary.
$ tar tvf text.tar
-rw-r-r- root/root 48 2000-01-05 11:15 mystuff.txt
-rw-r-r- root/root 6 2000-01-05 11:15 hadeha.txt
$ tar czvf Desktop.tar.gz Desktop
The result of this command is a compressed version of Desktop.tar archive,$ tar tzvf text.tar.gz
-rw-r-r- root/root 48 2000-01-05 11:15 mystuff.txt
-rw-r-r- root/root 6 2000-01-05 11:15 hadeha.txtdisplays the contents of the compressed text.tar.gz arhive, and
$ tar xzvf text.tar.gz
mystuff.txt
hadeha.txtextracts the contents of the archive.
| Note | Better details on the tar and gzip commands and the options that can be used with them are found in the man pages. |
If you work on a Linux box for any amount of time, the need to research a command has occurred. The research of a command can be found in the manual pages by using the man or xman commands. Let's consider using the man command first. This is the most basic form and can information on almost any Linux command can be found using:
$ man command-nameThis displays the manual page for the specified command and allows you to scroll through it and search in a fashion similar to the less command to display text.
If a specified man page cannot be found, you get the following error:
$ man non-existent-man-pageIf you are familar with X Windows, you have an alternative to using the man command to view man pages by using the xman program. To launch xman, use the following command in xterm:
No manual entry for non-existent-man-page
$ xman &or
$ /usr/bin/X11/xman &if your $PATH doesn't include /usr/bin/X11. When this command is executed a box will appear as follows:
From here, you can press the Manual Page button to display the main xman window shown below:
The main area of the window is used to display the text of the currently selected man page. Initially xman's help file is displayed. The window has two menus: Options and Sections.
The Options Menu, shown above allows you to switch between listings of man pages (Display Directory) and the contents of the current man page (Display Manual Page), in addition to searching the contents of the current man page. The Help option displays the complete help file for xman, which provides detailed instructions on using xman.
You are more interested in the Sections Menu, since this is where you begin the process of finding the man page you want to read. As you can see the manual pages are divided into eight main categories, including user commands, and system calls. Choosing one of these categories brings up a directory listing for the section like the one displayed below. When you find a command you wish to view information on, just click the mouse arrow on the command name and the man page for that command will appear.