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.
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.
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:
Go on to next section, Helpful unix commands to know
Return to Table of Contents