File Forge

Swati VermaSwati Verma
3 min read

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.

  1. Creates an empty file.

     touch <file-name>
    
  2. 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).

    1. Creating a directory

       mkdir my_folder
      
    2. 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.

  1. Removing a file

     rm file.txt
    
  1. 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
    

  2. 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
    

  3. Delete empty directories: find . -type d -empty -delete

  4. Removing multiple files

  5. Removing files starting with x : * → wildcard.

  6. 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.

  1. To copy file1.txt to file2.txt.

     cp file1.txt file2.txt
    

  1. To copy a file to a directory.

     cp file.txt /path/to/directory
    
  2. Copies the entire folder1 (including its contents) to folder2. 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.

  1. Renaming file or directory.

     mv oldname.txt newname.txt
    
  2. 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.

  1. Output Redirection (> and >>) : Redirects command output to a file instead of displaying it on the terminal.

Usage:

  • Overwrite (>): Saves the output of ls into file.txt. If file.txt already exists, it overwrites the content.

      ls > file.txt
    

  • Append (>>): Appends the output of ls to file.txt without overwriting existing content.

      ls >> file.txt
    

    1. 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
      

0
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!