Fri. Aug 7th, 2020

[Linux basic] Copy, delete, and move commands for Linux files

2 min read

cp (copy) command

The cp command copies files from one location to another. If the destination file exists, it will be overwritten; if the destination directory exists, the file will be copied to that directory.

Example:

Copy a file:

cp file1 file2

The above cp command copies the contents of the file1 to the file2.

Back up the copied files:

cp -b file1 file2

Copy folders and subfolders:

cp -R sqlmap sqlmap1

The above cp command copies folders and subfolders from sqlmap to sqlmap1.

mv (move) command

Use it to move/rename files from one directory to another. The mv command is different from the cp command. The mv command completely moves the file from the source and moves the file to the specified directory. The cp command just copies the contents of the file from one file to another.

Example:

To rename/move a file:

mv file1.txt file2.txt

This command renames the file1.txt file to file2.txt.

To move a directory

mv sqlmap tmp

In the above command, if the tmp directory already exists, the mv command will move all files, directories, and subdirectories under the sqlmap folder/directory to the tmp directory. If there is no tmp directory, it will rename the sqlmap directory to the tmp directory.

Move multiple files to another directory

mv file1.txt tmp / file2.txt newdir

This command moves the file1.txt file in the current directory and the file2.txt file in the tmp folder/directory to the newdir directory.

rm delete command

The rm command is used to remove/delete files in a directory.

Example:

Remove/delete files:

rm file1.txt

Here the rm command will remove/delete the file file1.txt.

Delete the directory tree:

rm -ir tmp

Here the rm command recursively deletes the contents of all subdirectories under the tmp directory. You will be prompted for the deletion of each file, and then delete the tmp directory itself.

Delete multiple files at once.

rm file1.txt file2.txt

The rm command deletes both file1.txt and file2.txt.