Organizing Files – mkdir, mv, cp

  • mkdir (make directory) – Creates a new directory or folder
  • mv (move) – Moving files from one directory to another
  • cp (copy) -Copying files from one directory to another

My Downloads folder is a complete mess right now. It contains lots of photos, PDFs, pem files, and a lot! In this tutorial, I’ll be showing you how to use mkdir, mv, and cp commands while sorting these files in different folders. Normally to create a folder, you go to a directory, make folders by clicking and naming, and then sort the files by marking them one by one and then move them. It’s painful when dealing with lots of files. But you’ll be using the terminal to do the job very quickly and efficiently!

Making Directories with mkdir

The command format is:

$ mkdir path

2 things to note here. Paths can be either relative or absolute, and the path contains the new directory/folder name that you wish to create.

mkdir with absolute path

Open your terminal. Right now you’re at the /home directory. You want to create a folder named PDFs within the Downloads directory. To do this using an absolute path, you need to specify the path starting from the home directory or root directory till your new folder. You can use any of these. Try them all, later you’ll learn how to delete unwanted folders.

$ mkdir /home/<user>/Downloads/PDF1 # replace <user> with your user name
$ mkdir ~/Downloads/PDF2

Both of these are the same, as you’ve already known from previous tutorials that /home/<user> and ~ are the same.

up 1

When to use absolute path

Say you’re working at /home/Desktop/Programming directory. But you need to create a new folder at /home/Downloads directory. So you must first need to change your directory to /home and then enter /Downloads folder, and create the new folder. Although this is a clean and easy process, it’s not fast and efficient. In that case, you can use an absolute path. The most attractive part is that, you’re still in your /home/Desktop/Programming directory even after creating a new directory at /home/Downloads/new-folder! Let’s see everything in action.

up1 2

mkdir with relative path

Say you’re at /Downloads directory, and want to create a file here. Instead of using an entire path to the new directory, you can truncate the path. Here’s how:

~/Downloads$ mkdir ~/Downloads/folder1 # using absolute path
~/Downloads$ mkdir folder2 # using relative path

Why did it work? Because the current working directory and the targeted directory have a common part. In short, when you’re creating a directory inside your current working directory, you can use only the part of the path you needed. It’s the same as changing directory with cd.

up2 2

Creating multiple folders with mkdir

Using mkdir once for every folder can be time-consuming when you’re trying to create a bunch of folders. You can, however, use curly brackets with mkdir to create multiple folders at once. Just remember to separate them by commas. You can use either absolute or relative paths inside the curly brackets, even a combination of both of them!

Caution: Do not use spaces around the commas. Otherwise mkdir will consider the spaces as part of the folder names.

up3 5 1

Moving Files with mv

To move file or files from one path to a different path, you can use mv. The command format is as follows:

$ mv from_path to_path

Depending on what directory you’re working at, both of the paths can either be absolute or relative or a mix of them. For example, I’m currently in the /Downloads folder. I want to move the art.jpg file from here to the /Downloads/Photos folder, i.e. Photos folder is inside my current working directory. I can apply any of the commands:

~/Downloads$ mv art.jpg Photos
~/Downloads$ mv art.jpg ~/Downloads/Photos
~/Downloads$ mv ~/Downloads/art.jpg Photos
~/Downloads$ mv ~/Downloads/art.jpg ~/Downloads/Photos

While practicing these commands, you may need to get art.jpg back from Photos folder to the Downloads folder. For that, you can do this:

~/Downloads$ mv Photos/art.jpg .

Remember single dot(.) represents current directory?

Moving multiple files

Use curly brackets as before to move multiple files at once. Don’t use any space inside the curly brackets.

up4 3 1

Remember, not showing any output is the sign of successful completion of the command.

Moving one kind of all files

There’s a bunch of PDF files in my /Downloads directory. What if I want to move them all at once to the PDF folder? In this case, using regex or regular expression comes in handy. In regex, wildcard character (*) represents one or more any characters. So *.pdf means any file name that ends with .pdf. Here’s how you can move all the PDF files.

~/Downloads$ mv *.pdf PDF

The PDF folder must be created before using this command.

In case you’re in the /home directory, you could do this:

~$ mv Downloads/*.pdf Downloads/PDF
up6 2
Showing both of the processes

Moving all files from a folder

You may have guessed from the previous picture, what could be the procedure. As wildcard (*) represents any one or more characters, if you just apply *, it will represent any file, as extensions are also characters. Here’s how you can move all files from PDF folder back to the Downloads folder. Say you’re at /home directory:

$ mv Downloads/PDF/* Downloads 

Moving multiple file types

In the Downloads folder, there are a bunch of photos. I want to move them to a single folder named Photos, which is inside the Downloads directory. But the problem is, there are multiple file types for photos, e.g. jpg, png, jpeg, etc. So, I want to move ‘all files’ that match ‘multiple’ file types. Then what can I do? I can combine wildcard and curly braces! From my /home directory:

$ mv Downloads/*.{jpg,png,jpeg} Downloads/Photos
up7 1

In my case, there were no jpeg files, that’s why I got this error only for jpeg files. Other files moved perfectly!

Moving Files that contains spaces in file name

If a file name contains spaces, how would you move them? You’ve already seen that spaces are said to be excluded from all the commands. There is a way around. As you’ve learned from the fourth tutorial, single quotes are used to convert a string into a raw string. So, either wrap the entire path with single quotes, or the file name part. Here’s the example showing both of them:

$ mv Downloads/'LM user guide.pdf' Downloads/PDF
$ mv 'Downloads/CSE question.pdf' Downloads/PDF
up8 1

Moving an entire folder

You can move the entire folder from one place to another using the same process you moved files. Say I want to move the PDF1 and PDF2 folders to the PDF folder. Here’s how:

~/Downloads$ mv PDF1 PDF2 PDF

Here I’m not using curly brackets for moving multiple files. Instead, file names are separated just by spaces. Notice, mv will consider the last path as target folder and everything before that as the source folders.

up8 3

How to see what files are being moved

If you want, you can see what files are being moved from where to where. For that, you need to use arguments. Arguments and options will be discussed in a later tutorial, but to learn more, use man mv command. Here you’ll use the ‘v’ argument. Let’s move the .pem files from Downloads to a new folder named PEM.

~/Downloads$ mv -v *.pem PEM
up9 1

Copying Files with mv

Everything about copying files is same as what you saw in moving files. The command format is:

$ cp from_path to_path

Same as before, you can use either absolute or relative paths or a combination of both of them when defining the paths. There’s a Linux Mint user guide available in PDF format in my ~/Downloads/PDF directory. I want to keep a copy of it in my ~/Desktop directory. Here’s how it can be done from the home directory:

~$ cp Downloads/PDF/'LM user guide.pdf' Desktop

Copying multiple files with cp

You can pass multiple file paths to cp. It will consider the last path as a destination, and all the previous ones as sources. If I want to copy c.jpg, d.png, s.jpg files from my /Downloads/Photos directory to /Desktop directory, I can do this:

~$ cp Downloads/Photos/{c.jpg,d.png,s.jpg} Desktop

Copying directories with cp

Same as before, pass directories as source arguments, and another directory as destination argument. But you must add an extra -r option with cp to copy directories. r or R stands for recursive. It means the cp command will recursively copy files from source to destination until all files are copied. To copy PDF1, PDF2 folders from ~/Downloads/PDF directory, and Files folder from ~/Downloads, to Desktop directory:

~$ cp -r Downloads/PDF/{PDF1,PDF2} Downloads/Files Desktop

Renaming Files with mv, cp

When you copy or move files to a directory, say Secret, if there are files previously with the same names, then cp and mv will overwrite the contents of those previously existed files. You can use this idea to rename files. Say I want to rename the file in my ~/MLwiki directory to As the source and destination are the same here, if I use mv, then will be overwritten. But if I use cp, will exist, but a different file will be created. Let’s see:

~MLwiki$ cp
~MLwiki$ mv

Overwriting can be dangerous sometimes, erasing important content previously stored with the same name. To be careful, you can pass -i option with cp and mv. i stands for Interactive. It prompts for a response from you if there’s a situation of overwriting a file. If you give an input that starts with y, e.g. y, yes, yo, yeah, then the file will be overwritten. Any other input will skip the process of overwriting.

Moving from previous folder to current folder

There’s a file in my Desktop folder, named I want to keep a copy of it in MLwiki folder. But there’s already a file in MLwiki with the same name. Then how can I do it without affecting anything? I can rename it when copying. Remember, in this process, if the filename given in the path does not exist, cp or mv will create it. Here you can see, MLwiki doesn’t have any file. I want to copy the file from Desktop to here and give it the name

up11 1


  • Tabs can be used as a command completion shortcut. Write ‘Dow’ and press tab, it’ll complete ‘Downloads’ if ‘Downloads’ directory is present inside your current working directory.
  • If pressing tab does not complete what you’re trying to achieve, rather give you another name, then press tab again. It happens when there is more than one matching string to ‘Dow’, or whatever you wrote.

Errors and Cautions

  • You’re in /home directory. You want to create a new directory /home/bash/commands, but the directory /home/bash does not exist. Then you can’t create the commands folder before creating the bash folder.
  • If you try to create a directory that already exists, or move a file that does not exist, then bash will return an error.
  • If you run multiple commands at once, and some of them return errors, it doesn’t mean all your commands won’t work. Only the valid commands will be executed, and errors will appear for invalid commands.
  • Don’t use any space inside curly brackets when applying multi commands at once. For example, mv -v *.{jpg,jpeg, png} Photos will return a complete error and won’t move anything.

Mind Sharing It?
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others