Products
Services
Pricing Documentation
Servercore
Blog
Computing
Data Storage and Processing
Network Services
Security
Machine Learning and Artificial Intelligence
Home/Blog/Articles/Working with files and directories in Linux

Working with files and directories in Linux

1
Introduction
2
Directory Locations Within the File System
3
Viewing Directory Contents
4
Navigating Between Directories
5
Operations with Files and Directories
6
Editing Files
7
Conclusion
Let's explore how to view available files and directories, use file management commands, and learn basic editing in the "nano" text editor.

Introduction

This beginner’s guide introduces basic commands for managing files and directories in Linux systems.

Requirements

Server with Ubuntu 20.04

All described commands are also applicable in other Linux distributions.

 

Directory Locations Within the File System

Upon logging into the server, you typically enter your account’s home directory, designated for file storage and directory creation.

To determine the current directory’s location, use this command:

pwd

The command output will be as follows:

/home/demo

The home directory follows the user account name; in this example, it is named /demo. This directory is located within /home and at the top level known as the root directory, hence it is represented by a single slash /.

Viewing Directory Contents

Use the ls command to view directory contents. It stands for “list files”.

There are several ways to view the contents of any directory:

ls directory_name

Alternatively, by entering the command:

ls /directory_path

Executing the command displays the files and directories within the specified directory:

dir1 file1  file2  file3

You can add additional flags to the ls command, for example, to display a detailed view (permissions, list of file or folder owners, size, last modified date) of files and directories in the current directory, use the -l flag:

ls -l

Command output:

total 16
drwxr-xr-x 2 home demo 4096 Nov  3 17:40 dir1
-rw-r--r-- 1 home demo   13 Nov  8 17:17 file1
-rw-r--r-- 1 home demo   42 Nov  9 13:04 file2
-rw-r--r-- 1 home demo  42 Nov  10 13:04 file3

To view a list of all files, including hidden files and directories, you can add the -a flag:

ls -a

Command output:

.       dir1        file1           .mysql_history  .ssh
..      .bash_history  file2   file3  .nan

To display the contents of the current directory with symbols indicating type added to the names, use the command:

ls -F

Command output:

dir1/   file1  file2  file3

Navigating Between Directories

The cd command is used to navigate to the home directory. The name of this command is an abbreviation of “change directory”.

The command is used to navigate to the user directory:

cd ~user

To return to the previous directory, use the cd command again.

In Linux systems, every file and directory is located in the topmost directory, known as the “root” and denoted by a single slash /.

The absolute path indicates the location of the directory relative to this top-level directory. This allows directories to be accessed from anywhere in the file system.

Each absolute path must begin with a slash /.

To move up one directory level, use the following command:

cd ..

To move up two directory levels, use this command:

cd ../..

Operations with Files and Directories

Each command has many parameters; to find them out, type the command followed by the -help parameter, for example:

ls --help

Below is a list of basic navigation commands in the Linux console.

Creation

Creating files is done using the following command:

touch имя_файла

Creating directories is executed using a command like:

mkdir имя_директории

The name of this command is an abbreviation of “make directory”.

To create two directories simultaneously:

mkdir   имя_директории_1   имя_директории_2

To create a directory tree, use the following command:

mkdir -p  /имя_директории_1/имя_директории_2

Deletion

The rmdir command is used to delete directories. The name of this command is an abbreviation of “remove directory”.

The rm command is used to delete files. For example, to delete a file named file1, use this command:

rm file1

The rm command also allows you to delete not only files but also directories.

To delete a directory named dir1 along with all its subdirectories and files, use the -r option (from the word recursive):

rm -r dir1

You can delete two directories along with all their contents simultaneously using this command:

rm -r  имя_директории_1   имя_директории_2

You can also use the -f parameter, which means that no confirmation will be requested during deletion.

The command to delete a file will look like this:

rm -f file1

The command to delete a directory is as follows:

rm -rf dir1

Moving

The mv command is used to move and rename files and directories. The name of this command is a contraction of “move”.

You can rename a file using this command:

mv file_name new_name

To move a file, use this command:

mv file_name path/

Copying

The cp command is used for copying. The name of this command is a contraction of “copy”.

To copy file1 and name it file2, use this command:

cp file1 file2

To copy directory dir1 to directory dir2, use this command:

cp -a dir1/ dir2/

To copy a file named file1 into a directory named dir1, use this command:

cp file1 dir1/

Editing Files

We have learned the basic commands for working with directories and files.

Next, we’ll explore how to edit files and add content to them.

The nano command allows you to work in one of Linux’s simplest command-line text editors, occupying the entire terminal for its duration.

Entering nano into the terminal opens a blank file.

The interface appears as follows:

The top part of the opened editor displays the application name and the name of the file being edited.

The middle section should display the file’s contents, which are currently empty.

At the bottom of the interface are hotkeys that serve as the main controls of the text editor. For each, the ^ symbol represents the CTRL key.

Use the CTRL+G combination to open help.

The help closes with the CTRL+X keyboard shortcut. Once the help is closed, we return to editing.

You can type and edit any text, for instance, you can type: “Hello World!”

To save the changes made, press the CTRL+O key combination. Important! This is the letter O, not zero.

Next, the system will prompt you to enter or confirm the name of the file you wish to save:

File Name to Write:                                                            
^G Get Help          M-D DOS Format       M-A Append           M-B Backup File
^C Cancel            M-M Mac Format       M-P Prepend

As you can see, the options at the bottom of the editing window have changed as well. They are contextual, meaning they will change based on what you are attempting to do.

After typing the file name, press the ENTER key.

To exit the text editor, press the CTRL+X combination.

If you have made changes and not saved the file, you will be prompted to save your changes:

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?                         
 Y Yes
 N No           ^C Cancel

You can press the Y key to save changes, the N key to cancel changes and exit, or the CTRL+C key combination to cancel exiting the file editing mode.

If you decide to save your changes, you will be prompted to save them in the same file that was edited.

Press the ENTER key to save the file and exit the editor.

Conclusion

We explored how to view available files and directories, learned basic file commands for viewing, copying, moving, and deleting files, as well as the basics of editing in the nano text editor.

Home/Blog/Articles/Working with files and directories in Linux
Join our Newsletter
We’ll keep you on the loop with everything going on in clouds and servers