Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

File and Directory Operations in Linux

Introduction

File and directory operations are fundamental tasks in any operating system. In Linux, these tasks are accomplished using various commands that allow users to create, delete, move, and manipulate files and directories efficiently. This tutorial covers the most commonly used commands for these operations.

Creating Files and Directories

To create files and directories in Linux, you can use the touch and mkdir commands respectively.

Example

To create a file named example.txt:

touch example.txt

To create a directory named example_dir:

mkdir example_dir

Listing Files and Directories

The ls command is used to list files and directories within the current directory.

Example

To list all files and directories in the current directory:

ls
example.txt
example_dir/

Copying Files and Directories

The cp command is used to copy files and directories. Use the -r option to copy directories recursively.

Example

To copy a file named example.txt to example_copy.txt:

cp example.txt example_copy.txt

To copy a directory named example_dir to example_dir_copy:

cp -r example_dir example_dir_copy

Moving and Renaming Files and Directories

The mv command is used to move or rename files and directories.

Example

To rename a file from example.txt to example_renamed.txt:

mv example.txt example_renamed.txt

To move a file example_renamed.txt to the example_dir directory:

mv example_renamed.txt example_dir/

Deleting Files and Directories

The rm command is used to delete files and directories. Use the -r option to delete directories recursively.

Example

To delete a file named example_copy.txt:

rm example_copy.txt

To delete a directory named example_dir_copy:

rm -r example_dir_copy