Copy, Move, and Delete Files and Directories
Linux provides powerful commands to manage files and directories. Here are the most commonly used commands for copying, moving, and deleting files. These commands are essential for file management in any Linux distribution, including Red Hat.
Copy Files and Directories
Command: cp
Copy a file
cp source.txt destination.txt- Copies
source.txttodestination.txt.
Copy to another directory
cp source.txt /path/to/dir/Copy multiple files
cp file1.txt file2.txt /path/to/dir/Copy a directory recursively
cp -r sourcedir/ targetdir/-ror--recursive: required to copy directories.
Useful flags
-v– Verbose, shows files being copied.-u– Copy only when the source file is newer than the destination.-i– Prompt before overwrite.
Move (Rename) Files and Directories
Command: mv
Move a file
mv file.txt /path/to/destination/Rename a file
mv oldname.txt newname.txtMove a directory
mv mydir/ /new/location/Useful flags
-v– Verbose output.-i– Prompt before overwrite.
Note:
mvcan also move files across filesystems.
Delete Files and Directories
Command: rm (remove)
Delete a file
rm file.txtDelete multiple files
rm file1.txt file2.txtDelete an empty directory
rmdir mydir/- Only works for empty directories.
Delete a directory and contents recursively
rm -r mydir/Force delete (no prompt)
rm -rf mydir/Useful flags
-r– Recursive (required for directories).-f– Force deletion (no confirmation).-i– Prompt before each file removal.-v– Verbose output.
Examples
cp -r docs/ backup_docs/ # Copy entire directory
mv report.txt reports/ # Move file to another folder
mv draft.txt final.txt # Rename a file
rm old.txt # Delete a file
rm -rf temp/ # Delete a directory with all contentsLast updated on