Basic Files Operations

LinuxLinuxIntermediate
Practice Now

Introduction

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.

This is a Guided Lab, which provides step-by-step instructions to help you learn and practice. Follow the instructions carefully to complete each step and gain hands-on experience. Historical data shows that this is a intermediate level lab with a 67% completion rate. It has received a 99% positive review rate from learners.

Understanding Your Working Environment

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.

Terminal interface screenshot

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.

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:

  1. 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.

  1. 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.

  1. 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.

  1. Return to your project directory:
cd project

This takes you back to /home/labex/project.

  1. Go to your home directory:
cd ~

The ~ is a shortcut for your home directory. Do pwd to confirm you're in /home/labex.

  1. 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
  1. 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.

  1. Now, let's create a directory:
mkdir testdir

The mkdir command (short for "make directory") creates a new directory named testdir.

  1. Basic listing:
ls
Directory contents listing output

This shows the contents of your current directory. You should see file1.txt, file2.txt, and testdir.

  1. Detailed listing:
ls -l

The -l option (that's a lowercase L, not the number 1) provides a "long" format listing. You'll see additional details like file permissions, owner, size, and modification date.

  1. Show hidden files:
ls -a

This will show all files, including the hidden .hiddenfile we created.

  1. Combine options:
ls -la

This combines the long format (-l) with showing all files (-a).

  1. 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:

  1. 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
  1. Copy a file to another directory:
cp file2.txt testdir/

This copies file2.txt into the testdir directory.

  1. 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.

  1. Verify our copies:
ls
ls testdir
ls testdir_copy
Terminal showing copied files

Moving and Renaming Files and Directories

The mv command is used for both moving and renaming in Linux:

  1. Rename a file:
mv file1.txt newname.txt

This renames file1.txt to newname.txt.

  1. Move a file to a directory:
mv newname.txt testdir/

This moves newname.txt into the testdir directory.

  1. Rename a directory:
mv testdir_copy new_testdir

This renames testdir_copy to new_testdir.

  1. 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.

  1. Verify our changes:
ls
ls testdir
Command execution results display

Removing Files and Directories

Removing files and directories is a powerful operation. Unlike graphical interfaces, the command line often doesn't have a "Recycle Bin" or "Trash". Deletions made with rm are usually permanent. Always double-check your commands before executing them!

Let's clean up the files and directories we created. Make sure you are in the /home/labex/project directory.

pwd
ll -a

You should see files like original_file1.txt, .hiddenfile, file2.txt, and directories like testdir, new_testdir.

-rw-rw-r-- 1 labex labex   12 May  3 08:44 .hiddenfile
-rw-rw-r-- 1 labex labex    0 May  3 08:45 file1_copy.txt
-rw-rw-r-- 1 labex labex   13 May  3 08:44 file2.txt
drwxrwxr-x 2 labex labex   23 May  3 08:45 new_testdir
-rw-rw-r-- 1 labex labex    0 May  3 08:44 original_file1.txt
drwxrwxr-x 2 labex labex   23 May  3 08:45 testdir
  1. Remove a single file:

    rm original_file1.txt

    The rm command (short for "remove") deletes files. Let's check:

    ls

    original_file1.txt should be gone.

  2. Remove interactively (safer):

    Let's try to remove file2.txt, but this time using the interactive flag -i:

    rm -i file2.txt

    The -i option prompts you for confirmation before deleting each file. Type y (for yes) and press Enter to confirm the deletion. If you type n or anything else, the file will not be deleted.

    ls

    If you confirmed, file2.txt will be gone.

  3. Remove an empty directory:

    Remember the new_testdir we created by renaming testdir_copy? Let's check if it's empty:

    ls new_testdir

    If it's empty (shows no files), we can remove it using rmdir:

    rmdir new_testdir

    rmdir (remove directory) only works on empty directories.

    ls

    new_testdir can't be removed because it's not empty.

  4. Attempt to remove a non-empty directory:

    Now, let's try rmdir on testdir, which still contains file2.txt (copied in Step 4):

    ls testdir
    rmdir testdir

    You will likely see an error message like rmdir: failed to remove 'testdir': Directory not empty. This is expected because rmdir cannot remove directories that contain files or other directories.

  5. Remove a directory and its contents (recursively):

    To remove a directory that is not empty, we need to use rm with the -r (recursive) option:

    rm -r testdir

    This command removes the testdir directory and everything inside it. Use this command with caution.

    ls

    testdir should now be gone.

  6. Force removal (use with extreme caution):

    Sometimes, you might want to remove files without being prompted, even if they are write-protected (though we don't have any here). The -f (force) option does this.

    Let's remove our hidden file:

    rm .hiddenfile
    ls -a

    Now, let's combine -r and -f. The rm -rf command is extremely powerful and potentially dangerous. It removes directories recursively (-r) and forces removal without prompting (-f).

    !!! DANGER ZONE !!!
    Be ABSOLUTELY SURE you know what you are deleting before running rm -rf. A small typo could delete critical system files or your personal data. There is no undo. For example, rm -rf / could attempt to delete your entire system (if you have permissions). Always double-check the path.

    Let's create a temporary directory and file to demonstrate (safely):

    mkdir temp_dir
    touch temp_dir/temp_file.txt
    ls -R temp_dir ## -R lists recursively

    Now, let's remove it forcefully:

    rm -rf temp_dir

    Verify the removal:

    ls

    temp_dir should be gone.

Remember: In Linux command line, deleted files are generally gone forever. Use rm carefully!

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!