Copying, Moving and Renaming Files
So far, we've learned how to create, inspect, and remove files. The next step is learning how to copy files, move them between directories, and rename them.
Linux provides simple commands for these operations: cp and mv. We'll also see how cat can combine multiple files and how history helps us review commands we've already executed.
mv - Move or Rename
The mv command is used for both moving and renaming files.
Rename a File
When the destination is a new filename in the same directory, mv renames the file:
mv old_name.txt new_name.txtThe file is now called new_name.txt.
No separate rename command is required in Linux.
Move a File
You can also move a file into another directory:
mv notes.txt /home/ubuntu/documents/The file is removed from its original location and placed inside the destination directory.
How Does mv Know the Difference?
Linux determines the operation from the destination:
mv file.txt new_name.txtHere, new_name.txt is treated as the new filename.
mv file.txt documents/Here, documents/ is an existing directory, so the file is moved into it.
Moving and Renaming at the Same Time
You can do both operations with one command:
mv notes.txt documents/linux-notes.txtThis moves notes.txt into documents/ and gives it the new name linux-notes.txt.
cp - Copy a File
The cp command creates a copy of a file.
cp aliens.txt jenkins.txtThis creates jenkins.txt as a copy of aliens.txt.
If jenkins.txt doesn't exist, it is created.
If it already exists, its contents are normally overwritten by the source file.
Important:
cpcopies; it does not move the original file.
After:
cp aliens.txt jenkins.txtboth files exist independently.
Copy a File to Another Directory
cp notes.txt documents/The original notes.txt remains in the current directory, while a copy is created inside documents/.
Combining Files with cat
The cat command can also combine the contents of multiple files into a new file.
cat ansible.txt telusko.txt > shell.txtThe contents are written in this order:
ansible.txt
↓
telusko.txt
↓
shell.txtSo the content of ansible.txt appears first, followed by the content of telusko.txt.
If shell.txt already exists, > replaces its existing content.
To append the combined content instead:
cat ansible.txt telusko.txt >> shell.txtThis adds the output to the end of the existing file.
history - Review Previous Commands
The history command displays commands previously entered in the shell.
historyExample:
101 pwd
102 ls
103 cd projects
104 touch notes.txt
105 lsThis is useful when you want to:
- Review commands you've already run.
- Find a command you used earlier.
- Troubleshoot what you did during a session.
- Reuse a previous command.
Note: The exact history available depends on the shell and its configuration. Shell history can also be stored for use across sessions.
Quick Reference
| Command | Purpose |
|---|---|
mv old.txt new.txt | Rename a file |
mv file.txt directory/ | Move a file |
mv file.txt directory/new.txt | Move and rename a file |
cp source.txt copy.txt | Create a copy of a file |
cp file.txt directory/ | Copy a file to another directory |
cat file1 file2 > merged.txt | Combine files into a new file |
history | View previously executed shell commands |
Written By: Muskan Garg
How is this guide?
Last updated on
