Skip to main content Skip to navigation

3.1.1 Manipulating files

1. Copying files

One of the first things you might like to do with a file is copy it. Say you have a file called input, and you would like to copy this file to another name, input-May05. The way to do this is to use the cp command. In this example, you would type

 cp input input-May05

Putting it into Practice:

Using emacs, create a file called input . Just type a few words in the file, it doesn't actually matter what.
Then copy this file to input-week1
Now type ls -lrt . You should see two files, input and input-week1

You can also copy the contents of directories, using a recursive version of copy. To copy recursively, use
cp -R fromfile_or_dir tofile_or_dir

You can also copy files from locations that are not your current working directory. Just type in the path name (relative or absolute) of the file you wish to copy . If you want to copy this file to your current working directory, AND you want this file to keep the same name, then just type a full stop for the destination file name.

Example:

The user harry would like to copy the file input-May05 from ~phillipa/gaussian/ , and he would like the filename to stay the same. He also wishes to copy this file to his current working directory. He does so by typing
 cp ~phillipa/gaussian/input-May05 . 

Back to top

2. Deleting files

Use the rm command to remove single files, rm -r to remove entire directories (and subdirectories within). It goes without saying that you must be careful using rm -r ! If files have special permissions, on some systems you may not be able to delete them unless you use rm -f . Again, be careful with this command.

Example:

The user phillipa would like to remove the file input-May05 . She does so by typing
 rm input-May05 
Now phillipa would like to remove the entire directory junk and ALL the files within this directory. She does so by typing
 rm -r junk 


Back to top

3. Moving files

Sometimes you might wish to simply rename a file, rather than copy a file. This is the same as moving a file. You can think of moving a flie as being like copying a file, where the original file gets removed. This can be done using the mv command.

Example:

The user phillipa would like to move the file input to input-May05 instead. She does so by typing
 mv input input-May05 
The file input will no longer exist on the filesystem. The contents of input can now be found in the file input-May05. Note that input-May05 can be an existing file - the original file will then be overwritten without any warning. If the file input-May05 did not exist before phillipa typed this command, it would be created.


Back to top

4. Creating new directories

You already know how to create a new file using emacs. It's always a good idea to have a filing system implemented on your filespace - so you can find files quickly. The way to do this is to create directories (and sub-directories) in your filespace. This is done with the mkdir command (from make directory). Say you would like a directory, called PDFs, to store your pdf files downloaded from scientific journals. Then you would type

 mkdir PDFs 

Putting it into Practice:

Create a directory called PDFs in your home directory. Confirm that PDFs is a directory (how will you do this?).

Putting it into Practice:

Can you move this directory ( PDFs) to journals using the mv command?


Back to top

5. Moving around your filespace

Now that you know how to make directories, you'll also need to know how to move around in your home filespace. We call this changing directories and the command for this is cd . The general syntax for this command is

cd pathname

where pathname is your target location (where you'd like to get to in the tree).

If you omit the pathname, you will be taken to your home directory. So, just typing cd is the fastest way to get back to your home directory. Otherwise the path can be of absolute or relative form (including use of .. for moving up the tree).


Back to top