How to Retrieve the Contents of a File in Linux 


               


How to Retrieve the Contents of a File in Linux



Linux


In this article, we show how to retrieve the contents of a file in linux.

So linux, just like a programming language such as Python, is capable of reading the contents of a file.

The contents of a file can be retrieved onto the linux command terminal.

We can get the contents of a file in linux using the statement, cat, followed by the filename.

After we do this, the contents of the file will appear in the linux shell.

So I'm going to create a file called myday.txt.

In this file, I write the line, "My day has been really good so far"

You can view this file at the following link: myday.txt.

We will now read the contents of this file, myday.txt, from the Linux command terminal. This is shown in the following code below.



So the first thing you do is make sure you're in the right directory in order to retrieve the file or that the path you specified is correct.

Once you're in the directory, then simply specify, cat followed by the name of the file.

This will retrieve the contents of the file onto the command terminal.

Running the following code above, we get the following output shown below.


Retrieving the contents of a file in linux


So you see we have, "My day has been really good so far", which is what is in the myday.txt file.

Now let's do it one other way.

Let's suppose we're reading a file other than from the current working directory.

In this case, we have to specify the path to the file relative from our current working directory.

So let's go back one directory with the command, cd ..

We are now in the home directory, even though we want to read a file from the home/david/ directory.

So what we do is we specify, cat david/myday.txt

This retrieves the content from the myday.txt.

This is shown in the following code below.


Retrieving the contents of a file from a different directory in linux


So now you see above that we read the contents of the myday.txt file from a directory other than the current working directory.

And this is how to retrieve the contnets of a file in linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...