Introduction
This article provides a comprehensive guide on creating directories and files, as well as deleting them in Linux using command-line tools. We'll cover the essential commands: mkdir, touch, cat, and rm, along with their various options and use cases.

Index

  • Creating Directories with mkdir
  • Creating Files with touch
  • Working with Files using cat
  • Removing Files and Directories with rm
  1. Creating Directories with mkdir Use Case: Setting up a project structure for a new web application The mkdir command is used to create directories (folders) in Linux.

How to Use mkdir
Create a single directory in / (Top level directory):

mkdir /dir (name of the directory)

ls /

Create more than one directory:

mkdir /dir1 /dir2 /dir3

Create multiple directories:

mkdir /dir{1..10} or # mkdir /dir{a..f}

Syntax
mkdir -p (parental)

Create a directory inside a directory (parent/permissive):

mkdir

  1. Creating Files with touch Use Case: Initializing configuration files for a software application The touch command creates empty files or updates timestamps of existing files.

How to Use touch

Create a single file:

touch /appv1.txt

Create more than one file:

touch /appv1.txt /appv2.txt /appv3.txt

Create multiple files:

touch /appv{4..8}.txt

  1. Working with Files using cat Use Case: Creating and managing log files for system monitoring The cat command is versatile, allowing you to create, write to, read, and append to files. How to Use cat The cat command does not support editing a file in the middle or the beginning of a script/text.

Syntax

cat

Options:

write/create file

Append file (add text to the current following text)

Reading a file does not require any option
< read file (default) [not necessary to use]

Examples
Create and write to a file:

cat > Linux.txt [Press Enter]

“Linux is a super powerful server
It is open source and very secure”

Press (Ctrl+d+d to save and exit)

Append to a file:

cat >> /Linux.txt

“Linux is a super powerful server
It is open source and very secure
Linux is lightweight and 90% used more than other servers in the industry”

Press (Ctrl+d+d to save and exit)

To read a file:

cat < /Linux.txt or cat /Linux.txt (I prefer this one)

  1. Removing Files and Directories with rm Use Case: Cleaning up temporary files and outdated project directories The rm command is used to remove (delete) files and directories.

How to Use rm
Syntax

rm

Options:
-r (recursive) Used for deleting directories and their contents
-v (verbose) [acknowledgement]
-f (forcefully) Deletes without prompting.

Examples
How to delete a single file:

rm -rvf /note.txt

Delete multiple specific files:

rm -rvf /note.txt /note1.txt /note3.txt

How to Delete multiple files in sequence:

rm -rvf /note{1..5}.txt

How to delete a single directory:

rm -rvf /directory1

How to delete multiple directories:

rm -rvf /directory1 /directory2 /directory3

How to delete multiple directories in sequence:

rm -rvf /directory{a..f}

How to delete all directories matching a specific pattern:

rm -rvf /dirpattern*

How to delete all directories (and files) in the current directory:

rm -rvf /*

Warning: Be extremely cautious when using rm, especially with wildcards or the -rf options, as it permanently deletes files and directories without possibility of recovery.

Summary
This guide covered essential Linux commands for file and directory management. The mkdir command creates directories, touch creates empty files, cat allows for file creation and manipulation, and rm removes files and directories. These tools are fundamental for efficient file system organization and maintenance in Linux environments. Remember to use these commands carefully, especially rm, to avoid unintended data loss.

Linux #Automation #CloudComputing #DevOps #AWS #RedHat #30DaysLinuxChallenge #DevOps #AWS #CloudEngineer

Let's connect on LinkedIn
www.linkedin.com/in/alex-enson-a6692815a