Command Shift

Manipulating Files

As well as manipulating directories, we can also manipulate files.

Creating Files

To create a new file, you can use the touch command, so:

  1. Navigate to your desktop, which is one of the directories of your home folder.
  2. Try touch newFile.
  3. List the contents of the folder.

As an extra, check your desktop directly, which should now have a new file called newFile (original name, we know). If you can’t see it, it might also be because your desktop is full of a thousand files and folders. Time to get your life together and organise them! Jokes aside, let us know if you can’t see your newest creation.

Deleting Files

This is a throwback moment. The command you’ve used to remove a directory and its content, is primarily used to delete a file. In this case, you won’t need the recursive switch, so this should do the job:

rm newFile

A word to the wise - it is very difficult to recover a file deleted using rm (they don't go into the trash, as you might imagine they would) - so be careful when using this one.

Copying Files

Copying files is quite straightforward and we use cp for that.

The cp command takes two parameters: the file to be copied and the new file that will be created from it:

  1. Move to your Desktop, if you’re not already there.
  2. Create a new file called file1.
  3. Run cp file1 file2.
  4. List the content and you should see both files.

You can also use cp to copy a directory and all of its contents, but you have to use an -r switch to do so.

Moving Files

The command to move a file also takes 2 parameters: the file to move and the new location for the file.

  1. Create a new directory called my-directory on your Desktop.
  2. Move the previously created file1 into this new directory by running mv file1 my-directory.
  3. List the content of this new directory without actually moving inside it. Do you remember how?

Renaming Files

Good things come in pairs, or so they say? You can also rename a file using the mv command. If the second parameter you give is a directory, it will move the file into that directory, but if the path doesn't exist, then it will move the file to that location with that name:

  1. Move into my-directory.
  2. Run mv file1 renamedFile.
  3. List all the contents to do a quick check.

Because renamedFile isn’t an actual subdirectory of the my-directory folder, it renames file1 to renamedFile instead. Helpful and easy to remember.

On this page