To copy files and directories in Bash, you can use the cp
command.
To copy a single file, use the following syntax:
cp source_file destination_file
For example, to copy a file named file.txt
to a new file called file_copy.txt
, you would run:
cp file.txt file_copy.txt
To copy multiple files into a destination directory, use the following syntax:
cp source_file1 source_file2 ... destination_directory
For example, to copy files file1.txt
and file2.txt
into a directory called my_dir
, you would run:
cp file1.txt file2.txt my_dir/
To copy an entire directory and its contents, use the -r
option (which stands for recursive). This is necessary to copy directories because directories can contain other files and subdirectories:
cp -r source_directory destination_directory
For example, to copy a directory named my_dir
and all its contents into a new directory called my_dir_copy
, you would run:
cp -r my_dir my_dir_copy
Note that if the destination directory does not exist, a new directory with that name will be created during the copy process.