In this tutorial, we’ll cover how to move, copy, and rename directories within Linux, using the terminal and some easy commands.
Use the table of contents below to find a specific section or read on to learn more!
How to Move a Directory in Linux
If you’re trying to move a directory in Linux from one location to another, just follow these 4 steps:
- If you want to move a directory or file in Linux, you can use the move command, followed by the Source (the directory you’re moving) and Destination (the directory you’re moving it to):
mv [Source] [Destination]
- So, if you wanted to move /Directory1 into a second directory name /Directory2, you would use the following command:
mv /Directory1 /Directory2
*Note: If no “/Directory2” exists, /Directory1 will be renamed as “/Directory2”, essentially “moving” it into a newly created directory.
- This can also be done for multiple files and directories at once. Let’s say you have 3 files (File1, File2, and File3), and you’d like to move all them into /Directory1. Simply list out all your sources before your destination, and Linux will automatically move all these files at once.
mv File1 File2 File3 /Directory1
- Keep in mind that in order to move files and directories, a user must have permissions allowing them to modify both the source and destination directories. Otherwise, an error will occur and permission will be denied.
How to Copy a Directory in Linux
If you’re trying to make a copy of an existing directory, just follow these 4 steps:
- To copy a directory or file in linux, just use the copy command followed by the Source (the directory you’re copying), and the Destination (the place you’re copying it to):
cp -R [Source] [Destination]
- So, if you wanted to make a copy of /Directory1 and place it inside of /Directory2, you would use the command:
cp -R /Directory1 /Directory2
*Note: If no “/Directory2” exists, a new directory will be created, with that name, and containing the contents of /Directory1.
- Like moving files, it is possible to copy multiple files into a single directory by listing out multiple filenames for sources, followed by a single destination. For example:
cp -R File1 File2 Fil3 /Directory1
Which would copy File1, File2, and File3, placing their copies in /Directory1.
- Additionally, if you wanted to copy the entire contents of /Directory1 into /Directory2 (duplicating the files inside the folder but not the folder itself), you can do so by adding ” /* ” after the source directory. For example:
cp – R /Directory1/* /Directory2
This would create copies of all the files located in /Directory1, placing duplicates of its content into /Directory2, but not duplicating the actual directory itself.
How to Rename a Directory in Linux
If you need to rename an existing directory in Linux to something new, just follow these 2 steps:
- Interestingly, due to the way Linux’s file structures work, moving a file and renaming a file are actually the same action. This is because when you’re “moving” a file in Linux, you’re really “renaming” its file path: giving it a new name but also a new location in your system’s structure.
mv [Source] [NewName]
- So, to rename /Directory1 to /DirectoryNEW, you would use the command:
mv /Directory1 /DirectoryNEW
If “/DirectoryNEW” already exists, then this command will move the contents of /Directory1 to /DirectoryNEW. However, if “/DirectoryNEW” does NOT already exist, this command will rename /Directory1 to /DirectoryNEW.
- How to Check the Apache Version on a Linux Dedicated cPanel Server
- How to Change a Password in Linux
- How to Check if Your Linux Server is Under DDoS Attack
Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.
The Hivelocity Difference
With best-in-class customer service, affordable pricing, a wide-range of fully-customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.