Linux file operations
---
title: "Linux File Operations"
category: "linux-tutorials"
description: "Commands for creating, editing, copying, moving, renaming, and deleting files and directories."
---
# Linux File Operations
This tutorial covers essential Linux commands for managing files and directories via the command line interface (Shell). You will learn how to create, edit, copy, move, rename, and delete files and directories.
Most Linux commands follow a common syntax:
```bash
command -options arguments
Note: Linux is case-sensitive. Command names are typically lowercase, and filenames like MyFile
and myfile
are treated as distinct.
Creating and Editing Files
Using nano
nano
is a simple, beginner-friendly text editor commonly used in Linux environments to create or edit files.
To create a new file or open an existing one for editing:
nano filename.txt
For example, to create or edit a file named costdata.dat
:
nano costdata.dat
Inside the nano
editor, you'll see the file content area and a list of commands at the bottom. The ^
symbol represents the Ctrl
key.
- Save: Press
Ctrl+O
(Write Out) and confirm the filename by pressing Enter. - Exit: Press
Ctrl+X
. If you have unsaved changes,nano
will ask if you want to save them.
Copying Files
The cp
command is used to copy files or directories.
Syntax:
cp source_file destination_file
Example: To make a backup copy of costdata.dat
named costdata.dat.bak
:
cp costdata.dat costdata.dat.bak
This creates a new file costdata.dat.bak
with the same content as costdata.dat
. The original file costdata.dat
remains unchanged.
Moving and Renaming Files
The mv
command is used to move files or directories from one location to another, or to rename them.
Syntax (Renaming):
mv old_filename new_filename
Example: To rename the file costdata.dat.bak
to newcostdata.dat
:
mv costdata.dat.bak newcostdata.dat
Syntax (Moving):
mv source_file destination_directory/
Example: To move newcostdata.dat
into a directory named archives
:
mv newcostdata.dat archives/
Deleting Files
The rm
command is used to remove (delete) files.
Syntax:
rm filename
Example: To delete the file costdata.dat
:
rm costdata.dat
Warning: Files deleted with rm
are permanently removed. There is no "Trash" or "Recycle Bin" in the standard Linux command line. Exercise caution when using rm
.
Using Wildcards with rm
The asterisk (*
) is a wildcard character that matches any sequence of characters (including no characters). It can be powerful but dangerous when used with rm
.
Example: To remove all files in the current directory whose names start with costdata
:
rm costdata*
Important: Be VERY careful when using wildcards with rm
. You might permanently delete more files than intended. It is highly recommended to first list the files that match the pattern using ls
before deleting them:
ls costdata* # Check which files will be matched
rm costdata* # Only run this after verifying the ls output
Creating Directories
The mkdir
command is used to create new directories (folders).
Syntax:
mkdir directory_name
Example: To create a new directory named study1
:
mkdir study1
You can also create directories within other directories, forming a hierarchy.
Example: To create a directory costProject
in your current location:
mkdir costProject
Deleting Directories
rmdir
(for empty directories)
The rmdir
command is used to remove empty directories.
Syntax:
rmdir directory_name
Example: To remove the empty directory study1
:
rmdir study1
If the directory contains any files or subdirectories, rmdir
will fail.
rm -r
(for non-empty directories)
To remove a directory and all its contents (files and subdirectories), use the rm
command with the -r
(recursive) option.
Syntax:
rm -r directory_name
Example: To remove the directory costProject
and everything inside it:
rm -r costProject
Warning: Using rm -r
is powerful and permanently deletes the directory and its entire contents. Double-check the directory name before executing this command.
```