How can I copy, move or rename files in Linux using the cp, mv and rename commands?

Published on Aug. 22, 2023, 12:20 p.m.

Here are some examples of how to use the cp, mv, and rename commands in Linux:

To copy a file named “file.txt” to a directory named “new_dir”:

cp file.txt new_dir/

To move a file named “file.txt” to a directory named “new_dir”:

mv file.txt new_dir/

To rename a file named “old_name.txt” to “new_name.txt”:

rename 's/old_name/new_name/' old_name.txt

(Note that for the rename command, you need to provide a “search pattern” and a “replace pattern” for the file name)

Here are some additional examples of how to use the cp, mv, and rename commands in Linux

Certainly! Here are some additional examples of how to use the cp, mv, and rename commands in Linux:

To copy all files in the current directory to a subdirectory named “backup”:

cp * backup/

To move all files in the current directory to a subdirectory named “old”:

mv * old/

To rename all files in the current directory with the “.txt” extension to have a “.docx” extension:

rename 's/.txt$/.docx/' *.txt

(Note that for the rename command, we use a regular expression to search for all files with the “.txt” extension)

Tags: