Linux basics - Tutorial 3[redhat]
![Linux basics - Tutorial 3[redhat]](https://cdn.hashnode.com/uploads/covers/62db945e38759e6b49829749/11761442-25aa-407d-a3d2-960252bf2f09.png)
Redhat Linux Basics - Tutorial 3.
Managing files and directories is one of the most common tasks performed by Linux administrators and DevOps engineers. Whether you're deploying applications, backing up configuration files, or transferring data between servers, these commands are used every day.
In this tutorial, you'll learn how to:
Copy files and directories
Move files and directories
Rename files and directories
Securely copy files between servers using SCP
Change file ownership
Change group ownership
Verify ownership changes
Copy a File
The cp command is used to copy files from one location to another.
Syntax
cp source_file destination
Example
cp file1.txt backup.txt
If you type this in the terminal you get the output as shown below.
Explanation
cp→ Copies files.file1.txt→ Source file.backup.txt→ New copied file.
Copying the backup.txt to a directory called hello
mkdir hello
cp file1.txt hello
cd hello
ls
If you type this in the terminal you get the output as shown below.
Copy Multiple Files to a folder
touch file3.txt file4.txt file5.txt
mkdir database
cp file3.txt file4.txt file5.txt database
cd database
ll
If you type this in the terminal you get the output as shown below.
Explanation
Copies multiple files into the specified directory.
Copy a Directory.
We will make a new directory called backup_project and copy databse directory with all the files and folders present inside it.
Directories require the -r (recursive) option.
mkdir backup_project
cp -r database backup_project
cd backup_project
ll
pwd
cd database
ll
If you type this in the terminal you get the output as shown below.
Explanation
-r→ Copies the entire directory, including all files and subdirectories.
Preserve Original File Attributes
cp -p file1.txt javascript.txt
If you type this in the terminal you get the output as shown below.
Explanation
Preserves:
Ownership
Permissions
Timestamp
Note: Useful when taking backups.
Interactive Copy
cp -i file3.txt coding.txt
If you type this in the terminal you get the output as shown below.
Explanation
Prompts before overwriting an existing file.
Verbose Copy
cp -v file1.txt demo.txt
If you type this in the terminal you get the output as shown below.
Explanation
Displays every file being copied.
Move a File
The mv command moves files from one location to another.
Syntax
mv source destination
Example
mkdir copyhere
mv file1.txt copyhere
cd copyhere
ls
If you type this in the terminal you get the output as shown below.
Explanation
Moves the file to the copyhere directory.
Move Multiple Files to a Directory
mkdir backup
mv file3.txt file4.txt file5.txt backup/
cd backup
ll
If you type this in the terminal you get the output as shown below.
Move a Directory
mkdir opt
mv copyhere /opt/
If you type this in the terminal you get the output as shown below.
Note: Moves the entire directory.
Rename a File
Linux uses the mv command to rename files.
mv demo.txt newname.txt
If you type this in the terminal you get the output as shown below.
Explanation
Renames the file without changing its contents.
Rename a Directory
mv database old_project
If you type this in the terminal you get the output as shown below.
Note: Renames the directory.
Secure Copy (SCP)
The scp command securely copies files between Linux servers using SSH.
It encrypts the data during transfer.
Copy Local File to Remote Server
scp file.txt ec2-user@<public-ip-of-vm>:/home/ec2-user/
Explanation
scp→ Secure Copyfile.txt→ Local fileec2-user→ Remote username<public-ip-of-vm>→ Remote server IP/home/ec2-user/→ Destination directory
Copy Remote File to Local Machine
scp ec2-user@<public-ip-of-vm>:/home/ec2-user/file.txt .
Explanation
Downloads the remote file into the current directory.
Copy a Directory Using SCP
scp -r project ec2-user@<public-ip-of-vm>:/home/ec2-user/
Explanation
-r→ Copies the entire directory recursively.
Use a Private Key
scp -i mykey.pem <file-name> ec2-user@<public-ip-of-vm>:/home/ec2-user/
Explanation
Uses the specified private key for authentication.
Change File Owner
The chown command changes the owner of a file or directory.
Syntax
sudo chown owner filename
Example
sudo chown pritam lola
If you type this in the terminal you get the output as shown below.

Explanation
Changes the owner of the folder to pritam.
Change Owner and Group
chown pritam:dancers unhappyuser
If you type this in the terminal you get the output as shown below.

Explanation
pritam→ New ownerdancers→ New group
Change Ownership Recursively
sudo chown -R hitesh:testers unhappyuser
If you type this in the terminal you get the output as shown below.

Explanation
-R→ Applies ownership changes to all files and subdirectories.
Change Group Ownership
The chgrp command changes only the group ownership.
sudo chgrp devops file.txt
If you type this in the terminal you get the output as shown below.

Explanation
Changes the file's group without affecting its owner.
Change Group Recursively
sudo chgrp -R devops project/
If you type this in the terminal you get the output as shown below.

Changes the group ownership of the directory and all its contents.
Verify Ownership
ls -l
If you type this in the terminal you get the output as shown below.

Check Detailed File Information
stat file.txt
If you type this in the terminal you get the output as shown below.

Explanation
Displays:
Owner
Group
Permissions
Inode
Access Time
Modify Time
Change Time
Practical DevOps Lab
Create Project Directory
mkdir project
Create Sample Files
touch project/app.py
touch project/config.yaml
touch project/requirements.txt
Copy the Project
cp -r project project_backup
Rename the Backup
mv project_backup project_backup_v1
Move the Backup
mv project_backup_v1 /tmp/
Change Ownership
sudo chown -R developer:devops project
Verify Ownership
ls -l project
Transfer Project to Another Linux Server
scp -r project ec2-user@192.168.1.10:/home/ec2-user/
Summary
In this tutorial, you learned how to:
Copy files and directories using
cpMove and rename files using
mvTransfer files securely between servers using
scpChange file ownership using
chownChange group ownership using
chgrpVerify ownership using
ls -landstat
These commands are fundamental for Linux administration and are used daily by DevOps engineers for deployments, backups, server migrations, and managing application files.
With this we come to the end of the tutorial 3 of Linux. Thank you for going through this. See you all in part four.. Until then Happy Learning 😀
![Linux basics - Tutorial 4[redhat]](https://cdn.hashnode.com/uploads/covers/62db945e38759e6b49829749/de009029-4fed-41f7-b9b2-08d02d7495fb.png)
![Linux basics - Tutorial 2[redhat]](https://cdn.hashnode.com/uploads/covers/62db945e38759e6b49829749/9f677207-0a84-499c-9d83-159fb45d2544.png)
![Linux basics - Tutorial 1[redhat]](https://cdn.hashnode.com/uploads/covers/62db945e38759e6b49829749/a932f151-8a70-4467-8c91-cdeecdf3c668.png)
