USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Creating, Copying, and Moving Files and Directories

Lesson 10/49 | Study Time: 20 Min

Managing files and directories efficiently is a fundamental task in Linux. Knowing how to create, copy, and move files and directories allows users to organize data, back up information, and streamline workflows. Linux provides straightforward yet powerful commands to perform these actions in both simple and complex scenarios, including handling entire directory structures recursively. 

Creating Files and Directories

Linux provides simple yet powerful commands to create files and directory structures. Following are the commonly used methods for file and directory creation.


1. Creating Files: The touch command is the standard way to create empty files or update the timestamp of existing files.

text
touch filename.txt

This creates a new file called filename.txt in the current directory if it doesn’t exist.


2. Creating Directories: Use the mkdir command to create new directories.

text
mkdir dirname


Example:

text
mkdir Projects

creates a directory named Projects.


3. Creating Nested Directories: With the -p option, mkdir will create parent directories as needed:

text
mkdir -p /path/to/new/directory

This is useful when multiple levels don’t exist.

Copying Files and Directories

Copying files and directories is a common task when managing data in Linux systems. Below are the different ways to copy single files, multiple files, and entire directories.


1. Copying Files: The cp command copies files from a source to a destination.

text
cp source_file destination_path


Example:

text
cp report.txt /home/user/Documents/

copies report.txt to the Documents directory.


2. Copying Multiple Files:

text
cp file1.txt file2.txt destination_directory/

copies both file1.txt and file2.txt to the specified directory.


3. Copying Directories: Use the -r or -R option to copy directories recursively (including all files and subdirectories).

text
cp -r sourcedir destinationdir

If the destination directory does not exist, cp will create it.


4. Preserving Attributes: Use -a option to preserve attributes like timestamps, links, and permissions.

Moving and Renaming Files and Directories

The command line enables quick movement and renaming of files and folders. Following are commonly used mv command patterns for these tasks.


1. Moving Files or Directories: The mv command moves files or directories from one location to another.

text
mv source destination


Example:

text
mv report.txt /home/user/Documents/

moves report.txt to Documents.


2. Renaming Files or Directories: Since moving within the same directory with a new name renames the file, mv doubles as a rename command.

text
mv oldname.txt newname.txt

renames the file oldname.txt to newname.txt.


3. Moving Multiple Files:

text
mv file1.txt file2.txt directory/


4. Renaming Directories:

text
mv old_directory new_directory

Practical Examples and Tips


1. To copy a directory and all its content at once:

text
cp -r /source/dir /destination/


2. To move a file and rename it simultaneously:

text
mv /path/oldname.txt /newpath/newname.txt


3. To ensure that directories exist before copying:

text
mkdir -p /destination/path && cp -r /source/path /destination/path


4. Use cp -i and mv -i to prompt before overwriting files.

Samuel Wilson

Samuel Wilson

Product Designer
Profile

Class Sessions

1- What is Linux and Operating System Concepts 2- Linux History and Evolution 3- Linux Distributions and Their Purposes 4- Open Source Software and Licensing 5- Graphical User Interface (GUI) and Desktop Environments 6- Terminal Access and Command-Line Fundamentals 7- Getting Help and Command Documentation 8- File System Hierarchy and Directory Structure 9- Navigating Directories and Listing Contents 10- Creating, Copying, and Moving Files and Directories 11- Deleting Files and Directories 12- Symbolic and Hard Links 13- Understanding File Permissions Model 14- Modifying Permissions and Ownership 15- User and Group Management 16- Sudo and Privilege Escalation 17- Text Searching and Pattern Matching 18- Text Processing and Stream Editing 19- Compressing and Archiving Files 20- Text Editing and File Creation 21- Package Management Systems Overview 22- Installing and Updating Software with APT 23- Installing and Updating Software with YUM/DNF 24- Managing Software from Non-Repository Sources 25- Understanding Processes and Process Management 26- Viewing Running Processes 27- Process Control and Termination 28- Task Scheduling with Cron 29- Networking Concepts and IP Addressing 30- Viewing and Configuring Network Interfaces 31- Basic Network Troubleshooting 32- Shell Script Basics 33- Variables and Data Types 34- Conditional Logic in Scripts 35- Loops and Iteration 36- Functions and Code Reuse 37- Input/Output and User Interaction 38- System Authentication and Access Control 39- File System Security 40- Software Updates and Patching 41- Basic Firewall Concepts 42- System Information and Monitoring 43- Service and Daemon Management 44- System Boot Process and Runlevels 45- System Backup and Disaster Recovery 46- Comprehensive File System Management 47- System Automation Workflows 48- Multi-Concept Troubleshooting Scenarios 49- Continued Learning Pathways

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.