User Tools

Site Tools


wiki:cpmv

Moving and copying files and directories (chapter 4)

First, go into the cs580 directory we created last time. Before we start moving and copying files we should create one. To do that we will use the command-line text editor nano, which is standard on any Linux/Unix system. Let's create a new file called my_new_file.txt:

$ nano my_new_file.txt

Type some stuff and then save the file and exit the program.

We can view the contents of the file without having to invoke the editor by using the more or less commands:

$ more my_new_file.txt

Note that less is more powerful than more and has a few additional features.

Now, to demonstrate why it's not a good idea to use spaces in file names, try the following:

$ nano my new file.txt

And type ctrl-x and observe what happens.

Today we are going to use the following commands:

  • cp - copy files and directories
  • mv - move/rename files and directories
  • mkdir - create a directory
  • rmdir - remove an empty directory
  • rm - remove files/directories
Copying files

We already started using mkdir and rmdir, so we will focus on the three others. And let's start with cp. There are two forms for the cp command:

  cp source_file target_file
  cp source_file ... target_directory

In the first form, cp copies the contents of the source_file to the target_file. For example:

$ cp my_new_file.txt another_new_file.txt

or

$ cp my_new_file.txt ../yet_another_new_file.txt

which copies the file under a new name into the parent directory of your current working directory.

The second form of cp copies each each source_file to the destination target_directory. The names of the files themselves are not changed. If cp detects an attempt to copy a file to itself, the copy will fail. Now let's see some examples:

$ cp my_new_file.txt /home/asa

which can also be written as cp my_new_file.txt ~.

You can also copy a file to the current working directory, abbreviated as .:

$ cp ../yet_another_new_file.txt .

Using this form of the command you can copy multiple files at the same time:

$ cp my_new_file.txt another_new_file.txt /home/asa

cp has a lot of options. Let's look at one of them - the -R option. Try the following: in your home directory type the command

$ cp -R cs580 copy_of_cs580

What did it do?

Removing files/directories

Time to clean up a bit … Let's remove the files from the copy_of_cs580 directory and the directory itself. We'll do that with the rm command. You can remove individual files and then the directory itself:

$ rm copy_of_cs580/my_new_file.txt
$ rmdir copy_of_cs580

or you can use the -R option of rm:

$ rm -R copy_of_cs580

Be very careful with “rm”. Linux does not have a way of undoing its action. As a way of addressing that, it is not a bad idea to use the -i option of rm, which will ask you to verify each removed file/directory. It can be combined with other options, e.g.:

$ rm -Ri copy_of_cs580

Renaming/moving files/directories

Like the cp command, the mv command has two general forms:

  mv source target
  mv source ... directory

Let's look at an example of the first one:

$ mv my_new_file.txt new_file.txt

And the second one:

$ mv new_file ..

The first one changes the name of the file, while the second moves the file to a different directory.

While you can do all these things quite easily with a graphical file manager, doing it from the command line provides extra flexibility. This flexibility comes from the use of wildcards.

Wildcards

Wildcards enable you to select multiple files/directories at once. The most commonly used wildcard is the *, which matches any characters. So, if we would like to see all the names of all files which end with .txt we would issue the following ls command:

$ ls *.txt

and

$ ls m*.txt

will list all files whose names begin with m and end with .txt.

Wildcards can be used in conjunction with any of the commands we looked at. For example:

$ cp directory1/* directory2

will copy all the files from directory1 to directory2.

There are many more wild cards that are recognized. For example ? matches a single character, so data??? will match file names that start with data and are followed by any three characters. Chapter 4 of the book provides more information on this topic.

Be especially careful when using wildcards with rm: first use the ls command with the wildcard to verify which files will be affected. Consider for example the following command:

rm * .html 

It will remove all your files (and then complain that the file .html does not exist).

wiki/cpmv.txt · Last modified: 2016/08/02 14:47 (external edit)