NC State Computing
Center

Files in Unix

There will be times when you need to copy, move, rename or remove files. We defined files earlier, but as a quick review: a filename can be up to 256 characters and can have multiple levels, each separated by a dot or underscore (whatever works best for you). Keep in mind, though, that some last level names are reserved and have a meaning in unix. For example, a ".f" ending references a FORTRAN source code file, a ".c" represents a source code in C and compressed files have a ".Z".

The cp or copy command

The cp command allows you to make a copy of a file and place the copy in a specified location, while the original file remains in its same location.

Make sure that you are in your home directory (cd). We already have two files created, test1 and file2_test. We will copy the file test1 and place it in the directory called data.

The format is cp file_name directory_name.

	unity%cp test1 data [return]
	unity%cd data [return]
	unity%ls [return]
	test1
	unity%
We could also have done a directory listing of all files in the "data" directory by doing an ls -l data. We now have two copies of the file test1.

You can use the wildcard character, the "*", to copy groups of like files. For example, if you had three files called "test1", "test2" and "test3", you could enter the following command to copy all three files to the "data" directory:

 
	unity% cp test* data [return]
The above command would copy those three test files (test1, test2, test3) into the specified directory. Of course, if you had more files than those three that began with "test", it would have copied those as well (e.g.,: tested, testy).

You can also use the copy command to make a copy of a file and leave the copy in the same directory. For example, you could copy a file called "test1" to another file called "final.copy"by entering

cp test1 final.copy .

This would copy the contents of "test1" into "final.copy" and both would be in the same directory. Because it found no directory with the name of the final.copy, it assumed you wished to make another copy of the same file with a different name. In the earlier examples, because it found a directory called "data", it copied the specified file there, leaving the file name the same.

The mv or move command

The mv command works similar to the copy command, but instead of placing a copy of the original file in the specified directory, it physically moves the specified file to the destination directory and the file is no longer in the original directory.

We can move the file "file2_test" to the "data" directory; make sure you are in your home directory (notice that we do a listing of the files in the "data" directory without actually changing into it).

	unity% cd [return]
	unity% mv file2_test data [return]
	unity% ls -l data [return]
	total 1
	-rw-r--r-- 1 unix1 0 Feb 8 10:32 test1
	-rw-r--r-- 1 unix1 0 Feb 8 10:31 file2_test
	unity%
You can also use the "mv" command to rename a file. To rename the file "file2_test" to "example2", you should first change into the "data" directory where the file is located, and then enter

"mv file2_test example2":

	unity% cd data [return]
	unity%  mv file2_test example2 [return]
	unity%
Again, the system does not tell you it has performed this action, but if you do an "ls", you will see that the file has been renamed. If you don't receive an error message, you can usually be sure that your command or action has been performed.

The rm or remove command

To delete or remove a file, use the rm, command. It works the same way as when we deleted directories on Unity, the rm command is aliased to the rm -i command, where the -i prompts you for confirmation before deleting the specified file. This is for your protection so that you don't accidentally delete the wrong file.

Since we copied the file test1 from our home directory to the data directory, we can safely remove it from our home directory. First, we need to make sure that we are in the correct directory (e.g., our home directory), since that is where the file is that we wish to delete.

	unity% cd [return]
	unity% rm test1 [return]
	rm: remove test1? y [return]
	unity%
Below are other options that can be used with the rm command for removing files or directories: Be careful when you use the "-f" and/or "-r" options.

Go on to next section, Helpful unix commands to know

Return to Table of Contents