Welcome to the Basic File Operations in Linux lab! In Linux, almost everything is treated as a file, which makes file operations fundamental to using the system. This lab will guide you through the most common commands for managing files and directories, helping you become more proficient in navigating and organizing your Linux system.
Prerequisites
Basic understanding of what a command line interface is
Familiarity with the concept of files and directories (folders)
Objectives
By the end of this lab, you'll be able to:
Navigate the Linux file system with confidence
Create, list, and manipulate files and directories
In Linux, each user typically has a "home directory," represented by ~. However, in this lab environment, we'll start in the /home/labex/project directory, which is our default working directory.
First, please open a terminal on the Desktop OR switch to the terminal tab in the lab environment.
Let's begin by understanding our current location:
pwd
pwd stands for "print working directory". It displays your current location in the file system. This command is crucial for orienting yourself in the Linux file structure. You should see /home/labex/project as the output.
Now, let's explore the relationship between the current directory and the home directory:
echo ~
This command will display the path to your home directory, which should be /home/labex.
To see the contents of your current directory, use:
ls
This will list the files and directories in your current working directory (/home/labex/project).
Let's also check the contents of your home directory:
ls ~
This command lists the contents of your home directory, which may be different from your current working directory.
Understanding the distinction between your current working directory and your home directory is important for navigating the Linux file system effectively.
Navigating the File System
Linux uses what we call a "hierarchical file system". Think of it like a big tree with branches. The main trunk is called the "root directory", represented by a single forward slash /. All other directories and files branch out from this root.
Let's explore how to move around in this tree-like structure:
Check your current location:
pwd
This should show /home/labex/project. If it doesn't, you might be in a different directory. Use cd /home/labex/project to get back to the starting point.
View the contents of your current directory:
ls
This lists all files and folders in your current location. /home/labex/project is empty, so you won't see anything.
Move up one level to the parent directory:
cd ..
The .. means "the directory above". After this command, do pwd again. You should now be in /home/labex.
Return to your project directory:
cd project
This takes you back to /home/labex/project.
Go to your home directory:
cd ~
The ~ is a shortcut for your home directory. Do pwd to confirm you're in /home/labex.
Return to the project directory using an absolute path:
cd /home/labex/project
This is called an "absolute path" because it starts from the root (/) and gives the full location.
Creating Files and Listing Directory Contents
Now that we know how to navigate, let's create some files and explore how to list directory contents.
First, make sure you're in the /home/labex/project directory:
cd /home/labex/project
Let's create a few files:
touch file1.txt
The touch command is used to create an empty file. If the file already exists, it updates the file's timestamp without changing its content. It's a simple way to create new, empty files.
echo "Hello, Linux" > file2.txt
This command does two things:
echo is a command that prints text.
The > symbol redirects the output of echo into a file named file2.txt. If the file doesn't exist, it's created. If it does exist, its content is replaced.
echo "Hidden file" > .hiddenfile
This creates a hidden file. In Linux, any file or directory name that starts with a dot (.) is considered hidden.
Now, let's create a directory:
mkdir testdir
The mkdir command (short for "make directory") creates a new directory named testdir.
Basic listing:
ls
This shows the contents of your current directory. You should see file1.txt, file2.txt, and testdir.
Detailed listing:
ls -l
The -l option provides a "long" format. You'll see additional details like file permissions, owner, size, and modification date.
Show hidden files:
ls -a
This will show all files, including the hidden .hiddenfile we created.
Combine options:
ls -la
This combines the long format (-l) with showing all files (-a).
List contents of a specific directory:
ls -l testdir
This lists the contents of the testdir directory (which should be empty at this point).
Copying Files and Directories
Now that we have some files to work with, let's learn how to copy them:
Copy a file:
cp file1.txt file1_copy.txt
This creates a copy of file1.txt named file1_copy.txt in the current directory.
Let's verify the copy:
ls
Copy a file to another directory:
cp file2.txt testdir/
This copies file2.txt into the testdir directory.
Copy a directory:
cp -r testdir testdir_copy
The -r option stands for "recursive". It's necessary when copying directories to ensure all contents are copied.
Verify our copies:
ls
ls testdir
ls testdir_copy
Moving and Renaming Files and Directories
The mv command is used for both moving and renaming in Linux:
Rename a file:
mv file1.txt newname.txt
This renames file1.txt to newname.txt.
Move a file to a directory:
mv newname.txt testdir/
This moves newname.txt into the testdir directory.
Rename a directory:
mv testdir_copy new_testdir
This renames testdir_copy to new_testdir.
Move and rename in one command:
mv testdir/newname.txt ./original_file1.txt
This moves newname.txt out of testdir and renames it to original_file1.txt in the current directory.
Verify our changes:
ls
ls testdir
Removing Files and Directories
Removing files and directories is a powerful but potentially dangerous operation. Always double-check before using these commands, especially when using options that bypass safeguards:
Remove a file:
rm file1_copy.txt
This permanently deletes file1_copy.txt.
Remove an empty directory:
rmdir new_testdir
rmdir only works on empty directories, and you'll get an error if the directory isn't empty.
Remove a directory and its contents:
rm -r testdir
The -r option is needed to remove directories and their contents recursively.
Remove files interactively:
rm -i file2.txt
The -i option prompts for confirmation before each removal. Type 'y' and press Enter to confirm deletion.
Force removal without prompts:
touch remaining_files
ls
rm -rf remaining_files
The -rf combination is very powerful and potentially dangerous. It means:
-r: recursive (for directories)
-f: force (ignore nonexistent files, never prompt)
CAUTION: rm -rf will delete files and directories without asking for confirmation. It can be extremely destructive if used incorrectly. Always double-check your command before using it, especially when using it with wildcards or as a superuser.
Verify our removals:
ls
Remember: In Linux, there's usually no "Recycle Bin" or "Trash" for the command line. When you delete something with rm, it's generally gone for good.
Summary
Congratulations! You've learned the essential file operations in Linux:
Navigating the file system with cd and pwd
Creating files and directories with touch and mkdir
Listing contents with ls and its options
Copying files and directories with cp
Moving and renaming with mv
Removing files and directories with rm and rmdir
These commands form the foundation of file management in Linux. With practice, you'll become proficient in managing your files and directories from the command line.
Remember to use these commands carefully, especially rm, as it permanently deletes files and directories without the possibility of recovery.
As you continue your Linux journey, explore man pages (e.g., man ls) to learn more about each command and its options. Happy exploring!
We use cookies for a number of reasons, such as keeping the website reliable and secure, to improve your experience on our website and to see how you interact with it. By accepting, you agree to our use of such cookies. Privacy Policy