Skip to main content

Command Palette

Search for a command to run...

Linux basics - Tutorial 3[redhat]

Updated
7 min readView as Markdown
Linux basics - Tutorial 3[redhat]
P
I am Instructional Designer with interest in Cloud Computing, Devops and Infrastructure management. I also write code to automate the boring stuff.

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.

Screenshot 2026-08-02 at 10.23.46 PM

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.

Screenshot 2026-08-02 at 10.26.33 PM

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.

Screenshot 2026-08-02 at 10.29.52 PM

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.

Screenshot 2026-08-02 at 10.33.48 PM

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.

Screenshot 2026-08-02 at 10.37.25 PM

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.

Screenshot 2026-08-02 at 10.42.33 PM

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.

Screenshot 2026-08-02 at 10.45.22 PM

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.

Screenshot 2026-08-02 at 10.56.23 PM

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.

Screenshot 2026-08-02 at 10.59.45 PM

Move a Directory

mkdir opt
mv copyhere /opt/

If you type this in the terminal you get the output as shown below.

Screenshot 2026-08-02 at 11.01.46 PM Screenshot 2026-08-02 at 11.03.14 PM

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.

Screenshot 2026-08-02 at 11.06.53 PM

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.

Screenshot 2026-08-02 at 11.09.04 PM

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 Copy

  • file.txt → Local file

  • ec2-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. Screenshot 2026-08-02 at 11.29.25 PM

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. Screenshot 2026-08-02 at 11.34.55 PM

Explanation

  • pritam → New owner

  • dancers → 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. Screenshot 2026-08-02 at 11.37.17 PM

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. Screenshot 2026-08-02 at 11.46.10 PM

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. Screenshot 2026-08-02 at 11.48.09 PM

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. Screenshot 2026-08-02 at 11.48.09 PM


Check Detailed File Information

stat file.txt

If you type this in the terminal you get the output as shown below. Screenshot 2026-08-02 at 11.49.48 PM

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 cp

  • Move and rename files using mv

  • Transfer files securely between servers using scp

  • Change file ownership using chown

  • Change group ownership using chgrp

  • Verify ownership using ls -l and stat

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 😀