Table of Contents
ToC

ToC4. File management

Up to start of section4.1. Creating new files

Files are usually created by an application like an editor, wordprocessor, or spreadsheet, or when you download a file with your web browser. It is possible to create a file from the command-line, but it's extremely rare that you would need to do so.

Up to start of section4.2. Moving and renaming files

Moving or renaming a file or directory in the Unix file system are essentially the same thing. The file or directory doesn't actually get moved physically, it just has the name of its entry in the table of files changed.

To move a file to a new location, type the  mv  followed by the current filename, followed by the name of the directory you want it moved to.

The directory must already exist, so use the mkdir command first, if necessary.

$ mkdir old
$ mv thesis.pdf old/

You can use the ls command afterwards to verify that the file has been moved.

To rename a file or folder (directory), type the  mv  followed by the current name, followed by the new name.

$ mv thesis.pdf thesis-2009-03-27.pdf

You can use the ls command to verify that the file has changed name.

You can do both at the same time if you need to:

$ mkdir old
$ mv thesis.pdf old/thesis-2009-03-27.pdf

This moves the file to the specified directory and renames it as well.

Because directories are just a type of file, you can rename them the same way as you do normal files.

$ mv old /var/archive/backup/oldfiles-2009-03

The new location must of course exist before you can move a file or directory into it. Use the mkdir command to create it if necessary.

The only restrictions are: a) you can't use the  mv  command to move whole directories between disks; and b) you can't move files into directories you do not own. To move files or directories between disks, you must use the cp command to copy the file[s] to the new location, and then use the rm command to delete the originals. To move files into directories you don't own, you must either be the Administrator (root) or you must ask the owner of that directory to allow you in.

Up to start of section4.3. Copying files

To copy a file, type the  cp  command followed by the name of the existing file, followed by the name of the new copy you want to create.

The destination can also be the name of a directory you want to create the copy in.

$ cp thesis.pdf web/phd/

Like the mv command, the destination can be either a file or a directory.

If the destination filename already exists, you will get a warning message and a question about whether you want to overwrite it or not. Answer y or n appropriately.

To copy a whole directory-full of files, and all its subdirectories (if any, and all their files), use the r option.

$ cp -r Mail/ /var/archive/backup/mail-2009-03/

Up to start of section4.4. Deleting files

To remove (delete) a file, type the  rm  command followed by the filename[s] you want to delete.

$ rm thesis.tmp thesis.log thesis.aux

When it's gone, it's gone. Forever. Permanently. There is no Trash or Wastebasket feature when you use this interface. If you delete a file by mistake, you must contact the Operators to get it restored from backup. If it's your own machine, and you don't have a backup, tough.

It is also possible to delete a whole directory and all its subdirectories and all their subdirectories and all the files in them as well with an option to the  rm  command, but you should contact an expert if you think you need to do this.

Table of Contents
ToC
RSS newsfeedKeep up to date with our RSS newsfeed