Commands with Some Teeth

 < Day Day Up > 



The simple commands you have tried so far are all of the safe-and-sane, fire marshal–approved variety; they merely print out information to your Terminal. Now you are going to try to get some real tangible results from the commands you use. These commands are also essentially safe and sane if you follow my instructions.

$ mkdir

You have already learned how to create folders by means of menus and your mouse, but you can also do this using the command line. The command is mkdir (make directory), and it is easy as punch to use (though I’ve never been quite sure how punch is easy).

To see how this command works, and to work with the commands that follow, use the mkdir command now to create a folder called command_exp (for command experiments). All you have to do is type mkdir command_exp and then press ENTER. The new folder should appear in your Home folder, so go ahead and check to see if it is there by clicking the user’s Home icon on your desktop.

OK, good, bra, bueno! Now let’s create another new folder within that new folder — a subfolder, if you will. We’ll call this one sub. So, just type mkdircommand_exp/sub and then press ENTER. You can now go take a peek and see if the subfolder appears within the command_exp folder, if you like.

$ mv

The next command is the mv (move) command, but before we experiment with it, we need to create a dummy file — we need something to move, after all. Go to the Main menu and select Accessories > Text Editor. This will bring up the text editor program called Gedit. All you are going to do with Gedit is click the Save button; you don’t have to write anything in the document itself. A Save As window should then appear, and in the box near the bottom of that window (just under the words Selection: /home/username), type expfile and then click OK. The new file, named expfile, should appear in your Home directory.

To move the file that you’ve just created, you will use the mv command, of course. Just type mv expfile command_exp/sub (this tells the system which file to move and where to move it to) and then press ENTER. The file will now be in your sub folder.

$ cd

Until now, you have been using the command line from your Home folder. With the cd command, you can change your Terminal’s location to another folder. This is a very handy command that you will be using quite a lot when doing the other projects in this book. To take it out for a spin, let’s get inside the command_exp folder by typing cd command_exp and pressing ENTER. If you’ve done this correctly, the prompt in your Terminal should now read [username@localhost command_exp]. If so, you can pat yourself on the back.

While you are there, you might as well try out the ls command with -R flag to see how that works. Just type in ls -R and press ENTER. Your Terminal should now show that you have a subfolder there called sub and a file inside that subfolder called expfile.

That is all you really want to do in there for now, so to get back to your Home directory, just type cd and press ENTER, which will take you back home, so to speak.

$ cp

Being fickle, as humans by nature are, you have now decided that you not only want your expfile in the sub folder, but that you also want a copy in your Home directory, where it was in the first place. To copy expfile, you can use the cp(copy) command.

To do this, the command needs to know where the file you want to copy is, what it is called, and where you want to copy it to. In this case, type the following command (replacing username with your actual username) and then press ENTER:

cp command_exp/sub/expfile /home/username 

Be sure to put a space between the file you are copying and its destination (in this case, between the expfile and /home/username).

Once you’ve done this, you should have two copies of expfile, one in your Home folder and one in your sub folder. Go have a look to see the fruit of your endeavors.

$ rm

When you were a kid, you may well have experienced the joy of building a castle out of LEGO bricks and then the even greater joy of tearing the whole thing down (preferably with D-cell batteries). We will now embark on a similar move. The first tool in this nostalgic endeavor is the rm (remove) command, with which we can trash files.

The rm command, albeit very useful and easy to use, should be used with caution. Once you remove a file with this command, there is no going back — the file is gone for good.

To play it safe, let’s try out the rm command by getting rid of that new copy of expfile that we just created in your Home folder. The basic rm command structure consists of the command itself, rm, followed by the name of the file that you wish to remove. In this case, you want to remove the file called expfile located in your Home folder. Assuming your Command Terminal shows you to be Home, you can remove the file by typing rm expfile followed by a tap on the ol’ ENTER key. The file will then be gone, and gone for good.

Now, double your pleasure by getting rid of the version of expfile that is located in the subfolder sub. In this case, you need to specify where the file is because it isn’t in the folder that the Terminal is in. Just type rm command_exp/sub/expfile and then press ENTER. Oooh, very cool. Brings ya back, doesn’t it?

$ rmdir

You will now continue the fun with the rmdir (remove directory) command, which is a bigger and more powerful version of the rm command.

The rmdir command, like the rm command, should be used with caution. There are no do-overs with rmdir. Once you remove a directory or folder with this command, it is gone for good.

To try this command, you can get rid of that sub folder you created. Type rmdir command_exp/sub and press ENTER. The sub folder should now be gone. Finally, to round out the fun, use the rmdir command once more to get rid of the command_exp folder that we created earlier. You’ve probably got it down by now, but just in case you haven’t, type rmdir command_exp and then press ENTER.

$ chmod

In Chapter 3, you learned how to change file permissions via the Nautilus interface. This is without a doubt the easiest way to go about such things, but when you have a folder full of files, perhaps copied to your hard disk from CD, that are emblazoned with the verboten symbol, it can prove quite tiring to change the permissions of such files one by one. In this case, the command-line approach proves to be much easier to deal with.

The command for changing file permissions is chmod (change mode). To use it, just type the command followed by the permissions you want to extend to a file, and then the location of the file itself. For example, let’s say that you copied a JPEG file called mybirthday.jpg from CD to the personal subfolder within the Photos folder on your hard disk, and the file is write protected. To change the file so that you have write permissions (meaning that you can alter the file), you would type in the following and then press ENTER:

chmod 744 photos/personal/mybirthday.jpg 

To change the permissions of all the files and subfolders (and all the files within those subfolders) in one fell swoop, you can add the -R (recursive) flag to the chmod command. The command would thus be as follows:

chmod -R 744 photos/personal

The number 744, by the way, extends read, write, and execute permissions to you, the owner, but gives read-only rights to everyone else — a pretty safe choice when in doubt. If you want to figure out permission numbers for yourself, it is pretty easy. You are basically dealing with three number positions, each of which has eight numerical possibilities (0–7). The left slot represents permissions for the owner; the center slot represents the permissions for the group; and the third slot represents permissions for others.

The meanings of the numbers themselves are as follows:

7 = Read, write, and execute permissions

6 = Read and write permissions

5 = Read and execute permissions

4 = Read-only permissions

3 = Write and execute permissions

2 = Write-only permissions

1 = Execute-only permissions

0 = No permissions

Figure 9-6 points out the meaning of each of these numbers and what each number slot represents — permissions aren’t all that complicated.

click to expand
Figure 9-6: The meaning of permissions numbers



 < Day Day Up > 



Linux for Non-Geeks. A Hands-On, Project-Based, Take-It-Slow Guidebook
Linux for Non-Geeks: A Hands-On, Project-Based, Take-It-Slow Guidebook
ISBN: 1593270348
EAN: 2147483647
Year: 2003
Pages: 188

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