2.1 Basic Operations on Files

   

The most common operations on files are creating new files, deleting unnecessary files, listing file names , and displaying the contents of a file. After login, you can perform all of these operations in your home directory. Let us see how we do it.

Creating a File

A file is a named area on the disk(s) where you can store information. The cat command is a basic command used for creating new files containing text. For example, if you want to create a file with the name newfile , containing three lines, the process is as follows :

 $  cat > newfile  This is first line. <  ENTER>  This is the second line. <  ENTER>  This is third and last line. <  ENTER>   <CTRL-d>  $ 

Note that you press the graphics/enter.gif key at the end of each line. When you have finished entering the text, you press graphics/ctrld.gif (pressing the Control and d keys simultaneously ) to end the text entry process and save the file.

Please note that use of the cat command for creating a new file is not very common but it is the simplest way to do so. Most of the time you will be using the vi editor to create or modify files. The vi editor is discussed in more detail in Chapter 5.

Listing Files

Now that you have created a file, you can verify it by listing it using the ls command.

 $  ls newfile  newfile $ 

The ls command shows that our newly created file, newfile , does exist and that the file creation process was successful. What if you want to see a list of all other files? This is very simple; you use the ls command without any argument.

 $  ls  FORMAT   FORMAT.ZIP   myf   newfile   rafeeq.zip $ 

Now the ls command shows that there are five files with the names shown above. Please note that UNIX is case sensitive, meaning it differentiates between lowercase and uppercase letters . So the file name myfile is different from MyFile .

HP-UX has another popular command to list files. This is the ll (long listing) command.

 $  ll  total 350 -rw-r-----   1 boota   users  104230 Aug 27 19:04 FORMAT -rw-rw-rw-   1 boota   users       0 Aug 30 20:47 myf -rw-rw-rw-   1 boota   users      72 Aug 30 20:47 newfile $ 

This command shows that there are three files, with the names displayed in the last column of the output. If you are wondering what the -rw-rw-rw- characters displayed in the first column are, just leave these for the time being. These are the file permissions showing who is allowed to read, write, and execute a particular file. We will be discussing file permissions in more detail in Chapter 7. If you remember from the first chapter that some commands are linked to other commands, ll is another example. This command is linked to the ls -l command. The ls command has many options, and you can have a look at these using its manual pages.

Now we try to figure out the other columns in the file listing. The second column shows how many links are associated with this file. A 1 (numeric one) means there is no other link to this file. The next column shows the owner of the file. The users is the group name of the user boota who owns this file. The next column shows the file size in number of bytes. Then we have the date and time of last modification to the file, and in the last column the file name is displayed.

Deleting Files

To keep the system clean, you need to delete unwanted files from time to time. The files are deleted with the rm command.

 $  rm newfile  $ 

Warning

The rm command has no output in the normal case. You need to be careful when deleting files, as the deleted files cannot be undeleted. An error message is displayed only if the file you are deleting does not exist.


Displaying Contents of a Text File

We already have used the cat command for creating new files. The same command is used to display contents of text files.

 $  cat newfile  This is first line. This is the second line. This is third and last line. $ 

We have just omitted the " > " symbol from the command line. The cat command displays the entire contents of a file in one step, no matter how long the file is. As a result, the user is able to see only the last page of text displayed. There is another useful command with the name more that displays one page of text at a time. After displaying the first page, it stops until the user hits the spacebar. The more command then displays the next page of text and so on. Figure 2-1 shows a screen shot of the more command while displaying the .profile file.

Figure 2-1. Use of the more command.

graphics/02fig01.jpg


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

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