How to use 'cp' command to exclude a specific directory
Last Updated :
25 Oct, 2024
The cp command is used to create an exact copy of the contents of files and directories but with a different name and location. It works using the terminal.
Syntax
cp [source_file/directory] [destination]
Where cp is used for the copy, source_file provides the exact file or directory or folder from where you want to copy the contents; destination provides the location where you want to copy the file.
Basic Example
To copy the contents of a file to another file in the same directory using the "cp" command:
Step 1: We will list all the files and directories using the ls command.
ls
Listing contents of directoryStep 2: Create a file f1.txt and display its contents using the cat command.
cat > f1.txt
Hello, this is a sample file.
Press : Ctrl + D
Creating and writing into f1.txtStep 3: If we want to copy the file in the same directory, we use the following syntax:
cp f1.txt f2.txt
cp f1.txt f2.txt outputHere, the contents of file1 is copied to file2.
Step 4: To check that, type the ls command
ls
Copying f1.txt contents to f2.txtStep 5: To find out the contents of file2.txt,
cat f2.txt
Displaying contents of f2.txt fileKey Options for the cp Command
We will enlist brief working of all the commands utilized for our example:
Command | Usage |
---|
cd | To change the directory from the current directory - cd .. will move one level up in the hierarchy
- cd directoryName will go to the specified directory
|
---|
mkdir | Will make a directory/folder in the current folder. We can create multiple directories in the same line separated by space. Ex: mkdir d1 d2 d3 |
---|
ls | Will list all the files and directories in the current one ls -A will list all files and directories including the hidden one (starting with ".") |
---|
cat | Will create a new file - cat>f1.txt creates new text file f1 and opens in writable mode
- cat f1.txt displays the contents of file f1
|
---|
cp | Copy command Copies the contents of one file to another in either the same or the other directory |
---|
Usage of the cp command to exclude a specific directory
Example: To copy all the files from one directory to another except a specific one using cp command:
Step 1: Let's create 2 empty directories: source (src) and destination (dest).
mkdir src dest
ls
Creating directory src and destStep 2: Change the directory to src using cd command and create two files f1.txt and f2.txt in the src directory using the cat command.
cd src
cat > f1.txt
This is file 1
Press : Ctrl + D
cat > f2.txt
This is file 2
Press : Ctrl + D
Creating f1.txt and f2.txt using cat commandStep 3: Check the contents of the dest using the ls command. Currently, it has 0 files/folders.
cd ..
cd dest
ls
Check contents of dest directoryStep 4: Now use the cp command to copy all files and folders from src except f2.txt to the folder dest.
cp src/(!f2.txt) dest
ls dest
copy all files and folders from src except f2.txt to the folder dest
Conclusion
The cp command is a powerful tool for copying files and directories in Linux. By using various options and techniques, you can exclude specific files or directories from being copied, making it a versatile command for file management tasks.
Similar Reads
How to Copy Files and Directories in Linux | cp Command The cp (copy) command is your go-to tool in Linux for duplicating files and folders quickly. Whether youâre backing up data, organizing files, or sharing content, cp lets you copy items between locations while keeping the original intact. The cp command requires at least two filenames in its argumen
8 min read
Copy all files from one directory to another using Python Copying files from one directory to another involves creating duplicates of files and transferring them from one folder to another. This is helpful when organizing files, backing them up, or moving them to different locations on your computer. Letâs explore various methods to do this efficiently.Usi
2 min read
How to Copy a Directory in Linux There is this famous quote for Linux by Jamie Zawinski that goes like this "Linux is only free if your time has no value." Certainly, that's true as people start using Linux they have to learn more about it, and when encountering it we have to solve it by ourselves, and that way we learn more about
5 min read
How to Copy a File to Multiple Directories in Linux In this article, we will discuss how to copy a file to multiple directories in Linux. xargs, find, tee, shell loop, and parallel are the commands that can help you to copy a File to Multiple Directories. Suppose we have a file with the name "xyz.txt" and we want to copy it into 3 directories with th
5 min read
How to Create Directory If it Does Not Exist using Python? In this article, We will learn how to create a Directory if it Does Not Exist using Python. Method 1: Using os.path.exists() and os.makedirs() methods Under this method, we will use exists() method takes path of demo_folder as an argument and returns true if the directory exists and returns false if
2 min read
Copy Multiple Directories with One Command System administration and development must have their files and directories well administered. One such way of doing this that is really effective is the task of copying multiple directories together, which actually saves one from consuming a huge amount of time and decreases the possibility of erro
7 min read