How to copy a file with the linux command

how to copy a file with the linux command
Photo by Gabriel Heinzer / Unsplash

To copy a file with the Linux command line, you can use the cp command. This command takes two arguments: the source file and the destination file. For example, to copy a file named file1.txt to a file named file2.txt, you would use the following command:

Copy codecp file1.txt file2.txt

cp file1.txt file2.txt

This command will create a copy of file1.txt at the location specified by file2.txt. If file2.txt already exists, it will be overwritten by the new file.

If you want to copy a file to a different directory, you can specify the destination directory in the file2.txt the argument, like this:

Copy code

cp file1.txt /path/to/destination/directory/file2.txt

This will copy file1.txt to the /path/to/destination/directory/ directory, using the same file name (file2.txt) as the original.

You can also use the -r option to copy directories and their contents recursively. For example, to copy a directory named mydir and all of its contents to a new directory named mydir-copy, you would use the following command:

Copy code:

cp -r mydir/ mydir-copy/

This will create a new directory named mydir-copy and copy all files and subdirectories from mydir into it.

For more information about the cp command and its options, you can consult the cp man page by running the following command:

Copy codeman cp