Managing Files

Linux includes a number of commands to help you read files in different ways. You can verify different types of files, and you can read files from the top or from the bottom. This read can be limited to a few lines, or it can set you up to page through the entire file. You can also count the lines, words, and alphanumeric characters within a file. In addition, Linux lets you search through a file using the search term of your choice.

Because it is difficult to define words or lines in binary files, most of these commands work best with text files.


Although some distributions differentiate between file types by color , there are no standard extensions in Linux. Executable files don t end in .exe , and document files may not end in .doc . The file command allows you to view the type of each file. You can see how this works in Figure 6.2.

click to expand
Figure 6.2: Reviewing different file types

As you can see in Figure 6.2, you are not able to see the file type if you don t have the proper permissions.


The concatenate ( cat ) command sends the text of a file to standard output. You can use the cat command on any file. The following command sends the text of the file to your screen:

 # cat  file  

This command is flexible; you can even use it to read multiple files, in sequence, with the cat file1 file2 command.

head and tail

The head and tail commands are like two sides of a coin. The head command provides you with a view of the first few lines of a file; the tail command provides you with a view of the last few lines of that same file. You can regulate the amount of the file that you see with switches. For example, use the following command to see the first 15 lines of the bully.txt file:

 # head -n15 bully.txt 

If you substitute tail for head , you see the last 15 lines of this file. Table 6.7 lists more switches you can use with these commands.

Table 6.7: head and tail Commands



head 400b bully.txt

You see the first 400 bytes of the file known as bully.txt .

tail 4k bully.txt

You see the final 4KB of the file known as bully.txt .

head 3m bully.txt

You see the first 3MB of the file known as bully.txt .

tail -n22

You see the final 22 lines of the file known as bully.txt .

more and less

The more and the less commands aren t opposites, like head and tail . They both start at the beginning of a text file. When you run these commands on a text file, you review the contents of the file one page at a time. The less command is more versatile; unlike more , it allows you to scroll up and down any large text file by using the Page Up and Page Down keys on your keyboard.

Because they can read text a little bit at a time, these commands can open a file more quickly than a text editor like vi . The less command also has some of the advantages of the vi editor, since you can use some vi commands to search through a file.

Each command includes two sets of options. A command like the following sets up the file named bigfile with line numbers :

 # less -N bigfile 

Once the text file is open, you can run other commands, as described in Table 6.8.

Table 6.8: Commands Used After less Is Applied to a Text File




Pressing the spacebar on your keyboard scrolls forward one page in your screen.

page up

Scrolls back one page on your screen.

page down

Scrolls forward one page on your screen.


# represents a number. For example, 8z scrolls forward eight lines in the file. If you do not use a number, this command is equivalent to the space command.


Searches through the file for the text string abc . This is a command from the Linux vi text editor.

The more and less commands are also known as pagers because they allow you to review text files one page at a time using the Page Up and Page Down keys on your keyboard. When you ve finished, just press the q key to exit from this "browse" mode.


As shown in the output from ls -l , each file is associated with owners , groups, and a series of permissions. (For an example of this setup, see Figure 6.1 .) The permissions associated with a file are assigned to owners, groups, and everyone else on your Linux computer. Take a look at the following entry, which is the output from an ls -l command applied to a hypothetical file named abc :

 -rwxrw-r-- 1 root root  1213 Feb 2 09:39 abc 

Permissions are based on the characters on the far-left end of the output. The 10 characters determine what different users can do with this file.

If the first character is not a dash ( - ), it s not a regular file. It could be a directory ( d ) or a file that is linked ( l ) to another.

The remaining characters can be grouped in threes. The subsequent three characters shown are rwx . In other words, the owner of the file named abc can read ( r ), write ( w ), and execute ( x ) this file.

The next three characters shown are rw- . Users in the same group as the file owner can read this file ( r ) or edit and write to this file ( w ). These users cannot execute the file.

The final three characters are r ” . Users that don t belong to the same group as the file owner can read this file. They can t write to it, nor can they execute it if it s a script.

You can set up these permissions on any file using the following command:

 # chmod 764 abc 

Permissions are set with a three-number code. In the preceding command, the first number ( 7 ) sets permissions for the owner, the second ( 6 ) for the other users in the owners group, and the third ( 4 ) for everyone else. Each number represents all permissions given to the owner, group, or everyone else, as described in Table 6.9.

Table 6.9: Numeric Permissions






= r (4)



= w (2)



= x (1)



= r (4) + x (1)



= r (4) + w (2)



= w (2) + x (1)



= r (4) + w (2) + x (1)

Look at the permissions associated with the file named abc again. Because the first number is 7, the owner of this file has read ( r ), write ( w ), and execute ( x ) permission to this file. Since the second number is 6, other users in the owner s group have read ( r ) and write ( w ) permissions on this file. Since the third number is 4, everyone else has just read ( r ) permissions on this file.


Two closely related commands are chown and chgrp , which the root user can use to change the owner and group owner of a file. For example, the chown mj abc command makes user mj the owner of the file abc .


When you create a new file or directory, the permissions you get depend on the value of what is known as the umask . Type umask at the command-line interface, and you ll see the current numeric masked value of your permissions:

 #  umask  0022 

To understand this number, you need a clear idea of the numeric value of permissions. The first number in the umask is currently unused. So the actual umask is 022.

Now let s look at an example. If you gave everyone permissions to your files and directories, you would have read, write, and execute permissions for all users. As discussed in the previous section, these permissions correspond to the number 7 ( r + w + x = 4+2+1). When applied to all users, they correspond to 777. You could set up the same permissions for all users on the abc file with the following command:

 # chmod 777 abc 

By convention, this corresponds to a umask of 000. However, umask does not allow you to configure execute ( x =1) permissions on any file. Therefore, a umask of 022 corresponds to permissions of 644, or rw-r--r--; in other words, for new files, the owner has read and write permissions, the members of the group that own the file have read permissions, and all other users have read permissions.


Mastering Red Hat Linux 9
Building Tablet PC Applications (Pro-Developer)
ISBN: 078214179X
EAN: 2147483647
Year: 2005
Pages: 220

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: