Introduction
Welcome to the world of the Linux command line! A fundamental skill for any Linux user is navigating and manipulating the file system. In Linux, everything is treated as a file, and understanding how to interact with files and directories is crucial.
In this lab, you will get hands-on experience with the essential commands for file system management. You will learn how to:
- List files and directories with
ls. - Create new directories with
mkdir. - Copy files with
cp. - Move files and directories with
mv. - Remove files and directories with
rm.
By the end of this lab, you will be comfortable performing basic file operations from the terminal.
List Files with ls -la ~ Command
In this step, you will learn how to list the contents of a directory using the ls command. This is one of the most frequently used commands in Linux.
The ls command, short for "list", displays information about files and directories. We will use it with two common options:
-l: This option enables the "long listing" format, which shows detailed information such as permissions, owner, size, and modification date.-a: This option stands for "all" and it tellslsto include hidden files (files whose names begin with a dot.).
The ~ symbol is a shortcut that represents your home directory. Now, let's list all the contents of your home directory in a long list format. Execute the following command in your terminal:
ls -la ~
You will see an output similar to this. The exact contents might vary, but you should see the contents of your labex user's home directory.
total 12
drwxr-x--- 1 labex labex 4096 Oct 26 10:10 .
drwxr-xr-x 1 root root 4096 Oct 26 10:10 ..
-rw------- 1 labex labex 0 Oct 26 10:10 .zsh_history
This output shows you the permissions, owner (labex), group (labex), size, modification date, and name of each item within your home directory.
Create Directory with mkdir ~/testdir Command
In this step, you will create a new directory. The command for this is mkdir, which stands for "make directory".
You need to provide the path and name of the directory you want to create. We will create a directory named testdir inside your home directory.
Execute the following command:
mkdir ~/testdir
The command will not produce any output if it is successful. To verify that the directory was created, you can use the ls command again.
ls ~
You should now see your new directory testdir listed in the output.
testdir
Copy File with cp /etc/hosts ~/testdir/ Command
In this step, you will learn how to copy files using the cp command. The cp command requires at least two arguments: a source and a destination.
The syntax is cp [source_file] [destination_path].
We will copy the /etc/hosts file, a system file that maps hostnames to IP addresses, into the testdir directory you created in the previous step.
Execute the following command:
cp /etc/hosts ~/testdir/
Again, a successful cp command doesn't produce any output. To confirm that the file has been copied, list the contents of the ~/testdir directory.
ls ~/testdir
You should see the hosts file inside the directory.
hosts
Move Directory with mv ~/testdir /tmp/ Command
In this step, you will move the testdir directory to a different location. The mv command, short for "move", is used for this purpose. It can move both files and directories. The mv command is also used to rename files and directories.
The syntax is mv [source] [destination].
We will move the entire testdir directory from your home directory to the /tmp directory, which is a standard location for temporary files.
Execute the following command:
mv ~/testdir /tmp/
To verify the move, you can check two things. First, check if testdir is no longer in your home directory:
ls ~
The output should no longer list testdir.
Next, check if testdir now exists in /tmp:
ls /tmp
You should see testdir in the output list.
testdir
...
Remove Files with rm -rf /tmp/testdir Command
In this final step, you will learn how to remove files and directories using the rm command, which stands for "remove".
Warning: The rm command is powerful and permanently deletes files. When used with the -rf options, it can delete entire directory structures without any confirmation. Always double-check the path you provide to rm -rf before executing it.
The options we will use are:
-r(or-R): "recursive", which is necessary to remove a directory and its contents.-f: "force", which ignores nonexistent files and arguments, and never prompts for confirmation.
Now, let's clean up by removing the testdir directory from /tmp.
rm -rf /tmp/testdir
To verify that the directory has been removed, list the contents of the /tmp directory again.
ls /tmp
You will notice that testdir is no longer in the list.
Summary
Congratulations! You have successfully completed this lab on basic file system navigation in Linux.
You have learned and practiced the five most fundamental commands for managing files and directories:
ls: To list the contents of a directory.mkdir: To create new directories.cp: To copy files.mv: To move or rename files and directories.rm: To remove files and directories.
Mastering these commands is the first step toward becoming proficient with the Linux command line. We encourage you to continue exploring and practicing these commands to build your confidence.



