Monday, 25 August 2008

Manipulating files and folders

Apologies for the delay since my last post - as I don't have access to the Internet during the week at present and my parent's Internet connection has been playing up, it's been hard to get any blogging done! Hopefully that will change soon!
Anyway, manipulating files and folders is something that you need to know about, seeing that Linux applies the Unix philosophy of "everything is a file". So I'll walk you through some basic commands.
First up is mkdir. This command makes a new folder. So, if you're in your /home directory and you enter the following:
mkdir Work

You'll create a new directory called Work. Never forget that Linux, unlike Windows, is case sensitive. Also, if you want to create a new directory anywhere other than in your own /home directory, you'll need to use sudo.
To remove an empty directory, you can use rmdir, as in this example:
rmdir Work

Now, to remove a file, you use the rm command, as in this example where we're deleting the document Example.html:
rm Example.html

Like most commands in Linux, rm has a number of options you can specify. A word of warning: watch out for the command rm -rf. The r option means remove things recursively (so you can remove a directory AND everything inside it), while the f option forces it, even if it gives a warning. You can specify the folder you want to delete with its path, so NEVER run the following command (unless you actually want to destroy your system!):
sudo rm -rf /

This means "delete the whole filesystem recursively, ignoring all warnings, with root access", essentially meaning delete everything!. I've tried this on VirtualBox installs of Ubuntu and it will destroy everything on your system! That said, the command rm -rf can be quite handy for deleting a folder and it's contents, but be VERY careful with it!
To copy a file, use the cp command, with the source followed by the destination, as in this example:
cp Example.html /media/USB

You can change the name of the destination by specifying the file name as well, like this:
cp Example.html /media/USB/Example2.html

Moving a file is similar to copying it, with similar syntax:
mv Example.html /media/USB

And again you can change the name, in the same way as with cp. Or you can use it to rename a file without moving it, like this:
mv Example.html Example2.html

Now, moving files and folders is all very well, but how can you show them? Simple, use the ls command:
ls

To show hidden files and folders as well, use this:
ls -A

As usual, if you want to know more about these commands, refer to the man pages for more specific guidelines. Hopefully this has given you enough to get started. I recommend spending a little time becoming comfortable with these commands, they'll give you a sound grasp of the fundamentals of using the command line.

No comments: