Creating, Moving, Renaming and Copying Files and Directories in Linux

Most basic Linux file and directory management – creating, renaming, moving and copying – can be achieved with the use of just three Linux commands. Their use is fairly straightforward, though it’s probably a good idea to be reasonably familiar with the Linux directory structure before getting too adventurous with file work (or download Knoppix to test drive Linux).

To rename a file, we use the mv command, giving two arguments. The first is the current name of the file, and the second is the name to change the file to.

$ mv oldname.txt newname.txt

Here the file oldname.txt is renamed to newname.txt. The same absolute and relative rules apply when addressing files here, so the file oldname.txt has to be in the current working directory.

Moving a file is achieved using the same command.

$ mv something.txt newplace/something.txt

This will move the file something.txt from the current directory to the directory newplace. The directory has to exist, and we’ll look at creating directories in a moment.

It might be worth pointing out that using the mv command can achieve both moving a file and renaming it at the same time, like this.

$mv oldname.txt newplace/newname.txt

So we come to copying files. This is done using the cp command.

$ cp this.txt there.txt

In this example the file this.txt is copied to file there.txt. This is extremely useful for creating backups of files before editing them. The convention in this case is to use a .BAK extension.

$ cp important.txt important.txt.BAK

Notice that the previous file extension of .txt has been kept, which isn’t necessary but just means that you won’t forget it.

Finally a point on the tilde character, ~. This is a special character that represents the current user’s home directory, and can provide a useful shorthand. For instance, take this command:

$ cp thisfile.txt ~

This will copy the file thisfile.txt into the home directory. This applies to mv too.

$ mv thisfile.txt ~

This moves the file to the home directory. It’s useful to remember, then, that the tilde ~ character means home.

Creating Directories

Organising your file system is hugely important so there will be times when you need to make directories for your files. This is done using the mkdir command. The argument is the name and location of the new directory, and the location can, as usual, be absolute or relative.

$ mkdir [directory]

As long as you have permission, this command will create a subdirectory, relative to the current directory, called whatever name you give it.