Working with Files at the Unix Shell Prompt


When working with Unix, you will most likely find it easiest to work with tools at the shell prompt. Using commands such as touch and rm is very helpful, and these commands perform different niche-based functions in much the same way as the whereis command covered in the last lesson. In Unix, there are multiple ways to create a file and edit it.

The command line is your primary interface to the Unix file system as well as your primary tool for creating, deleting, and rearranging your files. In this part of the lesson, we will learn how to manipulate data within the Unix system with the touch command as well as with commands used to help you remove newly created data on your Unix system.

The touch Command

Creating a new empty file in Unix is easy when you use the touch command. The touch command is used to update the last modified time of a file, setting it to the current time. This is usually not commonly used, but knowing about it can serve you well. The touch command is normally used with one particular area: backup and disaster recovery. As a Unix system administrator, you may be asked to do backup and restore jobs. There are a few different types (and methods) of backup and restore you can choose from, one of which is called an "incremental backup. Although this may not be something you work with in Unix, it is something you are most likely affected by. Most systems are backed up and protected by companies that need to save and keep their valuable data.

The touch command can be used with the incremental backup by helping to verify that a backup was in fact completed. Even if you never altered the data or modified it for any reason, you can still use the touch command to change the modification date and make it appear as if it had been modified, thus making it appear to have been modified at the time you touched it. This can be useful when you work with backups in particular, and you may also find other uses for it in your work with Unix.

Where Did That New File Come From? When you "touch" a file by accident, you may wind up with a new file you may not have wanted. If you use the touch command and do not specify the actual filename (a file that really exists on your system), Unix will create a new file for you. For example, if you said you wanted to touch a file on your system named unixmaster, and it was really called unixmasters, then a unixmaster file will be created by Unix within the current directory you are working (or saving). You can do this by typing pwd.


Now that you understand the touch command, let's take a look at how you can use it. In this example we will look at both how to change the modification date of a file and how to make a new file altogether.

1.

Determine the filename of the file or files you want to update or create.

2.

Issue the touch command as touch <filename>.

For example, type

 > touch backupfile 

If the backup file previously existed, its last-modified date would now be set to the current time. If the backup file did not previously exist, it would now exist as an empty file with a last-modification date of the current time. It's that easy.

There are additional uses for the touch command. When you become comfortable enough with Unix to start automating your work using shell scripting (discussed in Lesson 14, "Shell Scripting Fundamentals"), you will find the touch command useful for creating "flag files" that allow your scripts to talk to each other. Keep the touch command in mind during Lesson 14.

Use Your Time Correctly When working with Unix, (or any other operating system), you should have the correct time set on your system. This is usually done by a system administrator, as it's normally not done locally on the system. Instead, a time master is usually found somewhere on the network, from which your Unix system may pull the time. Of course, if your Unix system is not networked, then you will most likely be getting your correct time from the local system. Be aware of what time your Unix system says and where you are getting your time from.

Having your Unix system set with the proper time will help not only with file management, but also with logging and security.


Removing Files with the rm Command

If you used the touch command and now have a file that you do not want on your Unix system, you may want to get rid of it. In this next section, we will learn how to remove files from your Unix system.

The rm command is used to delete unwanted files on your Unix system. Remember that using this command can get you into some trouble if you don't use it properlyyou could remove data from your system that you do not want to remove. Unix takes what you say very literally, so be carefulyou are not in a Microsoft Windows environment that checks you every step of the way when you delete a file. If you want to remove a file from your Unix system, follow these steps to use the rm command:

1.

Determine which file, or files, you want to delete.

2.

Issue the rm command as rm <filename>.

As with using touch, rm is easy: As long as you can find and specify the filename, you can remove it.

What If I Have Multiple Files? You can delete multiple files by leaving a space after each filename and then specifying the next. You can add or remove multiple files by specifying what files you want to add or remove, one after the other in tandem.


An example for using the following rm command:

 rm newfile2 

After issuing the rm command for the files you want to delete, you might be presented with a response such as

 remove newfile (y/n)? 

You can also use options with the rm command. Just like you used find with the print option, you can use the rm command with its own set of options. Of course, you can use the man pages to learn more.

The rm command supports several useful options such as i, -f, and r. Each option will perform a different function when specified.

The "i" stands for interactive mode. The -i option makes rm ask you to confirm the deletion of each file before it is actually deleted.

Most Microsoft Windows users are familiar with this "safety checking" when you want to delete something. This -i option is how you can be absolutely sure you want to remove what you specified with the rm command in Unix.

Thus, the rm interactive mode is nothing more than a way for Unix to check with you to ensure that you really want to delete the data you specified. If you really want to remove the data, press y. This will take you back to the shell prompt, and your data will have been removed from the system.

The interactive mode will be configured by your Unix system administrator usually unless you are on a highly customized system.

The next option is rm using the -f option. The -f requests rm when you need to delete files that may have file permissions set on them. This option will allow you to remove the files without worrying about file permissions stopping the deletion process. Remember, you can create files and set the permissions on them so that even you can't read them. There will be times when using Unix where you may need to delete a file or files without having permission to read or write to them.

Recursive Is Not a Curse Word in Unix In this lesson we will discuss commands that, when used with a particular option, can perform recursive actions. What is that?

When relating to file management in Unix and the directory structure where Unix maintains its files and data, "recursive" means that when used, it will go through the directory from the starting point, down through all subdirectories, until it cannot search anymore.

The recursive actions are also repetitive; it will continue to search through each and every directory until the operation has been completedit's an exhaustive command.


The last option we will discuss is the -r option, which stands for recursive. The -r option is a powerful option when using rm; the recursive mode can cause panic if you didn't intend to use itso be careful. This command will remove the directory you specify, as well as anything that is contained within it, including other directories.

New Unix Mantra: Take a Quick Peek Before You Delete! The recursive option of the rm command is able to remove large amounts of data very quickly, so unless you have a backup handy, you may want to enter the directory you want to delete and use the ls command to see what is in the directory.


So now that we know how to create and delete files, let's look at another large aspect of file management within Unix: creating directories.

The mkdir Command

As mentioned earlier, directories are basically used to organize data. You may have experienced instances where you have one directory with hundreds of files. It would not be easy to find anything quickly unless you memorize every filename on your system.

Think of a phone book or telephone directory: The Unix directory is much the same. What if all businesses in your area were only listed in alphabetical order? How does that help you? How could you call an electrician, plumber, or anyone else unless you had groups of similar categories organized for quick retrieval? This is exactly why you need directoriesfor the simple organization of data on your Unix system. In this part of the lesson, we will learn to make directories.

To make a directory, you can do the following:

1.

Choose an organizational structure, and name your new directory.

2.

Issue the mkdir command as mkdir <directoryname>.

For example:

 > mkdir test 

This creates a new directory named test, which will be located in the current directory. To find the current directory you are in, you can issue the pwd command, and then the mkdir command. Doing an ls will show you the contents. So, now that you know how to make a directory, you will need to know how to delete it from your Unix system as well.

The rmdir Command

The rmdir command will delete an empty directory. What this means is that the directory you want to remove cannot contain any data within it.

To remove a directory, do the following:

1.

Decide which directory, or directories, you want out of your way.

2.

Issue the rmdir command as rmdir <directory>.

Don't Forget! You Can Do Many At Once! Don't forget that you can remove multiple directories at one time. As with most file management commands, specify the directories in sequence and they will be removed. For example, if you wanted to remove "test" and "test1," the two directories could be removed with one issuance of the rmdir command. You could rmdir "test" and "test1" by typing rmdir test test1.


When you issue the rmdir command, you will remove the test directory you created before:

 > rmdir test 

That's it! That was pretty easy to do. You should now be able to list the contents in your current directory and not see the test directory anywhere. It has been removed. If you had any problems, you may need to make the directory you want to remove from the Unix system "empty." In some cases, you must use rm * first to make the directory empty, and then you can delete it. As this is advanced syntax better covered in Lesson 13, "Regular Expressions," when we cover regular expressions, you should put this * symbol (known as the asterisk, or wildcard) to memory, as you will most likely often use this command when working with Unix.

Now that we have mastered file creation and deletion, as well as directory creation and deletion, you should learn what combinations are most commonly used and helpful, as well as know the answers to the most commonly asked questions: Why would I make a directory empty before deleting it? Couldn't I delete the directory with the files in it? Yes, you may do so with the rm r (recursive) command.

What Is This? The Same Command? Just like the remove files command, the recursive command is pretty much the same commandit just does something different when used in a different context (as in the context of directories), not files. When used this way, you can recursively remove all of the contents within that directory you specify.


The rm r Command

Removing files and directories at the same time can be done when using the rm r command specifying a directory instead of a file. The rmdir command will only work on empty directories, so you will need a way to remove the directory and any contents that may be within it. When you need this functionality, the rm command in recursive mode can be used.

To delete a directory and all its contents, do the following:

1.

Find the directory you want deleted.

2.

Issue the rm command as rm -r <directoryname>.

When you issue this command, you will be able to specify the directory you want to delete, including anything that may be in it:

 > rm -r /priv/home/rob/test2 

If you delete a directory you are currently in, you may get error messages. These may be fixed easily by moving to a known good directory, such as your home directory, symbolized by the tilde. The command you can enter to get back to home is cd ~/.

Wow, think about how far you have come! Not only are we navigating the Unix shell prompt, we are actually making and removing data from the Unix system, as well as becoming more familiar with how other commands tie in to the process. Now we will learn how to copy files from one location to another.

The cp Command

When you want to make a copy of a file, you can use the cp command. The cp command will allow you to copy a single file to a new destination file, or copy one or more files to a single destination directory. This file operation will come up many times in your workings with Unix. It's not uncommon to want to make copies of files you are working on. If you wanted to make a quick backup copy of a file, this command could help you do that.

To use the cp command to make a copy of a single file, follow these steps:

1.

Determine the source filename, and the destination filename to which you'd like to copy the file.

2.

Issue the cp command as cp <sourcefile> <destinationfile>.

There will be times where you may not have just one file, but perhaps a dozen files that you want to send to a particular directory. To do this, you would issue the cp command with the multiple source files listed to one directory.

To use the cp command to copy multiple files to a destination directory, you would change the command as follows:

1.

Issue the cp command as cp <sourcefile1> <sourcefile2> [...] <destinationdirectory>.

To see an example of this in action, type the command

 > cp testfile /priv/home/rob/storage 

The example shown here copies the file testfile from the current directory and places the copy in /priv/home/rob/storage. This would be commonly done if you were working in an environment in which you would work on a file and then send it to a local storage point on your Unix system, maybe in your home directory, for organizational purposes. You may have in your storage folder three other subfolders such as spreadsheet, document, and HTML. This is commonly practiced, and you may want to adopt this technique into your own work environment for your organizational needs.

Back to our example for copying files, if you wanted to send multiple files to your HTML folder, you may do something like this:

[View full width]

> cp /priv/home/rob/index.htm /priv/home/rob/test/banner.htm links.htm /priv/home/rob /storage/HTML

When using the cp command in this fashion, you are telling Unix to copy index.htm from the /priv/home/rob directory, which is where it was saved last, as well as both banner.htm and links.htm from the /priv/home/rob/test directory, and place them all in the /priv/home/rob/storage/HTML directory.

Now that you have learned the copy command, truly master it by reading the man page and making sure you know all the other things you can do with it. Next, we will learn how to copy directories.

The cp -r Command

Now that you have learned the fundamentals of copying files, what about directories? As we have learned with other commands in this lesson, the recursive option is very powerful, so when it is used in this application, it will allow you to copy multiple directories, subdirectories, and files to a destination directory of your choice.

The cp command has a recursive mode for copying directories. When it is used with the following syntax, the cp command with the -r option, you will be able to copy each source directory (as well as files) into whatever destination directory you specify. An example of this would be as follows:

1.

The data to be copied to the destination directory must be specified.

2.

Issue the cp command as cp -r <sourcedirectory1> <sourcedirectory2> [...] <destinationdirectory>.

Performing this action will take whatever you specify as the source and copy it recursively to the destination directory, as seen in the following example:

 > cp -r /priv/home/rob/storage/HTML /etc/HTMLLAB 

This command will copy the /priv/home/rob/storage/HTML directory and its contents into the /etc/HTMLLAB directory. This copied HTML and a subdirectory named IMAGES into the /etc/HTMLLAB directory, thanks to the recursive option.

In this part of the lesson, we covered using the recursive option with the cp command. You can now copy directories as well. Let's take a look at moving files and directories.

The mv Command

Moving files is not like copying them. Copying them leaves the source of the data in the original location in addition to creating a duplicate copy in another location, or in the same location with a different name. To move a file or directory, you need to use the mv command. The mv command will move or rename a file based on the destination. Depending on what you specify as the destination, the filename is going to specify how you used the command.

In other words, look at the following two examples of the mv command:

1.

Locate a file you want to move by its filename. This is the source file you want to move.

2.

Issue the mv command as mv <sourcefile> <directory>.

When used in this fashion, the command will move the selected file to the directory you want it to be in. You can also issue the same command to rename a file. It will also leave the source alone; however, when it copies the file (you can even do this to the same source directory), it is new and the contents are identical. To Unix, there are two completely different files located in the same directory.

To rename a file is not land keep it in the same source directory, issue the mv command as mv <currentname> <newname>.

Why could it not be in the same source directory unless you renamed it? Well, then it would be two instances of the same thing in one directory, and Unix will not allow for thatnor any other operating system. For the file system to be able to manage itself, having duplicate entries in the same source without a way to distinguish the two is not only impossible, but uselessif you edit one, then when you save it (besides for some file size changes), how would you know which is which?

You can do the same exercise with directories as welljust specify directories instead of filenames.

Where Did I Put That Thing? If you move something, you could lose it. So you must be careful and think through your actions. You could inadvertently move data that could ruin a website, or disable a service or application.

Copy is always your best choice, but then you pile up data on your hard disk. If you are not thinking about your actions, you could add storage stresses with unnecessary copying or disable your system by moving something you should have copied. Pay attention and if you make a mistake, contact your Unix system administrator for help, or revert to some of the older commands you learned to help you find what you need.


Let's take a quick look at the mv command in action:

 > mv /priv/home/rob/storage/HTML/index.htm /etc/HTMLAB 

Remember, this will move the source you specify (index.htm) to the HTMLAB directory located in the /etc directory. It's that simple. What if you wanted to move more than one source to a destination? That would be common, much like using the cp command; in that case, you would specify more source files to move.

To move multiple files to a directory, do the following:

[View full width]

> mv /priv/home/rob/storage/HTML/index.htm /priv/home/rob/storage/HTML/GRAPHICS/b1.jpg b2 .jpg /etc/HTMLLAB

This moves the index.htm from the /HTML directory as well as a couple of JPEGs (image files) to the /HTMLLAB directory within the /etc directory.

Before we end this part and move on, there is one limitation to the mv command that you need to know about. In a previous lesson, we discussed the fact that you may have data stored locally on your system, and you may have a network connection to other Unix systems that you may access for data. You accessing their local data would mean that you are accessing that data remotely. If you access data remotely, you may have issues with the mv command. Because the mv command cannot move directories between physical devices, you may need to copy the file from one location to another, and then delete the original. Up to now, we have learned all the commands needed to complete such an operation, shown here in this example of transferring data over a network.

Now that we have learned how to copy and move directories, we will do our last exercise of this lesson: learn how to create links. Remember, we discussed links in Lesson 5, "File System Navigation Techniques," when we saw files "linked" to others.

The ln Command

As discussed previously in this book, when you learn the ls command, you should be aware of "links" to other files when you list out the contents of a directory. Those links can be made with the ln command.

The ln command is used to build links or aliases to other files on your Unix system. You can create manageable links to other files so that they can appear in the ls command output when you want the source file to appear to be in different locations, as well as have different names.

Practical application of this command would allow a source file that constantly changed names to be linked to a name that everyone could remember. For instance, a file named sales_report that all marketing managers could access may be kept up to date by a secretary who links a series of dated files to sales_report. The managers only need to remember one name, instead of having to remember multiple names, or having to constantly find or request what that name would be. It's a way to make things easier for you while working with Unix.

To create a link, do the following:

1.

Specify the particular file you want to link to another file. The ln command will create a link from the source file to an alternative name for that file to be accessed. You need to know the original name and then the name you want it to be referenced by.

2.

Issue the ln command as ln -s <realfilename> <alternatename>.

3.

Issue the ls -l command to view the long listing and the link you created.

When using the ln command, you need to be very specific about creating a link. Take care not to make the mistake of specifying a filename that doesn't exist and then creating a link to itit will be useless and not link to anything. You should view the original source filename and make sure that when you build a link that you specify the exact filename to avoid this error.

Another issue you may face involves forgetting to use the -s option. The -s option is nothing more than specifying that you want to create a soft link instead of a hard link. Since hard links are not really used by Unix end users, we will not dig too deeply into the meanings here, as it's a bit beyond the scope of this book and not likely to be anything you will see or use as a novice user of Unix. As I mentioned earlier in this book, it's imperative that you take the initiative to dig into these topics deeper on your own if you are interested in all that you can do with Unixkeep learning and practicing as much as you possibly can.

An example of the ln command to create a link can be seen here:

 > ln -s /priv/home/rob/storage/HTML/123456789ABC.htm /etc/ HTMLLAB/easy 

If you were to do this, you would create a link named "easy" in the HTMLLAB directory that would be linked to the very long and not easy to remember HTML file located in your stored HTML directory.

That's it! That's creating a link, and now you not only know how to do it, but a practical reason why you should. Our last discussion in this lesson focuses on the proper way of using relative or absolute paths.

Knowing the Right Path

We discussed the difference between relative and absolute paths in Lesson 5. By managing data using commands such as cp and mv, you can also specify different paths when issuing the command. There are shortcuts and other tips (that could fill 50 of these books) that can help you be more productive.

It is very important to remember that when you give a filename as an argument to a Unix command, the filename can be either a relative or absolute path to the file. Consider this when you type out your command. Sometimes you may not be specifying the command properly and that is why it's failing; for example, if relative and absolute paths either use / or not, this may cause you to issue the command incorrectly and possibly give you an error message. If this happens, review the previous lesson on paths and apply that knowledge to your commands as you continue to learn and use them.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

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