Cat display file contents




















Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.

Visit chat. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. The most common use of the cat command is to output the contents of a file. The following is an example that you can try. In this simple example, we're using a combination of echo and a redirect to create a file containing "Dance, Dance". We then use the cat command to display the contents.

The previous example is actually a specific case of the cat command's main function, which is to concatenate files for display. If we use the command the same way, but give it two or more files, then it outputs the concatenation for the files. The output is the contents of the 1st file, followed by the contents of the 2nd file.

You can give cat many files and it will concatenate combine all of them. Notice however, that the cat command automatically inserts a line break between outputs. A complete breakdown can be found in the man pages. This is a less common usage of cat , but is the basis for the next section. If you run the cat command with no commands, cat will run in interactive mode and echo anything you type until you exit the command.

In the example here, I've typed a single word per line. Each time I hit enter, the line was echoed. In the previous examples, we've been using the echo command redirected to a file to create new files.

Cat can be used in a similar way. In fact, we can use cat 's concat and echo functionality to create files. In the above example, we're creating 3 files using echo , combining the 3 files into one using cat , and then displaying the new combined file using cat. We can also use cat 's interactive mode to create a file with the text that we type into the terminal. To exit the prompt and write the changes to the file, hold the Ctrl key and press d. Instead of displaying the contents of a file on the screen, cat can put them in a file.

If you run cat on test3. If a file is exported that already exists, this will overwrite the contents of the file :. The cat command can display the content of a file in reverse order by lines. To do this, use tac cat in reverse :. The cat command can add the contents of a file to the end of another file. Note: If you want to remove the sample files, take a look at how to remove files and directories using the Linux command line.

The functions of the cat command can be combined. For example, to combine the output of two files, and store the result in a new file:. Note that the order specified is the order the files in which they are added to the destination file. Note: Once you have created multiple files, you may want to group them in a single directory. Take a look at how to use mkdir command to make or create a Linux directory. You can break it into pages using more :.

You may find it useful to have line numbers in the output, especially for large files. To enable line numbering, add the -n option to the cat command:. Since the sample file test1. The cat command has the option of displaying the file content along with the tab space within the text. If you have trouble remembering the options, use the --help command:. You should now have a good understanding of how to use the cat command in Linux.



0コメント

  • 1000 / 1000