Setting Up Files and Directories

Creating a file in Linux is easy. You can copy from an existing file or save to the filename of your choice from an editor or another application. There s even a special command that allows you to set up an empty file. It s also easy to delete a file ”so easy that some commands for deleting files can be dangerous.

Although a Linux directory is just a special file, Linux includes specific commands for creating and deleting directories. First, we ll look at the file management commands, and then we ll examine the commands for creating and deleting directories.

touch

There are times when you simply need to set up an empty file in Linux. For example, before you can activate a quota for a user or a group , you need to create an empty aquota.user or aquota.group file in the target directory. Creating empty files is easy with the touch command. The following commands create these files in the /home directory:

 # touch /home/aquota.user /home/aquota.group 

The touch command can also be used to change the timestamp associated with an existing file. When you use the command without a switch, the last access time of the file is changed to the current time. For example, suppose it s 11:21 on April 15 and you run the following command:

 # touch /root/f0601.tif 

When you run the ls -l command on the f0601.tif file, you see the following output:

 -rw-r--r--   1 root root   883823 Apr 15 11:21 f0601.tif 

Other switches, such as -t , can change the access time associated with a file as desired.

cp

The simplest version of the copy command is cp file1 file2 . Issuing this command copies the contents of file1 and places them in destination file2 . The destination file will have a new creation date and inode number. Other copy commands can overwrite destination files. You can even use a switch for the cp command to copy the contents of one or more subdirectories. See Table 6.3 for examples of how the cp command works.

Table 6.3: cp Commands

Command

Result

cp file1 file2

Copies the contents of source file1 to destination file2 . The destination file has a new creation date and inode number.

cp file * Dir1

Copies multiple files to a directory.

cp -f file1 file2

If you already have a file named file2 , this command overwrites its contents without prompting.

cp -i file1 file2

If you already have a file named file2 , this command prompts you for confirmation before overwriting this file.

cp -p file1 file2

Copies the contents of source file1 to destination file2 . The destination file has the same inode number and creation date as the source file.

cp -r Dir1 Dir2

Copies the contents of the directory named Dir1 , including subdirectories, to Dir2 . The effect is recursive; in other words, if there are subdirectories under Dir1 s subdirectories, their files and directories are also copied .

cp -u file1 file2

If you already have a file named file2 and file1 is newer , this command overwrites its contents without prompting.

Note  

An inode is the identifier used on each Linux partition for a file. Every file gets its own inode. The inode includes metadata about the file, which includes the permissions, size , last access time, and the disk block where the file is located. If the inode is misaligned or corrupted, Linux won t be able to find the associated file. In addition, identical files have the same inode number. But because you can t have the same inode number on different partitions, the cp -p file1 file2 command doesn t work if you re copying a file from one partition to another.

mv

If you want to rename a file in Linux, you move it. The mv command changes the name of a file. Unless you re moving a file to a different volume, everything about the file, including the inode number, stays the same. There are four key move commands, as shown in Table 6.4.

Table 6.4: mv Commands

Command

Result

mv file1 file2

Changes the name of a file from file1 to file2 . If the source and destination files are located on the same volume, the files retain the same inode number.

mv file * Dir1

Moves multiple files to a directory.

mv -f file1 file2

If you already have a file named file2 , this command overwrites its contents without prompting.

mv -i file1 file2

If you already have a file named file2 , this command prompts you for confirmation before overwriting this file.

Tip  

Some Linux users create files that start in lowercase, such as file1 , and directories that start with a capital letter, such as Dir1 . This is far from an absolute rule; standard Linux directories start in lowercase letters , such as /bin .

rm

You can use rm to remove files and directories. This is one of the reasons that many Linux administrators are advised to run Linux in root or superuser mode only when necessary; small mistakes in this command can easily delete all of your Linux files. For example, suppose you want to remove a group of temporary directories in your root ( / ) directory: a.tmp , b.tmp , and c.tmp . You want to use the rm -r *.tmp command, but instead you type the following:

 # rm -r * .tmp 

Because there is a space between the asterisk and the .tmp , the shell assumes that you want to recursively delete all directories and then delete the file named .tmp . The result is not good.

For this reason, Red Hat configures the following as an alias for the root user:

 alias rm=rm -i 

The alias ensures that whenever you use the rm command (even rm -r ), the shell prompts you for confirmation before you delete any files. Some Linux distributions set up this alias as a shell variable for root users. The key rm commands are shown in Table 6.5.

Table 6.5: rm Commands

Command

Result

rm file1

Deletes file1 without prompting for confirmation. However, this command does not supersede an alias rm= rm -i , which requires confirmation.

rm -d Dir1

Deletes Dir1 without prompting for confirmation. However, this command does not supersede an alias rm= rm -i , which requires confirmation.

rm -i file1

Deletes file1 after prompting for confirmation from the user.

rm -f file2

If you already have a file named file2 , this command overwrites its contents without prompting. It even supersedes an alias rm= rm -i .

rm -r *

Removes files recursively; if there are any subdirectories in the current directory, this command deletes them (and all of their files) as well. However, this command does not supersede an alias rm= rm -i , which requires confirmation.

Tip  

You can find default aliases with the alias command.

start sidebar
Administering as Root

One of the raging debates in the Linux community is whether it s sensible for a Linux administrator to log in as the root user. Errors as root can damage or destroy the files on your system. In addition, logging in as root may expose the root password to someone who has put a Trojan horse program on your system.

On the other hand, Red Hat has made it safer to use the root account. Good aliases make it more difficult to accidentally delete key files. Defaults such as root_squash in NFS prevent root users on other computers from sabotaging your system. You can further protect your system with passwords for the GRUB bootloader and your BIOS. Because the people I know at Red Hat use the root account regularly, I do the same in this book.

If you do log in as the root user, remember to be careful. Don t leave your system without logging out; otherwise , someone could change your password and access your system at his or her leisure. And don t expose your system to services that can read or even control what you do as root, such as AT&T s Virtual Network Computing (VNC) environment.

end sidebar
 

ln

Instead of just copying or moving a file, you can link it. Links are common, especially for those programs that start at various runlevels. When you link a file, you re creating another path to a currently existing file. For example, if both you and a colleague are working on a file named project , you can create a linked file in your home directory. Assume the project file is in the /home/jm directory. To create a link to a file in mj s home directory, you use the following command:

 # ln /home/jm/project /home/mj/project 

When you work on either file, the change and results are visible and accessible to those who access both directories. This is sometimes known as a hard link . With a hard link, because both files retain the same inode number, both files are identical. If the original file is deleted, the hard-linked file remains in place. It retains all the information from the original file.

Note  

The ln file1 file2 command produces the same result as the cp -p file1 file2 command. Unless the files are located on different partitions, file1 and file2 retain the same inode number.

One useful option for links is symbolic mode , which allows you to see the linked file. For example, if you run the following command:

 # ln -s /home/jm/project /home/mj/project 

you will see the linked file when you run a long listing ( ls -l ) of that file. This is known as a soft link . If the original file is deleted, the soft-linked file points to an empty file. The information in the original file is lost.

mkdir and rmdir

As you d expect, the mkdir command lets you create directories. The directory that you create does not have to be based in your current directory. You can make several levels of directories if you choose. You can also assign the permissions of your choice to the directory that you create. The key mkdir commands are shown in Table 6.6.

Table 6.6: mkdir Commands

Command

Result

mkdir -p Dir1/Dir2

Creates a directory named Dir2 . If Dir1 does not exist, the -p switch tells Linux to create that directory as well. Both are created as subdirectories of the current directory.

mkdir -m 755 /usr/ Dir3

Creates a directory named Dir3 as a subdirectory in the /usr directory. The permissions (755) are rwx for the owner and r-x for other members of the group and everyone else.

The rmdir command allows you to delete empty directories. The directory that you remove does not have to be based in your current directory. You can delete several levels of directories if the directory that you delete empties others. For example, with the following command, you can delete the directories named Dir1 and Dir3 :

 # rmdir -p  Dir1  /  Dir3  

This command deletes directory Dir3 if it is empty. If the only "file" in directory Dir1 is Dir3 , this command also deletes directory Dir1 .

 


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

Similar book on Amazon

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