2.5.3. Making, copying, moving and removing files#

2.5.3.1. Making and editing files#

We talked a lot about files, but you have so far only looked at files that already existed.

2.5.3.1.1. Text editor#

The easiest way to create a new file, or edit an existing fie, is of course to use a text editor from the desktop environment, such as gedit. However, sometimes this won’t be available (for example, if you’re working over a network connection via a secure shell).

In Ubuntu, the easiest to learn text-mode editor that’s available is called nano[1]. It helpfully prints a list of commands at the bottom of the screen, see Fig. 2.6. Note that ^A is a standard way of describing the keyboard shortcut Ctrl-A. For example, Ctrl-G (^G) will open a help menu.

../../_images/nano_editor.png

Fig. 2.6 Screenshot of editing a file from the terminal using the nano editor.#

Exercise 2.63

Use nano to create a new file containing the text ā€œI swallowed a bug.ā€. Save it under the name river.txt.

Exercise 2.64

Open a file in nano, and look through its built-in help menu for the keyboard shortcut to enable line numbering. When this option is enabled, you should see the number of each line in a white bar on the left.

2.5.3.1.2. Creating an empty file with one command, touch#

If you just want to create an empty file, you can also do this in one go with touch:

$ touch testfile.txt

This creates an empty file testfile.txt if it didn’t exist yet, without you needing to open and close an editor. This command can be helpful when you quickly want to create some dummy files to try out copying, removing and other file operations throughout this manual.

However, the main use of touch is not just to create a file, but to update an existing (non-empty) file’s modification time to the current clock. The file system logs for each file when it was first created, but also when it was last modified. For example, with ls -t you can lists your files sorted by modification time, making it easy to find recently updated files in a big directory. The file system automatically changes the modification time every time a file is saved, but sometimes you want to update a file’s modification time without actually modifying its content. This can be done by using touch on the file.

Exercise 2.65

Create two empty files A and B using touch:

touch fileA.txt
touch fileB.txt

Now list the files sorted by time using ls -t. Which file shows up first in this order?

Now run touch fileA.txt again, and check the output of ls -t. How did the order change?

You can also run touch river.txt to update the modification time of the file from the previous exercise. Check the file order of ls -t, and also confirm (with cat, less or nano) that the content of the file was left unaltered.

2.5.3.1.3. Downloading files with wget#

Another option is to download a file from an URL. This can be done from the terminal using wget [URL], where you replace [URL] with a direct link to the file that you want to download. By default, wget will then try to download the file, and save it with the same name in your current working directory (of course, wget has many more options, as you can find in its manual).

For example, the following will download a README.md from the internet, and save it as README.md in your local directory.

$ wget https://raw.githubusercontent.com/ros2/ros2_documentation/refs/heads/rolling/README.md

You should now be able to see the README.md file listed in your current directory, and be able to inspect it

$ ls
$ less README.md

Important

Downloading will, of course, only work when your Linux system is connected to the internet. If you do not have a working network connection, you will get an error from wget that it could not find the server.

Exercise 2.66

The wget command is also often used with the -O option. What will the following command do?

$ wget -O test.md https://raw.githubusercontent.com/ros2/ros2_documentation/refs/heads/rolling/README.md

Verify by checking the content of the current working directory.

2.5.3.2. Copying files#

To copy files, you can use the command cp. It’s quite simple:

$ cp originalfile copiedfile

will create a file copiedfile (if it’s not already there) and copy the contents of originalfile into it. You can specify more than one file to be copied, but then the last name you specify should be a directory, for example:

$ cp file1 file2 /tmp

Exercise 2.67

Copy your file myfile.txt to anotherfile.txt. Copy both files to /tmp. Verify this using ls /tmp.

If the file you copy to already exists, cp will not ask you for confirmation before it overwrites it. Be careful!

You can also specify entire directories to be copied, using the -r (recursive) option:

$ cp -r somedirectory anotherdirectory

This will create a directory somedirectory in the directory anotherdirectory and copy all files and directories below somedirectory there.

Note

When working on a remote computer, as discussed in Section 2.4.8, it can be very useful to transfer files from one computer to the other. Instead of just cp, the command that accomplishes this is called scp, which securely copies files over the network. It works similar to the cp command, but needs some more information about server. A typical invocation looks like this:

$ scp originalfile user@host:copiedfile

where user is your username on the remote server, and host is its hostname or IP address. As with cp, you can specify a directory as the copiedfile, and the -r option works as well. As a convenience, you can omit copiedfile, in which case the original will be placed in your home directory on the server.

Exercise 2.68

Copy myfile.txt to the Student Linux Bastion server and verify that it arrived by logging in remotely and using the cat command. Also try to copy a file (say, /etc/redhat-release) from the server to your computer. You’ll need to do this locally, because the server doesn’t allow any outgoing connections; in this case, the user@host: specification needs to be given for the originalfile instead of the copiedfile.

2.5.3.3. Moving and renaming files#

Another option is to rename or move the file. In Unix , both these file operations are the same: if you move a file to another directory, all you do is give it another name. The mv command moves files and can be called just like cp.

Exercise 2.69

In the previous exercise, you created a copy of myfile.txt called anotherfile.txt. Rename this file to renamedfile.txt using mv.

Warning

Be careful when renaming files that the last argument that you give to mv, the intended new file name, is not an already existing file. If a file with that new name already exists, the existing file will be replaced without any confirmation, and its content will be lost.

To move one or more files into a directory, give mv all the file names you want to move as arguments, and the target directory as last argument. Note that this can lead to subtly different behaviors

  • mv foo.txt test: If test/ is an existing subdirectory, this will move the file foo.txt into that subdirectory

  • mv foo.txt test: If test is an existing file, it will rename foo.txt to this file name, losing the original file content.

  • mv foo.txt test: If test does not exist yet, it will just rename foo.txt to this file name.

  • mv foo.txt bar.txt test: This will attempt to move two files, foo.txt and bar.txt into a subdirectory test/ if it exists. If test does not exist or is a file, mv will give an error because with more than two arguments it understands you are trying to move files, not rename them.

Exercise 2.70

Move the file renamedfile.txt into to /tmp (a special subdirectory inside the root directory / that can be used for temporary files). Verify that the file has really moved from your current directory and to /tmp using ls and ls /tmp.

We will discuss mv for directories later in Section 2.5.4.2.

2.5.3.4. Removing files#

Finally, you will often want to remove files once you don’t need them anymore. The rm command does this for you. Be careful: if you remove a file in Unix , there’s no recycle bin, so there’s no way of getting it back!

Exercise 2.71

Use rm to remove the file you just moved, /tmp/renamedfile.txt. Verify using ls /tmp.

Like cp, you can use rm recursively: rm -R. Be very careful with this option! You can throw away things you didn’t really want to[2].

2.5.3.5. Common options for cp, mv and rm#

The cp, mv and rm commands have two options in common:

  • -i: if specified, always ask for confirmation when overwriting or removing files;

  • -f: if specified, never ask for confirmation when overwriting or removing files. Especially the -f option can be useful, but also very dangerous, as you can imagine…