Skip to content

Linux Terminal for Beginners - Mastering File Manipulation

Published: at 02:30 PM

terminal-2.png

Welcome back! In our last article, we learned how to navigate the file system using the Linux terminal. Today, we’ll learn how to manipulate files themselves. We’ll cover creating, moving, copying, and renaming files - all from the command line.

Before we start, I’d like to introduce you to Warp, a modern terminal that can make your command-line experience smoother and more intuitive, especially if you’re new to terminal interfaces. Warp offers features like auto-suggestions, clear output formatting, and a user-friendly interface. The best part? It’s completely free to use! If you’re interested in trying it out, you can download Warp here. Don’t worry if you prefer to stick with your current terminal - all the commands we’ll learn today work in any standard terminal.

Setting Up Our Workspace

Let’s start by creating a workspace that we’ll use throughout this article. This will ensure we’re all starting from the same point. Follow these steps:

  1. Open your terminal terminal.png

  2. Navigate to your home directory

    cd ~
    

    Th cd ~ command changes your current directory to your home directory, providing a consistent starting point for everyone.

  3. Create a new directory called “terminal_practice”

    mkdir terminal_practice
    

    The mkdir command stands for “make directory” and creates a new folder named “terminal_practice”. mkdir-terminal-practice.png

  4. Move into this new directory

    cd terminal_practice
    

    This command changes your current directory to the newly created “terminal_practice” folder. cd-terminal-practice.png

  5. Create some subdirectories

    mkdir Documents
    mkdir Projects
    mkdir Personal
    

    Create three new directories (Documents, Projects, and Personal) using the $dir$ command inside the “terminal_practice” folder. mkdir-documents-projects-personal.png

  6. Use the ls command to verify our setup

    ls
    

    The ls command lists the contents of the current directory. You should see the three directories we just created: Documents, Projects, and Personal. three-directories-ls.png

Now, let’s learn some advanced navigation tricks. We’ve used cd to move into directories, but navigating a complex directory structure requires more finesse. Let’s explore this with a hypothetical directory structure:

terminal_practice/
├── Documents/
│   ├── Reports/
│   │   └── quarterly_report.txt
│   └── Memos/
│       └── team_update.txt
├── Projects/
│   ├── WebDev/
│   │   └── index.html
│   └── DataAnalysis/
│       └── data.csv
└── Personal/
    ├── Photos/
    │   └── vacation.jpg
    └── Notes/
        └── ideas.txt

Let’s learn how to navigate this structure, assume that we are in “terminal_practice” directory:

  1. Start by moving into the Documents directory

    cd Documents
    

    This command changes your current directory to the Documents subdirectory.

  2. Now, let’s move into the Reports subdirectory

    cd Reports
    

    You’re now in the Reports directory, two levels deep from terminal_practice.

  3. To move back up to the Documents directory

    cd ..
    

    The .. notation represents the parent directory. This command moves you up one level in the directory structure.

  4. To move all the way back to terminal_practice from Reports

    cd ../..
    

    This moves up two levels at once: first out of Reports, then out of Documents.

  5. Now, let’s move to the DataAnalysis directory with a single command

    cd Projects/DataAnalysis
    

    This navigates into Projects, then into DataAnalysis in one step.

  6. To move from DataAnalysis to Photos, we can combine going up and down

    cd ../../Personal/Photos
    

    This goes up two levels (to terminal_practice), then down into Personal and Photos.

  7. Verify where you are

    pwd
    

    The pwd command, which stands for “print working directory”, shows your current location in the file system. You should see /path/to/terminal_practice/Personal/Photos.

  8. You can also use absolute paths to jump to any directory from anywhere. For example, to go directly to the Memos directory from anywhere:

    cd ~/terminal_practice/Documents/Memos
    

    The ~ represents your home directory, so this works regardless of your current location.

Remember, you can always use ls to list the contents of your current directory if you’re unsure of where you are or what’s around you.

I hope it’s clear how you can efficiently navigate complex directory structures using cd with relative and absolute paths

Creating Files: The Touch Command (touch)

Now that we can navigate effectively, let’s create some files to work with. We’ll use the touch command for this purpose.

The touch command is a simple tool used to create new, empty files. Its basic format is:

touch filename

When you use touch with a filename that doesn’t exist, it creates a new, empty file with that name. If the file already exists, touch updates its timestamp without changing its content.

Let’s practice using the touch command:

  1. Move to the Documents folder

    cd Documents
    

    This changes your current directory to Documents. cd-documents.png

  2. Create a new file:

    touch note.txt
    

    The touch command creates a new, empty file named “note.txt” in the current directory. touch-note-txt.png

  3. Verify that the file was created:

    ls
    

    This lists the contents of the current directory. You should see “note.txt” listed. ls-note-txt.png

  4. Let’s create files in Projects and Personal as well:

    cd ../Projects
    touch project_ideas.txt
    

    This moves to the Projects directory and creates a new file named “project_ideas.txt”. touch-project-ideas-txt.png

    cd ../Personal
    touch todo.txt
    

    This moves to the Personal directory and creates a new file named “todo.txt”. touch-todo-txt.png

Moving Files: Organizing With the mv Command

Now let’s learn how to move files using the mv command. The mv command is used to move files or directories from one location to another. Its basic format is:

mv source destination

Here, source is the file or directory you want to move, and destination is where you want to move it to.

Let’s practice using the mv command:

  1. Move to the Documents folder:
    cd ../Documents
    
    This changes your current directory back to Documents. cd-personal-to-documents.png
  2. Move the note.txt file from Documents to Personal:
    mv note.txt ../Personal
    
    The mv command moves “note.txt” from the current directory (Documents) to the Personal directory. The .. means “up one directory level”. mv-note-personal.png
  3. Check if the move was successful:
    ls
    
    This should show that “note.txt” is no longer in the Documents directory. ls-documents-mv.png
    cd ../Personal
    ls
    
    This moves you to the Personal directory and lists its contents. You should see “note.txt” listed here now. mv-personal.png Remember, when using mv, be careful with your source and destination paths. If you specify an incorrect path, you might move files to unintended locations. Always double-check your paths before executing the command.

The mv command can also be used to rename files, we’ll cover this in a later section in this article.

Copying Files: Creating Backups With the cp Command

Sometimes, you want to create a copy of a file. For this, we use the cp command. The cp command allows you to duplicate files or directories. Its basic format is:

cp source destination

Here, source is the file you want to copy, and destination is where you want the copy to be placed (this can be a directory or a new filename).

Let’s practice using cp command:

  1. Copy the todo.txt file from Personal to Documents:

    cp todo.txt ../Documents
    

    This creates a copy of “todo.txt” in the Documents directory while leaving the original in place. cp-todo-documents.png

  2. Verify the copy:

    ls
    

    This should show “todo.txt” still present in the Personal directory. ls-cp-personal.png

    cd ../Documents
    ls
    

    This moves you to the Documents directory and lists its contents. You should now see “todo.txt” here as well. cp-documents-todo-txt.png Remember, cp doesn’t move the original file, it creates a duplicate. This means you can safely create copies without affecting the original file. However, be cautious not to overwrite existing files unintentionally.

Renaming Files: Giving Files Meaningful Names

To rename files, we also use the mv command. While mv is primarily used for moving files, it can also rename files when used within the same directory. The basic format for renaming is:

mv oldname newname

Here, oldname is the current name of the file, and newname is the new name you want to give it.

Let’s practice renaming files with the mv command:

  1. Rename todo.txt in the Documents folder:
    mv todo.txt tasks.txt
    
    This renames the file “todo.txt” to “tasks.txt” in the current directory (Documents). mv-todo-tasks.png
  2. Check the result:
    ls
    
    You should now see “tasks.txt” instead of “todo.txt” in the Documents directory. mv-ls.png Remember, when renaming files with mv, you’re essentially “moving” the file to a new name in the same directory. Be careful not to accidentally move the file to a different directory by specifying a path in the new name.

Deleting Files and Directories: Cleaning Up Your Workspace

As you organize your digital space, you’ll often need to remove files or directories that are no longer needed. Linux provides several commands for this purpose, but it’s crucial to use them carefully as deleted items cannot be easily recovered.

Removing Files with rm

The rm command (short for “remove”) is used to delete files. Its basic syntax is:

rm filename

Let’s practice using the rm command:

  1. Navigate to the Documents folder:
cd ~/terminal_practice/Documents
  1. Create a test file:
touch test_file.txt
  1. Verify the file was created:
ls

rm-touch-test-file.png You should see “test_file.txt” listed

  1. Now, let’s delete the file:
rm test_file.txt

rm-test-file.png 5. Verify the file was deleted:

ls

“test_file.txt” should no longer be listed.

Removing Empty Directories with rmdir

To remove an empty directory, use the rmdir command:

rmdir directory_name

Let’s try it out:

  1. Create a new directory:
mkdir empty_folder

rmdir-mkdir.png 2. Now, remove it:

rmdir empty_folder

rmdir-empty-folder.png 3. Verify it’s gone:

ls

Note: rmdir only works on empty directories. If the directory contains files or other directories, you’ll need to use rm -r instead.

Removing Non-Empty Directories with rm -r

To remove a directory and all its contents, use rm with the -r (recursive) flag:

rm -r directory_name

Let’s practice:

  1. Create a directory with a file in it:
mkdir test_dir
touch test_dir/file.txt

rm-r-touch.png 2. Now, remove the directory and its contents:

rm -r test_dir

rm-r-test-dir.png 3. Verify it’s gone:

ls

Safety Precautions: The -i Flag

When deleting files or directories, it’s easy to accidentally remove something important. To add a layer of protection, you can use the -i (interactive) flag, which will prompt you for confirmation before each deletion:

rm -i filename
rm -ri directory_name

For example:

touch important_file.txt
rm -i important_file.txt

This will prompt you with “rm: remove regular empty file ‘important_file.txt’?“. You’ll need to respond with ‘y’ to confirm deletion.

Remember, in Linux, deletion is typically permanent. There’s no Recycle Bin or Trash to recover from, so always double-check before deleting!

Directory Manipulation: Mastering Folder Management

We’ve already learned how to create basic directories with mkdir, but let’s explore some more advanced directory operations.

Creating Nested Directories with mkdir -p

The -p flag allows you to create parent directories as needed. This is useful for creating a whole directory structure in one command:

mkdir -p parent_dir/child_dir/grandchild_dir

Let’s try it:

  1. Navigate you Projects directory:
cd ../Projects

nested-directories.png 2. Create Nested Directories:

mkdir -p Projects/WebDev/css
ls Projects
ls Projects/WebDev

ls-nested-directories.png

This creates a “WebDev” directory inside “Projects”, and a “css” directory inside “WebDev”, all in one command.

Moving Directories with mv

Just like with files, you can use mv to move entire directories:

mv source_directory destination_directory

Let’s practice:

  1. Create a test directory:
mkdir test_move

mkdir-test-move.png 2. Move it into the Projects directory:

mv test_move Projects/

mv-test-projects.png 3. Verify the move:

ls Projects

You should see “test_move” listed under Projects. mv-test-move.png

Copying Directories with cp -r

To copy a directory and all its contents, use cp with the -r flag:

cp -r source_directory destination_directory

Let’s try it:

  1. Copy the WebDev directory:
cp -r Projects/WebDev Projects/WebDev_backup

cp-webdev.png

  1. Verify the copy:
ls Projects

You should now see both “WebDev” and “WebDev_backup” cp-ls-projects.png

Practice Mission: Organizing Your Digital Space

Let’s put all these skills together:

  1. Navigate to the terminal_practice directory
  2. Create a new directory structure: Projects/PythonDev/scripts
  3. Create a file named “hello_world.py” in the scripts directory
  4. Copy the entire PythonDev directory to a new directory named PythonDev_backup
  5. Move the PythonDev_backup directory into the Documents directory
  6. Delete the original PythonDev directory from Projects
  7. Use ls after each step to verify your actions

Wrapping Up

Congratulations! You’ve just expanded your file management toolkit with some powerful new skills:

These commands form the backbone of file and directory manipulation in Linux. With practice, you’ll be able to efficiently organize and manage your digital workspace entirely from the command line.

Remember, the power of these commands also means they can be destructive if used carelessly. Always double-check your commands, especially when using rm, and consider using the -i flag for an extra layer of safety.

In our next lesson, we’ll dive into text processing and output redirection, opening up even more possibilities for working with your files and data in the terminal.

If you found yourself enjoying working in the terminal today, you might want to give Warp a try. Its modern interface and helpful features like auto-suggestions and clear output formatting can make your terminal experience even more enjoyable and productive. And remember, it’s completely free to use! You can download Warp here if you’re interested in enhancing your terminal experience.

Show Off Your Terminal Mastery!

You’ve accomplished a lot today! Why not share your achievements?

  1. Complete the practice mission
  2. Take a screenshot of your terminal showing the commands you used
  3. Post it on Twitter or LinkedIn
  4. Tag me (@introvertedbot on Twitter)

Keep exploring, and I’ll see you in the next lesson!

Share this post on: