Creating files and directories

2.4 Creating, Moving, and Deleting Files (Weight: 2)

📘Linux Essentials (LPI 010-160)


In Linux systems, files and directories are used to store data and organize system resources. System administrators, developers, and users frequently create files and directories when working in the command line.

For the Linux Essentials exam, you must understand:

  • How to create files
  • How to create directories
  • The commands used
  • Important options for those commands
  • How permissions affect file and directory creation
  • How to create multiple items at once

This section explains these topics in clear and simple terms.


1. Creating Files in Linux

A file is a container used to store data such as configuration settings, logs, scripts, or program code.

Examples of files used in IT systems include:

  • Configuration files
  • Log files
  • Script files
  • Application data files

Linux provides several ways to create files from the command line.

The most common methods include:

  • touch
  • Output redirection (>)
  • Text editors

For the Linux Essentials exam, the touch command is the most important method.


2. Creating Files with the touch Command

The touch command is the standard Linux command used to create empty files.

Basic Syntax

touch filename

Example

touch config.txt

This command creates a new empty file named:

config.txt

If the file already exists, touch does not overwrite it. Instead, it updates the file’s timestamp.


File Timestamps

Each Linux file stores timestamps such as:

  • Access time (atime) – last time the file was read
  • Modify time (mtime) – last time the file content changed
  • Change time (ctime) – last time file metadata changed

When touch is used on an existing file, it updates these timestamps.

Example:

touch system.log

If system.log exists, its timestamp is updated.


3. Creating Multiple Files at Once

The touch command can create multiple files in one command.

Syntax

touch file1 file2 file3

Example

touch server.conf database.conf network.conf

This creates three files:

server.conf
database.conf
network.conf

This method is commonly used in IT environments when preparing configuration files.


4. Creating Files in a Specific Directory

Files can also be created inside a specific directory using a path.

Example

touch /home/user/project/app.conf

This creates the file:

app.conf

inside the directory:

/home/user/project

The directory must already exist.


5. Creating Files Using Output Redirection

Linux allows file creation through output redirection.

Syntax

command > filename

Example:

echo "server configuration" > server.conf

This command:

  • Creates the file server.conf
  • Writes text into the file

If the file already exists, the content is overwritten.

This method is often used when generating files through scripts or commands.


6. Creating Directories in Linux

A directory is used to organize files and other directories.

Directories help structure data in the filesystem.

Examples in IT environments include:

/var/log/
/etc/nginx/
/home/user/projects/

Directories are created using the mkdir command.


7. Creating Directories with the mkdir Command

The mkdir (make directory) command creates new directories.

Basic Syntax

mkdir directory_name

Example

mkdir backup

This creates a directory called:

backup

8. Creating Multiple Directories

Multiple directories can be created at once.

Example

mkdir logs configs scripts

This creates three directories:

logs
configs
scripts

This method is commonly used when setting up application folders.


9. Creating Directories in Another Location

Directories can also be created using absolute or relative paths.

Example

mkdir /home/user/project

This creates the directory:

project

inside:

/home/user

Another example:

mkdir /var/backups/system

10. Creating Nested Directories

Sometimes a directory must be created inside another directory that does not yet exist.

Example:

project/app/config

If project and app do not exist, a normal mkdir command will fail.

Example:

mkdir project/app/config

Error:

No such file or directory

11. Creating Parent Directories with mkdir -p

The -p option allows creating multiple directory levels at once.

Syntax

mkdir -p directory_path

Example

mkdir -p project/app/config

This creates:

project
project/app
project/app/config

If some directories already exist, mkdir -p does not produce an error.

This option is frequently used in automation scripts and server setup tasks.


12. Checking Created Files and Directories

After creating files or directories, the ls command can verify them.

Example:

ls

Output:

config.txt logs scripts

To see directories clearly:

ls -l

Directories start with the letter:

d

Example:

drwxr-xr-x  logs

13. Permissions and File Creation

Linux uses permissions to control file creation.

To create files or directories in a location, the user must have write permission for that directory.

Example directory permissions:

drwxr-xr-x

The write permission (w) allows users to:

  • create files
  • delete files
  • rename files

If the user lacks write permission, file creation will fail.

Example error:

Permission denied

14. Important Commands for the Exam

You should know the following commands:

CommandPurpose
touch fileCreate empty file
touch file1 file2Create multiple files
mkdir dirCreate directory
mkdir dir1 dir2Create multiple directories
mkdir -p pathCreate nested directories
lsVerify files and directories

15. Typical IT Environment Usage

In IT environments, these commands are commonly used when:

Preparing application directories

Example:

mkdir -p /opt/app/config

Creating configuration files

Example:

touch /etc/app/app.conf

Creating log file placeholders

Example:

touch /var/log/app.log

Creating development project structure

Example:

mkdir src tests docs

These operations are very common in server setup, DevOps automation, and system administration.


16. Exam Tips (Linux Essentials)

For the exam, remember the following:

  • touch creates empty files
  • touch updates timestamps if file exists
  • mkdir creates directories
  • mkdir -p creates nested directories
  • Multiple files or directories can be created in one command
  • File creation requires write permission in the directory

You should also recognize commands that create files using redirection.


✅ After this topic, the next important section in 2.4 Creating, Moving, and Deleting Files is usually:

  • Copying files and directories (cp)
  • Moving and renaming files (mv)
  • Deleting files and directories (rm, rmdir)
Buy Me a Coffee