Linux 101 - Performing file operations in the Terminal

Introduction

In the previous section we explored commands in the terminal to navigate the Linux file system. In this section you will learn the commands required to make changes within that file system. In other words, how to perform file and directory input/output (IO) operations.

Create directories using mkdir

Good organization of a user's workspace requires the user to be able to create directories. For example, a user might group all files together in directories according to experiment title. The command mkdir enables users to create new directories in the Linux file system.

Common Use

Basic Syntax

$ mkdir *directory_path*

Example

Create a directory named DataFiles in the home directory (~):

io_linux_1

Note: An error message will be printed to the terminal if the directory already exists in the path given:

io_linux_2

Note: To create multiple directories, simply supply multiple directory paths and separate each with a space.

Create sub-directories

In many cases it might be necessary to create a new directory with a sub-directory. Adding the -p flag to mkdir will create all directories in the given path if they do not exist yet.

Basic Syntax

$ mkdir -p *new-directory/new-sub-directory*

Example

Create the directories:

  • DataFiles
  • DataFiles/sub-data
  • DataFiles/sub-data/sub-sub-data

io_linux_3

Note: The command, tree, used in the example is a program which needs to be installed manually (thus also why the location in this example is not the login node of the UFS HPC).

Create files using touch

There are multiple ways to create files in Linux (See the File editor section). To simply create an empty text file in the terminal the command touch can be used.

Common Use

Basic Syntax

$ touch *file_path*

Example

Create the file named newfile in pwd:

io_linux_4

Note: Using touch on a file or directory that already exists, modifies the access and/or modification time stamp (to the time at which the command was issued).

Copy files and directories using cp

Copying files is frequently used file and directory operation on any computer system. In the terminal, copying files and directories are accomplished by using the cp command

Common Use

Basic Syntax

$ cp *source path*  *destination path*

Example

Copy the file named newfile in the pwd to the directory cp1:

io_linux_5

Note: To copy multiple files to the same destination, provide the file paths for the files to be copied, separated with a space, and provide the destination path as the last entry to the command.

Copy a non-empty directory and all its contents

Basic Syntax

Copying a directory, with all its sub-folders and files, isn't completely straight forward in the terminal. To accomplish this operation, the recursive flag, -r, must be used:

$ cp -r *source path*  *destination path*

Example

Copy the directory named cp1 and its contents to the directory cp2:

io_linux_6

Move files and directories using mv

Moving files and directories functions similarly to the cp command. These actions are accomplished by using the mv command

Common Use

Basic Syntax

$ mv *source path*  *destination path*

Example

Copy the file named newfile in the pwd to the directory mv1:

io_linux_7

Example

Move the directory mv1 and its contents the the backups directory:

io_linux_8

Rename files and directories using mv

Linux utilizes the mv command to rename files and directories. The rename command in the terminal is simply a shell alias for the mv command.

Common Use

Basic Syntax

$ mv *original path*  *renamed path*

Example

Rename the file newfile to renamedfile:

io_linux_9

Rename the directory mv1 to mvrenamed:

io_linux_10

Delete files and directories with rm

Removing files and directories is a type of file operation that is common in all computing. In terminal these actions can be performed using the rm

A word of caution: Always be careful with this command, especially when pairing it with the -r flag. Recursive deletion can be disastrous when applied incorrectly, thus always double check any rm command.

Delete files using rm

Basic Syntax

$ rm *file path(s)*

Example

Delete the file named newfile in the pwd:

io_linux_11

Delete an empty directory using rm

To delete empty directories, add the -d flag to the command:

Basic Syntax

$ rm -d *some empty directory*

Example

Delete the empty directory, someemptydirectory:

io_linux_12

Delete a directory and its contents using rm

To delete directories with their contents, the -d flag must be used with the recursive flag, -r:

$ rm -dr *some directory*

Example

Delete the non-empty directy, somedirectory:

io_linux_13

Outcomes for this section

After completion of this section, you need to be able to do the following tasks in a Linux terminal:

  • Create files and directories
  • Copy files and directories
  • Move and rename files and directories
  • Delete files and directories