hi,

Is it possible to run a gedit command in the terminal that will create a text file automatically and save it in my desired file extension?

For example, if I run:

gedit file.txt

by default, it will open a null document named file.txt.

But all I want is to write and save texts to file.txt automatically so that it wont be NULL.

what should I type in the terminal?
tnx....

Dani AI

Generated

is right: the clean way is to make the file (with whatever initial content you want) and then open it in your GUI editor. also correctly points out that shell tools can create files and put text into them. gedit itself is an interactive GUI — it will not non-interactively "write and save" content for you unless the file already contains that content. The usual pattern is: create/populate the file from the shell, then launch the editor for further interactive work.

A minimal reusable shell script that creates the file (making parent directories if needed), writes an initial string, and then opens the file in your preferred GUI editor:

#!/bin/sh
file="$1"
shift
[ -n "$file" ] || { echo "Usage: $0 <file> [initial text]"; exit 1; }
mkdir -p "$(dirname -- "$file")"
printf '%s\n' "$*" >"$file"
${VISUAL:-${EDITOR:-gedit}} "$file" &

Notes and cautions: quote file names to handle spaces; the script overwrites an existing file — add a check if you need to avoid clobbering; backgrounding the editor with & keeps the terminal usable; use environment variables VISUAL or EDITOR to choose another editor if you prefer. For purely noninteractive creation (no editor open at all), just write the file from the shell and skip the final launch step.

Recommended Answers

All 2 Replies

Write a shell script that creates the file for you, then opens it using gedit.

If you just wanna create file without any contents use the command that is meant for it. :)
Use "mkfile", or simply "touch" if you don't wanna complicate things.
If you want to write something in the file use redirection.
Finally, next time you might wanna use Unix Shell Scripting forums.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.