Are you having trouble with Linux file system commands? Yeah, I know—it can feel pretty crazy when you are trying to figure it all out. Don’t worry, it’s totally normal to feel confused at first.
But with a little practice, you’ll see that it’s actually a great way to organize and manage your files efficiently—and yes, quickly!
As a developer, DevOps person, DB administrator e.t.c., it is important to understand how to navigate and work around the Linux system. Most production environments are hosted on Linux, so having a bit of knowledge about Linux commands is really handy—especially when it comes to installing development packages.
In this post, I will explain Linux file system commands step by step. We will go over folder structure, the key commands, and some simple tips for managing your files and folders.
Whether you are new to Linux or just need a quick refresher, this guide will help you feel more comfortable using it. So, let’s dive in and get started!
Linux System Folder Structure
Have you ever wondered how the Linux file system is structured?
C’mon! Back in the day, I thought it was just like my Windows setup, you know, with a Local Disk (C:) and some folders inside. Seriously, I am sure you have thought the same at some point too.
Well, here is the thing — Linux is totally different from Windows.
Instead of the usual “C:” drive, Linux organizes everything in a hierarchical structure, starting from the root directory, which is simply represented by “/”. Wild, right?
In a Linux system, everything branches out from this root directory.
Let’s take a look at some of the key directories you will find in a typical Linux setup:
- “/etc” for configuration files,
- “/usr” for user-installed software,
- “/home” for user home directories,
- “/media” contains removable devices,
- “/tmp” contains temporary files
- “/opt” contains third-party applications
- etc.
Navigating the Linux File System Commands
The command line interface (CLI) is the primary way to navigate the file system in a Linux system.
Illustration 1:
To see your current location in the file system, use the command pwd (print working directory)
pwd
Output:
This shows that am currently on my Desktop located inside a folder called “roland” in the “home directory”
Illustration 2:
To see the contents of a directory, use the command ls (list) followed by the path of the directory.
ls
Illustration 3:
To change your current directory, use the command cd (change directory) followed by the path of the directory (folder) you want to navigate to:
cd Desktop
Output:
From here, we tried moving from the current directory (which is “roland”) into the Desktop directory.
Commonly used Linux file system 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
Have you ever wondered how you can select all the .js, .php, .doc, or even .txt files in a particular folder or directory all at once?
I bet you are thinking, “Wait, is that even possible?”
Well, yes, it is! But only from the command line. On a graphical interface like Windows, you can not do this in one go.
This is where wildcards come into play.
Wildcards allow you to use special characters to match multiple files or directories at once.
Examples:
- ls *.html will list all files with the .html 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”
- 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
Note: You are probably wondering where “old_directory” and “new_directory” are coming from, right? Well, they are just the names of the folder you want to rename and the new name you want to give it, simple as that!
- To delete a directory, use the “rmdir” command followed by the name of the directory
rmdir old_directory
Working with Permissions and Ownership
One of the coolest things about using Linux is its security. Have you ever thought about having a file or folder you really do not want anyone to mess with? Like a secret vault filled with all your goodies! That is where permissions and ownership come in.
In Linux, every file and directory has its own permissions and ownership settings that determine who can access and change them. Pretty neat, right?
So, to check the permissions for a file or directory, just use the command “ls -l”, followed by the path of the file or directory.
Right now, let us see what the permissions and ownership look like for files and directories on my Desktop:
ls -l
Output:
From the image above, you will see that the entries starting with the letter “d” are directories (or folders), while the rest are basic files.
Now, you might be thinking, “What the heck do those other letters mean?” Do not fret!
In a Linux system, we have three types of users:
- Owner
- Group
- Others
After the d or –, the next three letters tell you about the owner’s permissions, the following three are for the group users, and the last three are for others.
The letters rwx stand for read, write, and execute permissions, respectively.
Let us break down the first entry in the image above, about.html, to understand its permissions:
The first part of the output shows -rw-rw-r–. Here’s what it all means:
- The first character, –, tells us this is a regular file (not a directory).
- The next three characters, rw-, indicate the owner’s permissions:
- r means the owner can read the file.
- w means the owner can write to the file.
- – means the owner does not have execute permission.
- The next three characters, rw-, are for the group permissions:
- r means the group can read the file.
- w means the group can write to the file.
- – means the group cannot execute it.
- The final three characters, r–, show the permissions for others (everyone else):
- r means others can read the file.
- — means others cannot write or execute it.
So, to sum it up for about.html:
- The owner and group can read and modify the file.
- Others can only read it but cannot change or execute it.
And that same breakdown applies to every entry in the list, though you will see different permissions depending on the files and directories!
For more info on Ownership, check out – An Introduction to Linux Permissions
Using the “ls” Command: Listing Files and Directories with Options
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”
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.
For more Info on linux file system commands, Check – Top 50+ Linux Commands You MUST Know
How to display the content of a file
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
- 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
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”
With Linux file system commands, you can do all sorts of magic quickly and effortlessly. Honestly, that is one of the best parts about working with the command line.
Did you know you can create multiple files at once? Yup, it is totally possible! The command line is not just a basic tool—it is a powerful machine ready to handle any task you throw at it.
In the snapshot above, we created multiple files in one go. Pretty cool, right? It saves you the hassle of creating them one by one.
I bet you can not even imagine all the amazing things you can do with the command line. Sounds exciting, doesn’t it?
How to Remove a File or Directory in a Linux System
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 in a Linux System
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.
Resources
- Want to delve deeper into the world of the Linux shell? Check here, for an in-depth look at ‘Learning the Shell’ and start mastering the command line today!
- Want a step-by-step guide on how to build and validate web forms in node.js, check out – Build Powerful Web Form with Node.js (Step-by-Step Guide)
Conclusion
Mastering the Linux file system is essential for any developer, DevOps person, DB administrator, etc. 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.
Keep practicing and experimenting with different commands and you will soon be a pro at managing the Linux file system.