Are you struggling to navigate the Linux file system? The Linux file system can be complex and overwhelming for beginners, but with the right knowledge and techniques, it can be a powerful tool for organizing and managing your data.

In this post, we will provide a complete overview of the Linux file system, including its structure, navigation commands, and best practices for managing files and directories.

Whether you are new to Linux or an experienced user, this guide will give you the skills you need to navigate and utilize the Linux file system with confidence.

So, let’s dive in and start mastering the Linux file system together!

Understanding the File System Structure

In a Unix system, the file system is organized in a hierarchical structure, starting from the root directory represented by ‘/’.

Understanding the different directories and their purpose (e.g. /etc for configuration files, /usr for user-installed software, /home for user home directories) can help navigate the file system more efficiently.

Linux file system

Navigating the Linux File System using the command line

The command line interface (CLI) is the primary way to navigate the file system in a Unix system.

Examples:

  • To see your current location in the file system, use the command pwd (print working directory)
pwd

Output:

pwd - Linux file system command

This shows that am currently in a folder called “roland” in the “home directory”

  • To change your current directory, use the command cd (change directory) followed by the path of the directory you want to navigate to.
cd Desktop

Output:

cd - Linux file system command
  • To see the contents of a directory, use the command ls (list) followed by the path of the directory.
ls /Desktop

Commonly used Navigation commands

  • pwd: displays the current working directory
  • cd: changes the current working directory
  • ls: list the contents of the current working directory
  • mkdir: create a new directory
  • rm: delete a file or a directory
  • mv: rename or move a file or a directory

Using Wildcards

Wildcards allow you to use special characters to match multiple files or directories at once.

Examples:

  • ls *.txt will list all files with the .txt extension in the current directory
  • rm -r /tmp/* will remove all files and directories in the /tmp directory

Creating, Renaming, and Deleting Directories

  • To create a new directory, use the mkdir command followed by the name of the new directory. For example, Let’s create a directory with the name “my_projects”
mkdir my_projects

Output:

This creates a new directory (folder) with the name “my_projects”

mkdir - Linux file system command
  • To rename a directory, use the mv command followed by the current name of the directory and the new name.
mv old_directory new_directory
  • To delete a directory, use the rmdir command followed by the name of the directory
rmdir old_directory

Working with Permissions and Ownership

In a Unix system, files and directories have permissions and ownership that determines who can access and modify them.

Examples:

  • To view permissions for a file or directory, use the command ls -l followed by the path of the file or directory
ls -l /etc/passwd

Output:

permissions - Linux file system
  • To change permissions for a file or directory, use the command chmod followed by the permissions and the path of the file or directory.
chmod 755 /etc/passwd

Finding and searching for files

To find files or directories in the file system, you can use the find command.

Examples:

  • To find all files with a certain name, use the command find / -name “file_name”
  • To find all files with a certain extension, use the command find / -name “*.txt”

How to list files and directions

In a Unix-based system, you can use the ls command to list the files and directories in a specified directory.

Here are some examples of how to use the ls command:

  • To list the files and directories in the current directory, simply enter ls in the terminal
  • To list the files and directories in a specific directory, enter ls followed by the path of the directory. For example, ls Documents will list the files and directories in the Documents directory.
  • To list the files and directories in a directory and its subdirectories, use the-R option. For example, ls -R Documents
  • To list the files and directions in a directory with more details such as permissions, owner, etc. use the -l option. For example, ls -l
  • To list the files and directories in a directory in a human-readable format such as file size, use the -h option. For example, ls -lh Documents
  • To list the files and directories in a directory in reverse order use the -r option. For example, ls -r Documents
how to list contents of directory in reverse order

These are just a few examples of how you can use the ls command to list files and directories.

There are many other options and flags that you can use with the ls command to customize the output and get the information you need.

How to display the content of a file

In a Unix-based system, you can use the cat, less, head and tail commands to display the contents of a file.

Here are some examples of how to use these commands:

  • cat command: To display the entire content of a file, use the cat command followed by the name of the file. For example, cat file.txt will display the contents of the file.txt how to display the entire content of a file using the cat command
  • less command: To display the contents of a file in a scrollable format, use the less command followed by the name of the file. For example, less file.txt will display the contents of the file.txt in a scrollable format, so you can move through the file using the up and down arrow keys.
  • head command: To display the top few lines of a file, use the head command followed by the name of the file. For example, head -n 10 file.txt will display the first 10 lines of the file.txt
  • tail command: To display the last few lines of a file, use the tail command followed by the name of the file. For example, tail -n 10 file.txt will display the last 10 lines of the file.txt

It is important to note that you should have permission to access the file and if the file is very large the output may take a long time to be displayed.

How to Create a file or directory

In a Unix-base system, you can use the touch and mkdir commands to create a file or directory respectively.

Here are some examples of how to use these commands:

  • touch command: To create a new empty file, use the touch command followed by the name of the file. For example, touch newfile.txt will create an empty file named “newfile.txt” in the current directory.
  • mkdir command: To create a new directory. Use the mkdir command followed by the name of the directory. For example, mkdir new_directory will create a new directory named “new_directory” in the current directory.
  • mkdir -p command:  To create a directory tree, use the mkdir -p command followed by the directory tree. For example, mkdir -p newdirectory/subdirectory1/subdirectory2 will create a directory tree with the name “newdirectory” and the subdirectories “subdirectory1” and “subdirectory2”

How to remove a file or directory

You can use the rm and rmdir commands to remove a file and directory respectively. Here are some examples of how to use these commands:

  • rm command: To remove a file, use the rm command followed by the name of the file. For example, rm file.txt will remove the file named file.txt from the current directory.
  • rm -r command: To remove a directory and its contents, use the rm -r command followed by the name of the directory. For example, rm -r mydirectory will remove the directory named “mydirectory” and all of its contents.
  • rmdir command: To remove an empty directory, use the rmdir command followed by the name of the directory. For example, rmdir mydirectory will remove the empty directory named “mydirectory”.

It is important to note that these commands will remove the files and directories permanently without any prompt, so be careful when using them.

Also, you should have permission to remove the files and directories.

You can also use rm - i to remove files interactively, this command will prompt you for confirmation before removing a file.

You can also use rm -f to remove files forcibly, this command will remove files even if the files are write-protected and it will not prompt for confirmation.

How to move or copy a file or directory

You can use mv and cp commands to move or copy a file or directory respectively.

Here are some examples of how to use these commands:

mv command: To move a file or directory, use the mv command followed by the source file or directory and the destination. For example, mv file.txt mydirectory will move the file named “file.txt” from the current directory to the directory named “mydirectory”

cp command: To copy a file, use the cp command followed by the source file and the destination. For example, cp file.txt mydirectory will copy the file named “file.txt” from the current directory to the directory named “mydirectory”.

cp -r command: To copy a directory and its contents, use the cp -r command followed by the source directory and the destination. For example, cp -r mydirectory newdirectory will copy the directory named “mydirectory” and all of its contents to the directory named “newdirectory”.

It is important to note that when moving or copying a file or directory, you should have the permission to access the source file or directory and the destination directory.

When you move a file with the mv command, the original file is removed and replaced by the new location, while the cp command creates a new copy of the file in the new location.

🔖 Further Resources:

Conclusion

Mastering the Linux file system is essential for any Linux user or administrator. The file system is the backbone of any operating system and understanding it is crucial for managing and organizing files and directories.

This guide provided a complete overview of the Linux file system, including its structure, file permissions, and common commands for navigating and managing files and directories.

By understanding the concepts and techniques presented in this guide, you will be able to efficiently and effectively manage your Linux file system.

Whether you are a beginner or an experienced Linux user, this guide will help you take your skills to the next level.

Keep practicing and experimenting with different commands and you will soon be a pro at managing the Linux file system.

If you found this guide helpful, please leave a comment below or share it with others who may find it useful. Your feedback and support are greatly appreciated and help us to continue creating valuable resources.