Copying Files and Directories in PowerShell

Window

We've already created a few files and directories but we need a couple more. We don't want to create them all from scratch. So let's make copies instead. 🤖

In the Windows GUI, all you need to do is right-click, copy, then paste. You can also use hotkeys if you want. 🔨

A hotkey is a keyboard shortcut that does some sort of task in Windows. The hotkey for copy is Ctrl-C and for paste, it's Ctrl-V. 🔑

In PowerShell, the command used to copy something is CP. We also need to add a file that we want to copy and the path of where we want to copy it to. Let's copy mycoolfile.txt to the desktop. 💾

There you can see mycoolfile.txt was added to our desktop. 🖨️

Using Wildcards to Copy Multiple Files 🌟

I have a few of these files I want to move over but I'm feeling a little lazy and don't want to run this command over and over again. So I'm going to use something called the wildcard to help me copy over multiple files at once. 🤩

A wildcard is a character that's used to help select files based on a certain pattern. 🔍

Let's say you want to get all the files that were JPEGs and copy them somewhere. They go into my documents directory. I have files called hotdog.jpg, cotton_candy.jpg, and pretzel.jpg. 🖼️

I need to come up with a pattern to help me select all these files. What do they have in common besides being named after delicious food? The .jpg extension. 🍔

Literally, anything else can be in front of the .jpg file extension and it won't matter. That's what the wildcard asterisk * does. It's the pattern for anything. 🌟

So I'm essentially saying select all the files with the pattern anything.jpg. So to copy over all the JPGs in the folder, I can use cp *.jpg and the path I want to copy them to. 🔍

Let's just verify. There it is. Now instead of copying files one by one, we can use a single command to get all the files we want. 👌

Copying Directories with Contents 📂

For now, the only selector we'll be using is the asterisk * for all. Next up, let's say I want to copy over a directory. I'm going to try to copy a folder called "Bird Pictures" to my desktop. 🐦

Let's just go back into Documents. There's "Bird Pictures", now cp "Bird Pictures" Desktop.

Now, this does exactly what we told it to do. It copies the directory. However, This directory is empty. What it doesn't do is copy over the contents of the directory. 🤔

To copy over the contents of the directory, you need to use another command parameter, -Recurse. 🔁

The -Recurse parameter lists the contents of the directory. Then if there are any subdirectories in that listing, it'll recurse or repeat the directory listing process for each of those subdirectories. 📁

We'll need to use the -Recurse parameter with copy to copy the contents of the directory along with the directory itself. We're also going to use a new parameter, -Verbose. 📋

Copy doesn't output anything to the CLI by default unless there are errors. When we use copy -Verbose, it'll output one line for each file the directory being copied. 💬

Let's give it a try. cp "Bird Pictures" -Recurse -Verbose. This just message says that we've already copied Bird pictures. But what we didn't do was copy over the files, which are now here. 🎉

Excellent, now the directory and all the contents are copied to my desktop. 🖥️

Linux

🗂️ Copying Files in Bash

In Bash, the exact same Windows command can be used for copying files. Take a look at this directory. Let's copy my_very_cool_file.txt to my desktop. 🖥️ There it is. We can also use the same asterisk wildcard to select patterns.

💻 Copying Files with Wildcards Since this is similar to our Windows Copy command, what do you think we can use to copy over the .png files in this directory? I have files called Pizza.png, Soda.png, Cake.png. I can use cp *.png, then the desktop directory. 🖥️ Now if I look at my desktop again. There they are. The same copy rules apply in Bash.

📁 Copying Directories Recursively If we want to copy over directory, we have to recursively copy over the directory to get all the contents. The flag for recursive copy is -r. If I want to copy over my cat pictures folder to the desktop, I could do something like this. 🖥️ There it is.

Last updated