Home > Articles and Editorials > Web Development and Design > Getting Dedicated - Moving to a Dedicated Server
Essential Linux Commands (3)
Linux Commands: Deleting Files
Deleting files is achieved with the rm command, as long as you have permissions. Deleting a single file is straightforward.
$ rm thisfile.txt
With this command the file thisfile.txt will be deleted. The power of this command can be extended with the use of wildcards. For instance, if you want to delete all the files in a directory that end with .txt, you can use the following command.
$ rm *.txt
The * character can represent any number of characters, so that in this example all files that end with .txt will be matched. Obviously you need to take a lot of care using this facility.
Linux Commands: Deleting Directories
The rmdir command can be used to delete directories as follows.
$ rmdir [directory]
Note, though, that this will only work if the directory is empty of files. To empty a directory that contains files you need to use the following command format.
$ rm -r [directory]
The -r option tells the operating system to delete the directory and everything in it. This is clearly something to be very careful of! Notice, too, that rmdir is used for empty directories, but rm -r for directories that contain files.
Linux Commands: Viewing files
To view a file's contents we can use the less command. For instance, if we have a text file info.txt, we'd type the following.
$ less info.txt
This will present the text in the file for reading. We can use the arrow keys to navigate around large files, and the page up, page down, home and end keys too. Note that in Linux there are many text files with many different extensions, but typically .conf and .ini files are worth looking at. Reading through these files (very often in the /etc directory) can start to give some inkling of how the operating system and various applications are working as they are often very well commented.
When you have finished looking at a file, simply press the q key to quit. Note that the less command will not allow you to edit files, which is something that we will come to.
Linux Commands: Finding files
Finding files is always going to be necessary, especially when you're unfamiliar with an operating system. The first thing to do is to build the search index for the operating system.
$ updatedb
This command will have the operating system build a database of the files on the system. It may take a while, but be patient. You'll need to keep updating the database as new files and applications are added, or it will become out of date. You can set up the updating of the database to happen automatically with a cron job, but we'll come to that later. For now, entering this command will make sure that the files are all ready to be searched.
Then, to find a file, or directory, use the locate command.
$ locate yourfile.txt
This will search the system for the file yourfile.txt, and will present the absolute location of it in the results. Sometimes, you may want to search for a substring of a filename. This is possible, but it may return an awful lot of results.
$ locate txt
This will return every file that includes txt anywhere in the name, not just the extension. The results can be vast, so in this case it can be useful to "pipe" the command to a less command.
$ locate txt | less
This will return exactly the same search results, but will present them in a more manageable format using less. That way you can navigate up and down the list of results until you find what you're after. Press q to quit less and return to the command prompt when you're finished.
You now know how to log on to your server, set up a user account for yourself, and navigate the file system with a set of basic Linux commands. Next we'll look at how to edit text files with vi, and so make a start on setting up our dedicated server.
Last Update: Sat Sep 9th 2006
