Creating, Copying, and Moving Directories (and Files)

Minicourse: The Linux Command Line for Beginners | Lesson 4

Welcome back, aspiring developers and coding enthusiasts! In this edition of The Tome of Zeal, we continue our journey through the fascinating realm of the Linux command line. We will pick up where we left off in our Minicourse: The Linux Command Line for Beginners. Today, we'll delve into some essential commands that will empower you to manage directories and files with ease. By the end of this lesson, you will understand how to:

  • Create directories with the mkdir command
  • Copy directories and files with the cp command
  • Move and/or rename directories and files with the mv command

Creating Directories with mkdir

As you embark on your coding adventures, organizing your files and directories (folders) becomes crucial. With the mkdir command, you can effortlessly create new directories in the Linux environment. Just follow this syntax:

mkdir my_dir_name

In the above example, we use mkdir to create a new directory named “my_dir_name” in the present working directory. Using the commands we learned about in Lesson 2, we can verify that the directory now exists.

Using some commands to verify the new directory was created

If you want to create a new directory with another new directory inside of it, you can do something like the following:

mkdir parent_directory/child_directory

Copying Files and Directories with cp

At times, you may find the need to duplicate files or even entire directories. Luckily, the cp command makes this task a breeze. To grasp its functionality, let's start by creating a file within the directory we just made:

cd my_dir_name
touch my_new_file.txt

In the above code snippet, we utilize cd to navigate into the newly created directory and touch to generate a new file named my_new_file.txt within that location.

Now that we have something to duplicate, let's explore the cp command. Its syntax is straightforward, as shown below:

cp source_file_name copied_file_name

For instance, to copy my_new_file.txt and name the duplicate my_copy.txt, execute the following:

cp my_new_file.txt my_copy.txt

Notably, you can copy files to and from various locations on your filesystem by utilizing paths instead of solely relying on filenames. Suppose you wish to copy a file from your Downloads folder to a backups folder, which is a subdirectory of your $HOME directory. The command might resemble this:

cp ~/Downloads/my_download.txt ~/backups/copy_of_my_download.txt

Observe the usage of the ~ symbol, which is shorthand for the path to the user's $HOME directory.

Since we employ paths as arguments for the cp command instead of just source and destination filenames, this command can be executed from any location, irrespective of the present working directory.

Now, let's take it a step further and copy the directory we created in this lesson (containing my_new_file.txt and my_copy.txt) into a new directory named copy_here:

cp -r my_dir_name copy_here

The Recursive Flag

The -r flag in the cp command stands for "recursive." It is used to copy directories and their entire contents, including subdirectories and files within them. When you want to copy a directory and everything it contains, including nested directories and files, you need to use the -r flag.

The -r flag ensures that the command copies not only the directory itself but also all the files and directories within it. Without the -r flag, the cp command would treat my_directory as a regular file and not copy its contents.

It's important to note that when using the -r flag, the target directory (copy_of_my_directory in the example above) should not already exist. If it does exist, the contents of the source directory will be copied into the existing target directory without creating a new directory.

In summary, the -r flag in the cp command allows you to recursively copy directories and their contents. It is a powerful tool when you need to duplicate directory structures and maintain the integrity of the files and subdirectories within them.

Running some familiar commands to navigate, view files, and copy an entire directory.

Moving and Renaming Files and Directories with mv

Moving and renaming files and directories is another essential skill when it comes to managing your files effectively. The mv command allows you to accomplish both tasks with ease.

To move a file or directory, use the following syntax:

mv source_path destination_path

For instance, let's say you want to move the file `my_new_file.txt` from the `my_dir_name` directory to the parent directory. You can execute the following command:

mv my_dir_name/my_new_file.txt .

The . represents the current directory, so running this command will move the file up one level.

Renaming Files and Directories

To rename a file or directory, you can also use the mv command. Simply provide the original name as the source path and the desired new name as the destination path. Here's an example:

mv my_dir_name my_directory

In this case, the my_dir_name directory will be renamed to my_directory.

Additionally, you can combine the move and rename operations in a single mv command. For example:

mv my_dir_name my_directory/new_directory

This command will move the my_dir_name directory and rename it as new_directory within the my_directory folder.

Remember, the mv command can be a powerful tool for managing files and directories, but exercise caution when using it, as it will overwrite existing files with the same name in the destination path.

Wrapping Up

Congratulations! You have now learned essential commands for creating directories, copying files and directories, as well as moving and renaming them using the Linux command line. These skills will significantly enhance your ability to organize and manage your coding projects effectively.

Stay tuned for the next edition of The Tome of Zeal, where we will explore more powerful commands and techniques to deepen your mastery of the Linux command line. Happy coding!


Join Our Discord!

If you have any questions or topics you'd like us to cover in future newsletters, feel free to reach out and let us know. We love hearing from our readers and want to provide you with the most valuable content possible.

Also, be sure to join the official Tome of Zeal Discord Server! It is the best place to reach out with questions you might have, share your experience with others, and find support as you pursue your coding goals.