How do you read a file in Linux?

Open file from linux terminal

In this part you will see in a very fast and direct way how to create and read text files from the command line, include lines of text in a file, or print its content on the screen in order to preview it. To do this, you will learn how to use commands such as touch, cat or more, and also how to defend yourself fairly well with Nano, a file editor in text mode, like Gedit but to be used from the terminal.

Before we start, this post is part of a series of articles focused on deepening the use of the Linux command interpreter. At the end of the post you have linked the rest of the posts. That said, let’s get started!

As in the previous cases, I am going to show a list with some examples of the most used commands when creating and reading text files from Bash. I will start with the case of creating an empty text file, for which I will use the touch command.

The touch command will allow you, among other things, to create an empty text file (in .txt format) to which, later, you will be able to add the content you want manually, or by using other commands that we will see.

Command to open files in linux

The type command allows you to display the contents of a text file on the console. The command is used as follows: type filename, where filename is the name of an existing file. If the filename includes its directory path, the file will be searched for in the specified path. If only the filename is included, the working directory will be searched.

Read more  Why do feeding tubes turn black?

The contents of the working directory can be displayed using the windows-style dir command or the linux-style ls command. Both commands support a mask that allows you to specify the type of file you are looking for. For example, we could use dir *.m to display a list of files with the .m extension. In the mask, the arterisk acts as a wildcard. We could read the above command as: ‘dir lo-que-sea dot m’.

We can also change the working directory from the Octave console. To do this we use the cd command, ‘change directory’, which supports a parameter below to specify the path we want to access. There is a shortcut you need to know. If we type cd .., cd dot dot dot, we will move to the parent directory of the directory in which we are positioned. To access a certain directory we will use: cd dirname, where dirname is the name or the complete path of the directory in which we want to be positioned.

Open files from the ubuntu terminal

It is well known that everything, absolutely everything that can be done with a Linux computer can be done from its terminal, also known as console. Today we are going to dive into what is probably one of the most used commands inside the terminal: list files with the Linux ls command. The ls command comes from the English word list, which means something like listing. This is not the first time that we analyze terminal-related topics, but in the other occasions it was in a slightly geekier way. For example, making a train move, watching a Star Wars movie, getting the Matrix effect or directly getting insulted by typing bad code.

Read more  What are ICD guidelines?

On a curious note, and very relevant within the free software community itself, this ls command was written by Richard Stallman himself and David MacKenzie. As would be completely expected, its release license is GPLv3+.

Very easy, the ls command in our console is used to put a list of the files we have in any folder of our Linux system. Nothing more, nothing less. You will see that listing files with the Linux ls command is so easy to use, that you will rarely resort to the graphical version.

Command to edit files in linux

One of the most common errors in scripting in Linux arises when you want to read a file line by line. There is a tendency to use the for loop, which results in an evaluation of each word and not of each line, which is what you are looking for. In this article we show a more effective method.

However, we can also obtain the same result with a for loop if we modify the value of the variable “$IFS” (Internal Field Separator) before executing the loop. This is what we will see next.

From a structured file (such as an address book or /etc/passwd for example), we can also obtain the values of each field and assign them to several variables with the “read” command. However, care must be taken to assign the “IFS” variable the appropriate field separator (space by default).

While it is true that the “while” loop is the simplest method, it has a major drawback: it eliminates line formatting and especially spaces and tabs. The “for” loop, associated with an IFS change, makes it possible to preserve the document structure at the output.

Read more  Who should complete the incident documentation form?