File Operations
Anonymous contributor
Published Mar 17, 2025
Contribute to Docs
File operations in Bash refer to commands that allow users to create, delete, move, copy, and manipulate files and directories using the command line.
Syntax
Creating Files
To create a new file:
touch filename
Copying Files
To copy a file from one location to another:
cp source_file destination
Moving/Renaming Files
To move or rename a file:
mv source_file destination_filemv old_name new_name
Deleting Files
To remove a file:
rm filename
Creating Directories
To create a new directory:
mkdir directory_name
Removing Directories
To remove an empty directory:
rmdir directory_name
To remove a directory with its contents recursively:
rm -r directory_name
Example
Consider a scenario where someone wants to create a directory, move a file into it, and then delete the file:
# Create a new directory named 'project'mkdir project# Create a new file named 'notes.txt'touch notes.txt# Move 'notes.txt' into 'project'mv notes.txt project/# Remove 'notes.txt' located in 'project'rm project/notes.txt# Remove the 'project' directoryrmdir project
After running these commands, the directory project
and its file notes.txt
will no longer exist.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Command Line on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn the Command Line
Learn about the command line, starting with navigating and manipulating the file system, and ending with redirection and configuring the environment.With CertificateBeginner Friendly4 hours