DEV Community

Cover image for Linux Terminal for Beginners - Mastering File Manipulation
Sahil
Sahil

Posted on • Originally published at blog.theenthusiast.dev

Linux Terminal for Beginners - Mastering File Manipulation

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

  1. Navigate to your home directory
   cd ~
Enter fullscreen mode Exit fullscreen mode

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

  1. Create a new directory called "terminal_practice"
   mkdir terminal_practice
Enter fullscreen mode Exit fullscreen mode

The mkdir command stands for "make directory" and creates a new folder named "terminal_practice".

Image description

  1. Move into this new directory
   cd terminal_practice
Enter fullscreen mode Exit fullscreen mode

This command changes your current directory to the newly created "terminal_practice" folder.

Image description

  1. Create some subdirectories
   mkdir Documents
   mkdir Projects
   mkdir Personal
Enter fullscreen mode Exit fullscreen mode
Create three new directories (Documents, Projects, and Personal) using the `mkdir` command inside the "terminal_practice" folder.
Enter fullscreen mode Exit fullscreen mode

Image description

  1. Use the ls command to verify our setup
   ls
Enter fullscreen mode Exit fullscreen mode

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

Image description

Navigating Between Directories (cd)

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This command changes your current directory to the Documents subdirectory.

  1. Now, let's move into the Reports subdirectory
   cd Reports
Enter fullscreen mode Exit fullscreen mode

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

  1. To move back up to the Documents directory
   cd ..
Enter fullscreen mode Exit fullscreen mode

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

  1. To move all the way back to terminal_practice from Reports
   cd ../..
Enter fullscreen mode Exit fullscreen mode

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

  1. Now, let's move to the DataAnalysis directory with a single command
   cd Projects/DataAnalysis
Enter fullscreen mode Exit fullscreen mode

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

  1. To move from DataAnalysis to Photos, we can combine going up and down
   cd ../../Personal/Photos
Enter fullscreen mode Exit fullscreen mode

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

  1. Verify where you are
   pwd
Enter fullscreen mode Exit fullscreen mode

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.

  1. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This changes your current directory to Documents.

Image description

  1. Create a new file:
   touch note.txt
Enter fullscreen mode Exit fullscreen mode

The touch command creates a new, empty file named "note.txt" in the current directory.

Image description

  1. Verify that the file was created:
   ls
Enter fullscreen mode Exit fullscreen mode

This lists the contents of the current directory. You should see "note.txt" listed.

Image description

  1. Let's create files in Projects and Personal as well:
   cd ../Projects
   touch project_ideas.txt
Enter fullscreen mode Exit fullscreen mode

This moves to the Projects directory and creates a new file named "project_ideas.txt".

Image description

   cd ../Personal
   touch todo.txt
Enter fullscreen mode Exit fullscreen mode

This moves to the Personal directory and creates a new file named "todo.txt".

Image description

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This changes your current directory back to Documents.

Image description

  1. Move the note.txt file from Documents to Personal:
   mv note.txt ../Personal
Enter fullscreen mode Exit fullscreen mode

The mv command moves "note.txt" from the current directory (Documents) to the Personal directory. The .. means "up one directory level".

Image description

  1. Check if the move was successful:
   ls
Enter fullscreen mode Exit fullscreen mode

This should show that "note.txt" is no longer in the Documents directory.

Image description

   cd ../Personal
   ls
Enter fullscreen mode Exit fullscreen mode

This moves you to the Personal directory and lists its contents. You should see "note.txt" listed here now.

Image description
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This creates a copy of "todo.txt" in the Documents directory while leaving the original in place.

Image description

  1. Verify the copy:
   ls
Enter fullscreen mode Exit fullscreen mode

This should show "todo.txt" still present in the Personal directory.

Image description

   cd ../Documents
   ls
Enter fullscreen mode Exit fullscreen mode

This moves you to the Documents directory and lists its contents. You should now see "todo.txt" here as well.

Image description
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This renames the file "todo.txt" to "tasks.txt" in the current directory (Documents).

Image description

  1. Check the result:
   ls
Enter fullscreen mode Exit fullscreen mode

You should now see "tasks.txt" instead of "todo.txt" in the Documents directory.

Image description
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.

Practice Mission: Organizing Your Digital Space

Now, let's practice what we've learned:

  1. Navigate to the Projects folder and create a new file
  2. Make a copy of this file in Documents
  3. Move to Documents and rename the copy
  4. Move the original file from Projects to Personal
  5. Check the contents of all directories

Wrapping Up

Congratulations! You've just learned some powerful file management skills:

  • Using .. to navigate up directories
  • touch: Create empty files
  • mv: Move or rename files
  • cp: Copy files

Remember, practice makes perfect. Try using these commands to organize some of your own files. Start small and gradually work your way up to larger organizations.

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!

Top comments (0)