Skip to main content

Command Palette

Search for a command to run...

Activate the "New Document" option with specific file extensions in Ubuntu 22.04

Published
2 min read
Activate the "New Document" option with specific file extensions in Ubuntu 22.04
M

Software Engineer@Datasoft Systems Bangladesh Limited BSc. in CSE

Here's how you can do it:

  1. Open a terminal by pressing Ctrl + Alt + T or searching for "Terminal" in the application launcher.

  2. Navigate to your templates directory by running the following command:

  •   cd ~/Templates
    
  • Create blank files with the desired file extensions. Run the following commands to create the required files:

  •   touch New Text Document.txt
      touch New Presentation Document.odp
      touch New Word Document.docx
      touch New Excel Document.xlsx
      touch New CSV Document.csv
    
  • Now, we'll create a script that allows you to create new files with these extensions from the right-click menu.

    Run the following command to create the script file:

  •   touch create_new_document.sh
    
  • Open the script file using a text editor:

  •   nano create_new_document.sh
    
  • Add the following lines to the script:

  •   #!/bin/bash
    
      file_ext=""
      case "$1" in
          "Text file") file_ext=".txt";;
          "Presentation file") file_ext=".odp";;
          "Word file") file_ext=".docx";;
          "Excel file") file_ext=".xlsx";;
          "CSV file") file_ext=".csv";;
          *) exit 1;;
      esac
    
      touch "${2}/${file_ext}"
    
  • Save the file by pressing Ctrl + O, and then exit the text editor by pressing Ctrl + X.

  • Make the script executable by running the following command:

  •   chmod +x create_new_document.sh
    
  • Next, we need to create a create_new_document.desktop file to add the script to the right-click menu.

    Run the following command to create the create_new_document.desktop file:

  •   touch create_new_document.desktop
    
  • Open the create_new_document.desktop file using a text editor:

  •   nano create_new_document.desktop
    
  • Add the following lines to the file:

  •   [Desktop Entry]
      Name=Create New Document
      Exec=/path/to/create_new_document.sh "%M" "%f"
      Icon=document-new
      Type=Action
      Categories=FileManagerActions;
      MimeTypes=inode/directory;
    

    Make sure to replace /path/to/create_new_document.sh with the actual path to the create_new_document.sh script file you created earlier.

  • Save the file by pressing Ctrl + O, and then exit the text editor by pressing Ctrl + X.

  • Move the .desktop file to the ~/.local/share/file-manager/actions directory by running the following command:

  •   mkdir -p ~/.local/share/file-manager/actions
      mv create_new_document.desktop ~/.local/share/file-manager/actions/
    
  • Finally, restart the file manager for the changes to take effect. You can do this by logging out and logging back in, or by running the following command:

nautilus -q

After completing these steps, you should see the "Create New Document" option with the specified file extensions when you right-click in any folder. Selecting the option will create a new file with the appropriate extension in the current directory.