How to open files in terminal linux

How to open, create, edit, and view a file in Linux

One thing GNU/Linux does as well as any other operating system is give you the tools you need to create and edit text files. Ask ten Linux users to name their favorite text editor, and you might get ten different answers. On this page, we cover a few of the many text editors available for Linux.

GUI text editors

This section discusses text editing applications for the Linux windowing system, X Windows, more commonly known as X11 or X.

If you are coming from Microsoft Windows, you are no doubt familiar with the classic Windows text editor, Notepad. Linux offers many similar programs, including NEdit, gedit, and geany. Each of these programs are free software, and they each provide roughly the same functionality. It’s up to you to decide which one feels best and has the best interface for you. All three of these programs support syntax highlighting, which helps with editing source code or documents written in a markup language such as HTML or CSS.

NEdit

NEdit, which is short for the Nirvana Editor, is a straightforward text editor that is very similar to Notepad. It uses a Motif-style interface.

The NEdit homepage is located at https://sourceforge.net/projects/nedit/. If you are on a Debian or Ubuntu system, you can install NEdit with the following command:

For more information, see our NEdit information page.

Geany

Geany is a text editor that is a lot like Notepad++ for Windows. It provides a tabbed interface for working with multiple open files at once and has nifty features like displaying line numbers in the margin. It uses the GTK+ interface toolkit.

The Geany homepage is located at http://www.geany.org/. On Debian and Ubuntu systems, you can install Geany by running the command:

Gedit

Gedit is the default text editor of the GNOME desktop environment. It’s a great, text editor that can be used on about any Linux system.

The Gedit homepage is located at https://wiki.gnome.org/Apps/Gedit. On Debian and Ubuntu systems, Gedit can be installed by running the following command:

Terminal-based text editors

If you are working from the Linux command line interface and you need a text editor, you have many options. Here are some of the most popular:

pico started out as the editor built into the text-based e-mail program pine, and it was eventually packaged as a stand-alone program for editing text files. («pico» is a scientific prefix for very small things.)

The modern version of pine is called alpine, but pico is still called pico. You can find more information about how to use it in our pico command documentation.

On Debian and Ubuntu Linux systems, you can install pico using the command:

nano is the GNU version of pico and is essentially the same program under a different name.

On Debian and Ubuntu Linux systems, nano can be installed with the command:

vim, which stands for «vi improved,» is a text editor used by millions of computing professionals all over the world. Its controls are a little confusing at first, but once you get the hang of them, vim makes executing complex editing tasks fast and easy. For more information, see our in-depth vim guide.

On Debian and Ubuntu Linux systems, vim can be installed using the command:

emacs

emacs is a complex, highly customizable text editor with a built-in interpreter for the Lisp programming language. It is used religiously by some computer programmers, especially those who write computer programs in Lisp dialects such as Scheme. For more information, see our emacs information page.

On Debian and Ubuntu Linux systems, emacs can be installed using the command:

Читайте также:  Восстановление файлов реестра для windows

Redirecting command output into a text file

When at the Linux command line, you sometimes want to create or make changes to a text file without actually running a text editor. Here are some commands you might find useful.

Creating an empty file with the touch command

To create an empty file, it’s common to use the command touch. The touch command updates the atime and mtime attributes of a file as if the contents of the file had been changed — without actually changing anything. If you touch a file that doesn’t exist, the system creates the file without putting any data inside.

For instance, the command:

The above command creates a new, empty file called myfile.txt if that file does not already exist.

Redirecting text into a file

Sometimes you need to stick the output of a command into a file. To accomplish this quickly and easily, you can use the > symbol to redirect the output to a file.

For instance, the echo command is used to «echo» text as output. By default, this goes to the standard output — the screen. So the command:

The above command prints that text on your screen and return you to the command prompt. However, you can use > to redirect this output to a file. For instance:

The above command puts the text «Example text» into the file myfile.txt. If myfile.txt does not exist, it is created. If it already exists, its contents will be overwritten, destroying the previous contents and replacing them.

Be careful when redirecting output to a file using >. It will overwrite the previous contents of the file if it already exists. There is no undo for this operation, so make sure you want to completely replace the file’s contents before you run the command.

Here’s an example using another command:

The above command executes ls with the -l option, which gives a detailed list of files in the current directory. The > operator redirects the output to the file directory.txt, instead of printing it to the screen. If directory.txt does not exist, it is created first. If it already exists, its contents will be replaced.

Redirecting to the end of a file

The redirect operator >> is similar to >, but instead of overwriting the file contents, it appends the new data to the end of the file. For instance, the command:

Источник

How to open a particular file from a terminal?

How do I open a file from a terminal? When I try to open a simple .txt file:

I get this message:

Have I used the wrong command? Or do you use different commands to open different file types?

3 Answers 3

You can use xdg-open to open files in a terminal.

From the man-page of xdg-open :

xdg-open — opens a file or URL in the user’s preferred application

Usage

The command xdg-open _b2rR6eU9jJ.txt will open the text file in a text editor that is set to handle text files. The command will also work with other common file extensions, opening the file with the relevant application.

/.bashrc file. Then, run source

/.bashrc and now you can use open instead of xdg-open .

You must use an editor to open a text file:

Any of those can do it:

Or if you wanted to just view the file without modifying its contents: cat _b2rR6eU9jJ.txt

EDIT #1: I just noticed that the question is tagged fedora, which up until now is using gnome as its core graphical user interface, which comes with gedit preinstalled. So this is guaranteed to work: gedit _b2rR6eU9jJ.txt

You should use an appropriate application to open it — try nano _b2rR6eU9jJ.txt or cat _b2rR6eU9jJ.txt . The former will edit, the latter will output it to standard output. (Note — you can replace nano with vi, emacs or other text editor of your preference)

From what I can tell

open refers to openvt — and the man page describes it as openvt — start a program on a new virtual terminal (VT). You’re trying to open a text file, so unless I’m missing something, it isn’t the software for doing what you want to do.

Not the answer you’re looking for? Browse other questions tagged linux terminal fedora or ask your own question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Как сделать свою панель задач windows

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Opening the file browser from terminal

What’s the command to open the file browser? I want it so that I can assign a keyboard shortcut to open a specific folder.

11 Answers 11

nautilus —browser will ensure that Nautilus is launched in browser mode even if you’re normally using it in spatial mode.

You can append the path you want to open to the end:

The gnome-open command will open a directory with the appropriate application, which in this case is Nautilus:

This will open the directory /tmp using the Nautilus file browser.

I like the gnome-open command because you can use this exact same command to open a file with the appropriate application. No need to remember any funny flags. It just works.

  • gnome-open file.pdf will open the PDF in a PDF browser.
  • gnome-open file.zip will open a zip file using the Zip archive viewer.

It’s also similar in name and function to the Mac OS X open command, for those of us who use Macs.

As of 2018, one can use the GIO commandline tool on Gnome:

Edit: Another option is xdg-open. Also take a look at this answer for further details.

For me the safest way that is compatible with almost all environments is xdg-open

This would open a directory named test (for example) under your home directory.

I put the following line in my .bashrc :

Now you can open with

For reference, I’m running Ubuntu Bionic 18.04.

The easiest and safest way I open the file explorer from command line is with the xdg-open command, which itself often aliased as the browse command if that’s more your style. xdg-open ships natively with Ubuntu.

xdg-open can also open any file or web URL, and will open it according to your computer’s default application for files of that filetype.

browse . Opens the file explorer in my current directory.

Does the same, but my home directory.

xdg-open https://www.google.ca Launches google’s homepage with your default browser (xdg-open will open it as a new tab if a browser session is already open).

Man pages for xdg-open can be found here

Note that the xdg-open command is not meant to be used with root priveleges.

Источник

How to open a folder in linux via terminal? [closed]

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago .

How can I open a folder in a dir?

say I change my current directory to: cd /root/dir/

then I list all the files there: ls folder1 folder2 folder3

Now I want to open folder1. If I try the «./» I get: ./folder1 bash: ./folder1: Is a directory

How can I do so without having to type cd again ie: cd $(pdw)/folder1

5 Answers 5

If you want to open the folder via the nautilus file manager, you can move to the wanted directory like you’ve mentioned cd /root/dir/ , check the folders under that path using ls and then if you want to open folder1 type:

./ is used to execute file (Not to open directory).

(In)CLI Method: You can open folder in terminal by cd folder1 or dir folder1 or ls folder1 .

(To)GUI Method: If you want to open with file-manager (ex:nautilus) then type nautilus folder1 (for Ubuntu nautilus is default file-manager)

I have found that simply typing gnome-open «any-oject» opens any folder or file in the default program on Ubuntu. If this happens to be a folder, it uses your default folder-explorer 🙂

zsh shell can do that with the AUTO_CD option.

Just put setopt AUTO_CD in your .zshrc file (start zsh one time first to create the zsh environment files). You can invoke directly zsh at the terminal prompt to start a zsh session or you can change your default shell to be zsh with the chsh command.

Btw this is not a strange feature, crossable directories do have the «execute» attribute so it makes sense to able to execute a directory like any standard commands.

Источник

How do I open a text file in my terminal?

There is a file named RESULTS.txt and I want to open this file in my terminal. (I mean I want to see the file contents be displayed in the terminal and not in some text editor)

How do I do that ?

Читайте также:  Wineskin для mac os catalina

16 Answers 16

For short files:

directly shows a text file in the terminal.

For longer files:

lets you scroll and search ( / text to search Enter ) in the file; press q to exit.

Another alternative is vim .

Once you opened a file with vim you can insert text by typing i , for instance. If you want to save your file use :w (write) or :q (quit) or :wq (for write and quit) or :q! (quit and do not save). Sometimes you need to hit the ESC key to be able to type the commands.

Vim requires some learning, but is widely used and it is very versatile.

Vim is an advanced text editor that provides the power of the de-facto Unix editor ‘Vi’ with a more complete feature set. Vim is often called a «programmer’s editor,» and is so useful for programming that many consider it an entire IDE. It’s not just for programmers, though. Vim is perfect for all kinds of text editing, from composing email to editing configuration files.

all those are best ways and there is one more way to do this & that’s with head command.

both will give you the same input.

Head command Explanation:

Generally head command used to print the starting lines of the any text file.we can view the text file with

That will prints the 1st 10 lines of the above text file.

If you want to specific on the number of lines which are to be view then you can use head as

Then in the above text file first 20 lines will be viewed.

If you want to view whole file data with head means then then we can get it by

Hope that above explanation will give you some idea on usage of head.

If the file is rather long, you might want to use

so that you can navigate through it with directional keys.

Another option is:

to print out the last 30 lines of a large file named result.txt .

It will show you the last ten lines of your_file . If a process appends something to this file, you see it on your terminal. man tail gives you more on tail .

It’s useful to see what happens with a server when you use this command on a log file.

Press Ctrl — C to quit when you are done viewing.

There are a lot of alternatives for doing that:

Some of these programs have a lot of parameters, so check that out with —help after the command..

  • cat filename prints the whole file at once
  • more / less filename similar behaviour for see the file in parts
  • tail filename start reading from the tail of the file
  • grep text filename for filtering results

Hope that some of this works for you..

With a terminal text editor: nano /path/to/file/RESULTS.txt

As we seem to be listing all available alternatives of displaying any text file in the terminal, it would be quite fun to introduce pv as technically one valid (but unusual) method, although I would normally use cat instead for most things.

It is in the repositories and so can be installed with sudo apt-get install pv if you don’t have it already.

As the man page notes, pv is very often used to

monitor the progress of data through a pipe. pv will copy each supplied FILE in turn to standard output (- means standard input), or if no FILEs are specified just standard input is copied. This is the same behaviour as cat(1).

With pv you can literally print the file to the screen, and choose the rate ( -L ) at which it appears. The example below uses a high rate (300), but if you choose a low rate such as -L 50 , it will appear as if the computer is typing out the file for you.

Needless to say you can increase the rate further ( -L 8000 ), and the command becomes very similar to cat , with the output appearing instantaneously.

For more information see man pv or the Ubuntu manpages online.

Источник

Оцените статью