File Forge


File Operations in Linux
Linux provides several commands to create, manage, and delete files and directories efficiently. Below are some essential file operation commands:
Creating Files - touch
The touch command is used to create an empty file or update the timestamp of an existing file.
Creates an empty file.
touch <file-name>
Create Multiple files
touch file1.txt file2.txt file3.txt touch file{1..10}.txt
Creating Directories – mkdir
The mkdir (Make Directory) command is used to create new directories (folders).
Creating a directory
mkdir my_folder
Creates a nested directory structure. The -p option ensures that parent directories are created if they don’t exist.
mkdir -p my_folder/sub_folder
Removing Files & Directories – rm
The rm (Remove) command deletes files or directories permanently.
Removing a file
rm file.txt
Deleting a directory and all its contents.
-r stands for "recursive". When used with
rm
, it means that if you're deleting a directory,rm
will also delete all the files and subdirectories inside that directory.rm -r my_folder
Removing empty directories - rmdir
If a directory contains any files or subdirectories, rmdir will not delete it and will produce an error message.
It is a safe way to remove directories as it won't delete the directory if it contains any content.
rmdir directory-name
Delete empty directories: find . -type d -empty -delete
Removing multiple files
Removing files starting with x : * → wildcard.
Delete empty files.
find . -type f -empty -delete
Copying Files & Directories – cp
The cp (Copy) command copies files or directories from one location to another.
To copy
file1.txt
tofile2.txt
.cp file1.txt file2.txt
To copy a file to a directory.
cp file.txt /path/to/directory
Copies the entire
folder1
(including its contents) tofolder2
. The-r
flag is required for copying directories.cp -r folder1 folder2
Moving & Renaming Files – mv
The mv
(Move) command is used to move files or directories to a new location or rename them.
Renaming file or directory.
mv oldname.txt newname.txt
Moving a file to a directory.
mv file.txt /home/user/Documents/
Redirection Operations
It allows you to control the flow of input and output between commands, files, and devices. It is useful for saving command outputs to files, reading from files, or combining multiple commands.
- Output Redirection (
>
and>>
) : Redirects command output to a file instead of displaying it on the terminal.
Usage:
Overwrite (
>
): Saves the output ofls
intofile.txt
. Iffile.txt
already exists, it overwrites the content.ls > file.txt
Append (
>>
): Appends the output ofls
tofile.txt
without overwriting existing content.ls >> file.txt
Input Redirection (
<
): Takes input from a file instead of typing manually in the terminal.Usage:
Sorts the contents of
file.txt
and displays the sorted output.sort < file.txt
Subscribe to my newsletter
Read articles from Swati Verma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Swati Verma
Swati Verma
Growing in DevOps, together! 🤝 | Associate Software Engineer at Tech Mahindra | Enthusiastic about automation, cloud solutions, and efficient software delivery. | Let's connect, collaborate, and learn from each other!