If you are new to Linux, finding your way around and organizing files is very high on the to do list. Linux is different from Windows or macOS in that it often does not rely on graphical user interfaces (GUIs), making command-line skills essential for a smooth experience.
In Linux, you do many tasks through the command line. This includes keeping your files organized, updating software, and troubleshooting issues. So, mastering file management through text commands is the key to becoming a Linux-Pro.
However, Linux is incredibly versatile. While it typically does not have a graphical interface like Windows or macOS, you have the option to run a desktop environment on a Linux system. If you are interested in learning how to set up a GUI on Linux, including Ubuntu and CentOS, we have a dedicated article on “How to Install a GUI and XRDP on Ubuntu and CentOS“. This can help you bridge the gap between the GUI-centric world of Windows and macOS and the command-line-focused environment of Linux. But now, let us focus on managing Linux through the command-line:
Getting Started: Introduction to the Linux Terminal
Welcome to the world of Linux! As you start your journey to master Linux navigation and file management, one tool will become your trusty companion: the Linux terminal.
The Linux Terminal: Your Command Center
The Linux terminal, often referred to as the command line, is where the real magic happens in the Linux universe. Unlike the familiar graphical interfaces of Windows or macOS, the terminal operates entirely through text commands. It is a powerful tool that gives you direct control over your Linux system, allowing you to perform a wide range of tasks, including file management, system configuration, and software installations.
Establishing an SSH Connection
Before we can start using the terminal, let us address a crucial step when working with a Linux server: establishing an SSH (Secure Shell) connection. This step is essential because it is how you connect to your Linux server securely.
For step by step guidance on establishing an SSH connection, we have a dedicated article titled “Establishing a Connection to Your Server via SSH“.
To connect to your server via SSH, you have several options:
1. Built-In Tools: On Windows, you can use Windows PowerShell, while on macOS and Linux desktops, you can use the built-in terminal. These are convenient options for connecting to your server securely.
Use the following command-structure to connect to your server via SSH using the build-in tools:
Of course, replace [username] with your username and [server_ip_address] with the IP address or hostname of the remote server
2. Third-Party Software: If you prefer third-party software, PuTTY is a popular choice for Windows users. It provides a user-friendly interface for SSH connections and is widely used in the Linux community.
With your SSH connection established, you will be ready to dive into the Linux terminal and start your exploration of file management and navigation. Let us get started!
Basic Linux Commands
Now that you are familiar with how to establish an SSH connection, let’s look at some essential Linux commands. These will serve as your foundation for navigating and managing files and directories in Linux. This chapter will give you basic information on the most important commands when it comes to managing your server. We will discuss all the commands and their syntax in later chapters.
1. pwd (Print Working Directory)
The pwd command is your GPS in the Linux file system. It shows you the path to your current directory, helping you understand where you are in the directory tree.
2. ls (List Files and Directories)
The ls command is used to list the files and directories in the current directory. It provides an overview of what is inside a folder.
3. cd (Change Directory)
The cd command is your ticket to move around the file system. It lets you change your current directory to another location.
Usage (Moving to a directory):
Usage (Moving up one directory):
Usage (Moving to your home directory):
4. mkdir (Make Directory)
Need to create a new directory? The mkdir command is here to help. It allows you to make a new folder in the current directory.
5. touch (Create Empty File)
When you need to create a new, empty file, use the touch command. It is a quick way to generate files for various purposes.
6. rm (Remove Files and Directories)
The rm command is for deleting files and directories. Be cautious when using it, as it permanently removes files, and deleted data cannot be easily recovered.
Usage (Removing a file):
Usage (Removing a directory and its contents):
rm -r directory_name
These are fundamental Linux commands that will help you navigate, organize, and manage files and directories effectively. As you become more comfortable with these basics, you will be well-prepared to explore more advanced Linux file management techniques.
Navigating the Linux File System
To become skilled in Linux navigation and file management, it is important to understand the Linux file system’s structure. In this chapter, we will explore the fundamentals of the Linux file structure and show the significance of the root directory (“/”).
Understanding the Linux File Structure
Linux follows a hierarchical file system, which means it organizes files and directories in a tree-like structure. At the top of this hierarchy is the root directory (“/”), and everything in the system stems from it. Here is a simplified breakdown:
- Root Directory (“/”): The top-level directory in the Linux file system. It is represented by a single forward slash (“/”) and serves as the starting point for all paths in the system.
- Subdirectories: Under the root directory, you will find various subdirectories, each serving a specific purpose. For example, “/home” typically contains user directories, while “/etc” holds system configuration files.
- Files: Files reside within directories and can include documents, programs, scripts, and more.
The Importance of the Root Directory (“/”)
The root directory (“/”) is the heart of the Linux file system, and its significance cannot be overlooked. Here is why it matters:
1. Absolute Path Reference: The root directory is the reference point for all absolute paths in Linux. An absolute path specifies the complete directory structure from the root directory to a specific file or directory. For example, “/home/user/documents” is an absolute path, with “/” denoting the root.
2. System Integrity: Critical system files and directories, such as configuration files and essential binaries, are stored within the root directory. This separation helps maintain the system’s integrity by ensuring that essential components are easily accessible and protected from accidental deletion.
3. User Directories: User home directories, where individual users store their files and configurations, are typically found as subdirectories of the root. For example, “/home/user1” and “/home/user2” represent the home directories for two different users.
4. Resource Organization: The root directory helps categorize resources. For instance, system-wide configurations reside in “/etc,” executable files in “/bin,” and libraries in “/lib.”
Important Folders in the Linux File System
In the Linux filesystem, there are some special folders that play vital roles in keeping the system running smoothly. These folders help organize important stuff, and knowing what they are can be a real asset when you are using Linux. Let us break down these essential directories and what is inside them:
1. /bin – Essential System Binaries
The /bin directory houses essential system binaries, including fundamental commands like ls, cp, and mv. These binaries are required for the system to function correctly, and they are available to all users, making it possible to perform essential tasks even in single-user mode.
2. /etc – Configuration Files
Configuration files for the system and installed software are stored in /etc. These files control various aspects of the system’s behavior, such as network settings, user permissions, and software configurations. It is a crucial directory for system administrators.
3. /home – User Home Directories
Each user on a Linux system has their own directory under /home. For example, the home directory for the user “john” would be /home/john. Users store their personal files, documents, and configuration files in their respective home directories.
4. /var – Variable Data
The /var directory contains variable data that may change frequently during system operation. This includes log files, databases, and spool directories for print jobs and mail.
5. /tmp – Temporary Files
As the name suggests, /tmp is where temporary files are stored. These files are typically short-lived and are automatically deleted when the system reboots. It is a handy location for applications to store data temporarily.
6. /usr – User System Resources
The /usr directory holds user-related system resources, including user binaries, libraries, and documentation. It is often mounted as a separate file system, allowing system administrators to keep it consistent across multiple machines.
7. /sbin – System Binaries (Superuser)
Like /bin, the /sbin directory contains essential binaries. However, the binaries here are typically intended for system administration tasks and require superuser (root) privileges to execute.
8. /lib – Shared Libraries
Shared libraries required for running programs are stored in the /lib directory. These libraries are essential for the proper functioning of various software on the system.
9. /opt – Optional Software
The /opt directory is where optional or add-on software packages are typically installed. It provides a standardized location for third-party software that is not part of the core system.
Listing Files and Directories
Now that we have covered the basics of important directories in the Linux file system, let us explore how to list files and directories using the ls command. ls is a handy tool for getting an overview of what is in your current directory or any other directory you specify. We will look at different options you can use with ls and provide code examples to illustrate their usage.
Using ls with Various Options
ls has various options that allow you to customize the way it displays information. Here are some commonly used options:
|Lists files and directories in long format, providing detailed information (permissions, owner, size, modification date).
|Includes hidden files and directories in the listing (those starting with a dot).
|Displays file sizes in a human-readable format (kilobytes, megabytes, gigabytes).
|Displays file sizes in a human-readable format (kilobytes, megabytes, gigabytes).
|Reverses the order of the listing, displaying items in reverse alphabetical or chronological order.
Now, let us see some code examples for different ls options:
Example 1: Basic Listing
To list files and directories in your current directory, simply run:
Example 2: Long Format Listing
To list files and directories with detailed information, use the -l option:
Example 3: Listing Hidden Files
To list hidden files and directories (those starting with a dot), use the -a option:
Example 4: Human-Readable File Sizes
To list file sizes in a human-readable format, combining the -l and -h options:
Example 5: Sorting by Modification Time
To list files and directories sorted by modification time, use the -t option:
Example 6: Reverse Order Listing
To reverse the order of the listing (e.g., to see the oldest files first), add the -r option:
These examples highlight some of the most used options with the ls command. Experimenting with these options will help you tailor your file listings to suit your specific needs, whether you are looking for specific files, checking file sizes, or monitoring changes in directories.
To effectively navigate the Linux file system, you will need to know how to change directories using the cd command. The cd command allows you to move from one directory to another, enabling you to explore and access various parts of the file system. In this chapter, we will talk about how to use the cd command and provide examples of both relative and absolute paths.
Using the cd Command
The cd command stands for “change directory.” It allows you to switch your current working directory to another location in the file system. Here is how you can use it:
Replace [directory_path] with the path to the directory you want to move to.
Examples of Relative and Absolute Paths
Understanding both relative and absolute paths is crucial when using the cd command.
– Relative Path: A relative path specifies the location of a directory or file in relation to your current directory. For example, if you are in the /home/user directory and want to move to the /home/user/documents directory, you can use a relative path like this:
You do not need to specify the entire path; just the portion relative to your current location.
– Absolute Path: An absolute path specifies the complete directory structure from the root directory (“/”) to the target directory or file. For example, if you are in the same /home/user directory but want to move to /var/log, you would use an absolute path like this:
You provide the full path, starting from the root directory, to reach your destination.
Understanding when to use relative or absolute paths depends on your current location and where you want to go within the file system. Relative paths are handy when you are moving within the same branch of the directory tree, while absolute paths are useful when you need to jump to a completely different part of the file system.
Creating and Managing Files and Directories
Creating and managing files and directories is an essential skill. This chapter will cover various commands that allow you to perform these tasks effectively, including creating directories with mkdir, creating files with touch, renaming and moving files and directories with mv, copying files with cp, and removing files and directories with rm.
Creating Directories with mkdir
The mkdir command stands for “make directory,” and as the name suggests, it is used to create new directories. Here is how you can use it:
Replace [directory_name] with the name of the directory you want to create.
For example, to create a directory called “documents,” you would run:
Creating Files with touch
The touch command is used to create new, empty files. It is a quick way to generate files for various purposes. Here is the basic syntax:
Replace [file_name] with the name of the file you want to create.
For instance, to create an empty file named “notes.txt,” you would use:
Renaming and Moving Files and Directories with mv
The mv command is a versatile tool that allows you to both rename and move files and directories. Here is how to use it:
Using mv for Moving Files and Directories
mv [source] [destination]
Replace [source] with the current name or path of the file or directory.
Replace [destination] with the new name or path you want to assign to the file or directory.
For example, to rename a file from “old_file.txt” to “new_file.txt,” you would run:
mv old_file.txt new_file.txt
To move a file or directory from one location to another, use the mv command with the source and destination paths. For instance:
mv /path/to/source /path/to/destination
Using cp for Copying Files and Directories
The cp command is used to copy files and directories from one location to another. Here is the basic syntax for copying files:
cp [source] [destination]
Replace [source] with the name or path of the file or directory you want to copy.
Of course replace [destination] with the location where you want to copy the file or directory.
For example, to copy a file named “document.txt” from your current directory to a folder called “backup,” you would use:
cp document.txt backup/
Copying Directories and Their Contents
To copy an entire directory and its contents, use the -r (recursive) option with cp:
cp -r [source_directory] [destination_directory]
For instance, to copy a directory named “my_folder” and everything inside it to another location, you would run:
cp -r my_folder /path/to/destination/
Removing Files and Directories with rm
The rm command is used for removing files and directories. Be cautious when using it, as it permanently deletes data, and deleted files cannot be easily recovered. Here is how to use it:
To remove a file, use the following command:
Replace [file_name] with the name of the file you want to delete.
For example, to delete a file named “unwanted.txt,” you would run:
To remove a directory and its contents, you can use the -r (recursive) option with rm. This option deletes all files and subdirectories within the specified directory. Use this command with extreme caution, as it cannot be undone.
rm -r [directory_name]
Replace [directory_name] with the name of the directory you want to remove.
For instance, to delete a directory named “old_directory,” including all its contents, you would use:
rm -r old_directory
Working with Hidden Files and Directories
Hidden files and directories play a crucial role in organizing and storing configuration and application-specific data. Names that start with a dot (e.g., .config) typically identify these files and directories. This chapter will introduce you to hidden files, explain their significance, and show you how to reveal them using the ls command.
Introduction to Hidden Files
Hidden files and directories in Linux serve several purposes:
- Configuration Files: Many applications and system components store their configuration settings in hidden files. The settings are often specific to the user and application, and they are stored away from regular user files to avoid clutter.
- Application Data: Hidden directories can contain application-specific data or cache files. These files help improve the performance and functionality of applications by storing data that does not need to be visible to the user.
- System Files: To prevent accidental modification or deletion by users, the system hides some system-related files and directories
- These files often play a critical role in the functioning of the Linux system.
How to Show Hidden Files
By default, when you use the ls command, it does not display hidden files and directories. To reveal hidden items, you can use the -a option with ls. Here is how it works:
The -a option tells ls to show all files and directories, including hidden ones, in the current directory. When you run this command, you will see a list that includes regular files, directories, and hidden files and directories, all marked with names beginning with a dot.
For example, running ls -a might display a list like this:
. .. .config .bashrc documents photos
In this listing, the hidden files and directories (.config and .bashrc) are now visible alongside the regular items.
Understanding hidden files and how to reveal them using ls -a is important, especially when you need to access or configure various application settings and system files in Linux. These hidden items are integral to the proper functioning and customization of your Linux system.
In this beginner-friendly guide to Linux navigation and file management, we have covered essential concepts and commands that will help you become proficient in working with the Linux file system. Let us summarize the key takeaways from this article:
- Importance of Linux Navigation and File Management: Linux navigation and file management skills are fundamental for effectively using a Linux system. They enable you to organize, access, and manipulate files and directories efficiently.
- Linux File Structure: Linux follows a hierarchical file structure with the root directory (“/”) at the top. Understanding the structure and significance of key directories, such as “/home” and “/etc,” is essential for effective file management.
- Terminal Usage: Linux often relies on the terminal or command line interface for file management tasks. Learning how to use the terminal is crucial for working with Linux servers, VPS, and other command-line-based systems.
- Copying and Moving: Commands like cp and mv allow you to copy, move, rename, and organize files and directories. These commands provide flexibility and control over your data.
- Creating and Managing: Use mkdir to create directories and touch to create empty files. Learn how to manage files and directories with rm to remove them safely.
- Hidden Files and Directories: Files and directories designated by names starting with a dot often serve as repositories for configuration and application-specific data. Use ls -a to reveal hidden items when listing directory contents.
Mastering these concepts and commands will equip you with the confidence to navigate, manage, and customize your Linux system effectively. Whether you are a beginner or looking to expand your Linux skills, this knowledge is valuable for a smooth and efficient Linux experience.