How to use ‘cp’ command to exclude a specific directory
cp command is used to create an exact copy of the contents of the files and directories but with a different name and location. It works using the terminal. The exact syntax goes like this:
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 to.
Usage of the cp command to exclude a specific directory
Example 1: 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

Step 2: Create a file f1.txt and display its contents using the cat command.

Step 3: If we want to copy the file in the same directory, we use the following syntax:
$ cp f1.txt f2.txt
Here, the contents of file1 is copied to file2.
To check that, type the ls command
ls

To find out the contents of file2.txt,
cat f2.txt

This is how the cp command works. It will copy the contents of a file or a directory to another one. We will understand the concept using the cp command to copy folders from one directory/location to other but excluding some specific folders/ directories.
We will enlist brief working of all the commands utilized for our example:
Command |
Usage |
---|---|
cd |
to change the directory from the current 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 |
Example 2: 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).
Syntax:
$ mkdir src dest
We will check the directories being created using the ls command
Syntax:
$ ls
Output:

Step 2: Change the directory to src using cd command.
Syntax:
$ cd src
We will create two files f1.txt and f2.txt in the src directory using the cat command.
Syntax:
$ cat>f1.txt
cat command allows us to create a file and write the contents in the console itself.
Similarly, we will create another file f2 in the same directory (src).
Output:

Step 3: Check the contents of the dest using the ls command. Currently, it has 0 files/folders.
Syntax:
$ cd .. [goes to one directory upwards]
$ cd dest [ move inside dest directory]
$ ls [ list the contents of dest directory]
Output:

Step 4: Now use the cp command to copy all files and folders from src except f2.txt to the folder dest.
Syntax:
$ cp src/(!f2.txt) dest
Check the files in the dest directory using the ls command
Syntax:
$ ls dest
Output:

So all the files(here f1.txt) is copied from the src folder to the dest folder using copy command except one file(f2.txt)
Please Login to comment...