-
To copy a file:
cp file1.txt file2.txt
- If
file2.txtdoes not exist, it will be created, and the content will be copied fromfile1.txt. - If
file2.txtalready exists and contains data, its data will be overwritten with the content offile1.txt.
- If
-
Interactive Mode (-i option):
- If you want confirmation before overwriting, use the
-ioption (interactive):Example:cp -i file1.txt file2.txt
cp -i a.txt b.txt cp: overwrite 'b.txt'?
- If you want confirmation before overwriting, use the
-
Verbose Mode (-v option):
- To see verbose output (showing what is being copied), use the
-voption:Example:cp -v file1.txt file2.txt
cp -v a.txt b.txt 'a.txt' -> 'b.txt'
- To see verbose output (showing what is being copied), use the
-
Copying Multiple Files:
-
You can copy multiple files to a directory, but not to a single file:
cp file1.txt file2.txt file3.txt directory/
-
Note: To concatenate multiple files into one, use the
catcommand, notcp:-
Invalid:
cp a.txt b.txt c.txt d.txt
This gives the error:
cp: target 'd.txt' is not a directory. -
Valid:
cat a.txt b.txt c.txt > d.txt
-
-
Both moving and renaming can be performed using the mv command.
1. Renaming Files:
- To rename a file:
Example:
mv oldname newname
This renamesmv file1.txt file2.txt
file1.txttofile2.txt.
2. Renaming Directories:
- To rename a directory:
This renames
mv dir1 dir2
dir1todir2.
3. Moving Files from One Directory to Another:
-
To move all files from
dir1todir2:mv dir1/* dir2After executing this command,
dir1will be empty, and its files will be moved todir2.
4. Moving an Entire Directory to Another Directory:
- To move the entire
dir1todir2:This movesmv dir1 dir2
dir1(and its contents) intodir2.
5. Confirmation Before Overwriting:
-
To get a confirmation prompt before overwriting files, use the
-ioption:mv -i a.txt d.txt dir1
Example output:
mv: overwrite 'dir1/a.txt'?
This provides a safeguard before overwriting files when moving them.
Directories:
- Source:
/home/durga/Desktop/x - Destination:
/home/durga/Desktop/y
Directory Creation Command:
mkdir -p x/x1/x1{1,2} x/x2 y/y1/y1{1,2} y/y2Assume file1.txt is available inside the x/x1/x11 directory, and we are currently in the user home directory. To copy this file to the y/y2 directory, you can use either an absolute path or a relative path.
To copy using the absolute path, you can use the following commands:
cp /home/durga/Desktop/x/x1/x11/file1.txt /home/durga/Desktop/y/y2/or
cp ~/Desktop/x/x1/x11/file1.txt ~/Desktop/y/y2/To copy using the relative path, you can use the following commands:
cp ./Desktop/x/x1/x11/file1.txt ./Desktop/y/y2/or
cp Desktop/x/x1/x11/file1.txt Desktop/y/y2/- Absolute Path: Specifies the complete path to the file from the root directory.
- Relative Path: Specifies the path to the file relative to the current working directory.
Assume file1.txt is available in the x/x1/x11 directory, and we are currently in the user home directory. To move this file to the y/y1/y11 directory, you can use either an absolute path or a relative path.
To move using the absolute path, use the following commands:
mv /home/durga/Desktop/x/x1/x11/file1.txt /home/durga/Desktop/y/y1/y11/or
mv ~/Desktop/x/x1/x11/file1.txt ~/Desktop/y/y1/y11/To move using the relative path, use the following commands:
mv ./Desktop/x/x1/x11/file1.txt ./Desktop/y/y1/y11/or
mv Desktop/x/x1/x11/file1.txt Desktop/y/y1/y11/