LFCA: Learn Basic File Management Commands in Linux – Part 2

This article is Part 2 of the LFCA series, here in this part, we will explain about Linux file system and cover the basic file management commands, that are required for the LFCA certification exam.

As you get started out in Linux, you’ll spend a great deal of time interacting with files and directories. Directories are also known as folders, and they are organized in a hierarchical structure.

In the Linux operating system, each entity is regarded as a file. In fact, there’s a popular statement in Linux circles that goes: ‘Everything is a file in Linux’. This is just an oversimplification and in the real sense, most files in Linux are special files that include symbolic links, block files, and so on.

Linux File System Overview

Let’s take a moment and have an overview of the main file types:

1. Regular Files

These are the most common file types. Regular files contain human-readable text, program instructions, and ASCII characters.

Examples of regular files include:

  • Simple text files, pdf files
  • Multimedia files such as image, music, and video files
  • Binary files
  • Zipped or compressed files

And so much more.

2. Special Files

These are files that represent physical devices such as mounted volumes, printers, CD drives, and any I/O ) input and output device.

3. Directories

A directory is a special file type that stores both regular and special files in a hierarchical order starting from the root ( / ) directory. A directory is the equivalent of a folder in the Windows operating system. Directories are created using the mkdir command, short for making the directory, as we shall see later on in this tutorial.

The Linux hierarchy structure starts from the root directory and branches out to other directories as shown:

Linux Directory Structure
Linux Directory Structure

Let’s understand each directory and its usage.

  • The /root directory is the home directory for the root user.
  • The /dev directory contains device files such as /dev/sda.
  • Static boot files are located in the /boot directory.
  • Applications and user utilities are found in the /usr directory.
  • The /var directory contains log files of various system applications.
  • All system configuration files are stored in the /etc directory.
  • The /home directory is where user folders are located. These include Desktop, Documents, Downloads, Music, Public, and Videos.
  • For add-on application packages, check them out in the /opt directory.
  • The /media directory stores files for removable devices such as USB drives.
  • The /mnt directory contains subdirectories that act as temporary mount points for mounting devices such as CD-ROMs.
  • The /proc directory is a virtual filesystem that holds information on currently running processes. It’s a strange filesystem that is created upon a system boot and destroyed upon shutdown.
  • The /bin directory contains user command binary files.
  • The /lib directory stores shared library images and kernel modules.

Linux File Management Commands

You will spend a great deal of time interacting with the terminal where you will be running commands. Executing commands is the most preferred way of interacting with a Linux system as it gives you total control over the system compared to using the graphical display elements.

For this lesson, and the coming lessons, we will be running commands on the terminal. We are using Ubuntu OS and to launch the terminal, use the keyboard shortcut CTRL + ALT + T.

Let’s now delve into the basic file management commands that will help you create and manage your files on your system.

1. pwd Command

pwd, short for the print working directory, is a command that prints out the current working directory in a hierarchical order, beginning with the topmost root directory ( / ).

To check your current working directory, simply invoke the pwd command as shown.

$ pwd

The output shows that we are in our home directory, the absolute or full path being /home/tecmint.

Print Current Working Directory
Print Current Working Directory
2. cd Command

To change or navigate directories, use the cd command which is short for change directory.

For instance, to navigate to the /var/log file path, run the command:

$ cd /var/log
Navigate Directories in Linux
Navigate Directories in Linux

To go a directory up append two dots or periods in the end.

$ cd ..

To go back to the home directory run the cd command without any arguments.

$ cd 
cd Command Examples
cd Command Examples

NOTE: To navigate into a subdirectory or a directory within your current directory, don’t use a forward slash ( / ) simply type in the name of the directory.

For example, to navigate into the Downloads directory, run:

$ cd Downloads
Navigate to Downloads Directory
Navigate to Downloads Directory
3. ls Command

The ls command is a command used for listing existing files or folders in a directory. For example, to list all the contents in the home directory, we will run the command.

$ ls

From the output, we can see that we have two text files and eight folders which are usually created by default after installing and logging in to the system.

List Files in Linux
List Files in Linux

To list more information append the -lh flag as shown. The -l option stands for long listing and prints out additional information such as file permissions, user, group, file size, and date of creation. The -h flag prints out the file or directory size in a human-readable format.

$ ls -lh
Long List Files in Linux
Long List Files in Linux

To list hidden files, append the -a flag.

$ ls -la

This displays hidden files which start with a period sign (.) as shown.

.ssh
.config
.local
List Hidden Files in Linux
List Hidden Files in Linux
4. touch Command

The touch command is used for creating simple files on a Linux system. To create a file, use the syntax:

$ touch filename

For example, to create a file1.txt file, run the command:

$ touch file1.txt

To confirm the creation of the file, invoke the ls command.

$ ls
Create Empty File in Linux
Create Empty File in Linux
5. cat Command

To view the contents of a file, use the cat command as follows:

$ cat filename
View Contents of Files
View Contents of Files
6. mv Command

The mv command is quite a versatile command. Depending on how it is used, it can rename a file or move it from one location to another.

To move the file, use the syntax below:

$ mv filename /path/to/destination/

For example, to move a file from the current directory to the Public/docs directory, run the command:

$ mv file1.txt Public/docs
Move Files in Linux
Move Files in Linux

Alternatively, you can move a file from a different location to your current directory using the syntax shown. Take note of the period sign at the end of the command. This implies this location’.

$ mv /path/to/file .

We are now going to do the reverse. We will copy the file from the Public/docs path to the current directory as shown.

$ mv Public/docs/file1.txt .
Move Files from Location in Linux
Move Files from Location in Linux

To rename a file, use the syntax shown. The command removes the original file name and assigns the second argument as the new file name.

$ mv filename1 filename2

For example, to rename file1.txt to file2.txt run the command:

$ mv file1.txt  file2.txt
Rename Files in Linux
Rename Files in Linux

Additionally, you can move and rename the file at the same time by specifying the destination folder and a different file name.

For example to move file1.txt to the location Public/docs and rename it file2.txt run the command:

$ mv file1.txt Public/docs/file2.txt
Move and Rename Files in Linux
Move and Rename Files in Linux
7. cp Command

The cp command, short for copy, copies a file from one file location to another. Unlike the move command, the cp command retains the original file in its current location and makes a duplicate copy in a different directory.

The syntax for copying a file is shown below.

$ cp /file/path /destination/path

For example, to copy the file file1.txt from the current directory to the Public/docs/ directory, issue the command:

$ cp file1.txt  Public/docs/
Copy Files in Linux
Copy Files in Linux

To copy a directory, use the -R option for recursively copying the directory including all its contents. We have created another directory called tutorials. To copy this directory alongside its contents to the Public/docs/ path, run the command:

$ cp -R tutorials Public/docs/
Copy Directory in Linux
Copy Directory in Linux
8. mkdir Command

You might have wondered how we created the tutorials directory. Well, it’s pretty simple. To create a new directory use the mkdir ( make directory) command as follows:

$ mkdir directory_name

Let’s create another directory called projects as shown:

$ mkdir projects
Create Directory in Linux
Create Directory in Linux

To create a directory within another directory use the -p flag. The command below creates the fundamentals directory inside the linux directory within the parent directory which is the projects directory.

$ mkdir -p projects/linux/fundamentals
Create Directory in Linux
Create Directory in Linux
9. rmdir Command

The rmdir command deletes an empty directory. For example, to delete or remove the tutorials directory, run the command:

$ rmdir tutorials 
Delete Empty Directory in Linux
Delete Empty Directory in Linux

If you try to remove a non-empty directory, you will get an error message as shown.

$ rmdir projects
Delete Directory in Linux
Delete Directory in Linux
10. rm Command

The rm (remove) command is used to delete a file. The syntax is quite straightforward:

$ rm filename

For example, to delete the file1.txt file, run the command:

$ rm file1.txt

Additionally, you can remove or delete a directory recursively using the -R option. This could either be an empty or a non-empty directory.

$ rm -R directory_name

For example, to delete the projects directory, run the command:

$ rm -R projects
Delete Directory Recursively in Linux
Delete Directory Recursively in Linux
11. find and locate Commands

Sometimes, you may want to search the location of a particular file. You can easily do this using either the find or locate commands.

The find command searches for a file in a particular location and takes two arguments: the search path or directory and the file to be searched.

The syntax is as shown

$ find /path/to/search -name filename

For example, to search for a file called file1.txt in the home directory, run:

$ find /home/tecmint -name file1.txt
Search Files in Linux
Search Files in Linux

The locate command, just like the find command, plays the same role of searching files but only takes one argument as shown.

$ locate filename

For example;

$ locate file1.txt
Locate Files in Linux
Locate Files in Linux

The locate command searches using a database of all the possible files and directories in the system.

NOTE: The locate command is much faster than the find command. However, the find command is much more powerful and works in situations where locate does not produce the desired results.

That’s It! In this topic, we have covered the basic file management commands that will give you the know-how in creating and managing files and directories in a Linux system.

If you read this far, tweet to the author to show them you care. Tweet a thanks
James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

3 thoughts on “LFCA: Learn Basic File Management Commands in Linux – Part 2”

  1. The -l option stands for long listing and prints out additional information such as (...) date of creation.

    This is inaccurate. The date and time of the last modification (ctime) of the file will be printed out. Not the date of file creation.

    Gérard

    Reply
  2. James,

    You need to include a warning about using the “rm -rf” command as its improper use can wipe an entire directory, an entire HOME directory, or if used with superuser privileges, the ENTIRE system.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.