<\/span><\/h2>\nTo create a file with “cat” execute the following command in some directory on your server:<\/p>\n
cd \/opt\r\n\r\ncat > testfile.txt<\/pre>\nEnter some text and press CTRL+D to save the content, of the newly created file.<\/p>\n
To check if the file is created successfully list the content of that directory:<\/p>\n
ls -al \/opt<\/pre>\n<\/span>2. Check File Content<\/span><\/h2>\nTo check the content of a file, you can execute the following command:<\/p>\n
cat \/etc\/apache2\/sites-enabled\/mydomain.conf<\/pre>\nYou will receive the output on the command line directly:<\/p>\n
<VirtualHost *:80>\r\n\r\nServerName mydomain.com\r\nServerAlias www.mydomain.com\r\nDocumentRoot \/var\/www\/html\/\r\n\r\nErrorLog ${APACHE_LOG_DIR}\/mydomain.com_error.log\r\nCustomLog ${APACHE_LOG_DIR}\/mydomain.com_access.log combined\r\n\r\n<\/VirtualHost>\r\n<\/pre>\n<\/span>3. Filter Specific Content<\/span><\/h2>\nSometimes, we need some specific lines of the file to be visible as output and this can be done with the cat and grep commands combined.<\/p>\n
cat \/etc\/apache2\/sites-enabled\/mydomain.conf | grep DocumentRoot<\/pre>\nYou should receive the following output:<\/p>\n
DocumentRoot<\/b> \/var\/www\/html\/<\/pre>\n<\/span>4. Print Line of Numbers<\/span><\/h2>\nTo print the line of numbers in the file execute the following command:<\/p>\n
cat -n testfile.txt<\/pre>\nYou will receive output with numbers before every new line:<\/p>\n
root@vps:\/opt# cat -n testfile.txt\r\n 1 Lorem Ipsum is simply dummy text of the printing and typesetting industry.\r\n 2 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.\r\n 3 It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.\r\n 4 It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.\r\n<\/pre>\n<\/span>5. Display Content of Multiple Files<\/span><\/h2>\nWe will create three files, test1.txt, test2.txt, and test3.txt with the content “Test file 1”, “Test file 2”, and “Test file 3” respectively. Once created execute the following command to display the content of all three files at once:<\/p>\n
cat test1.txt test2.txt test3.txt<\/pre>\nYou will receive the following output:<\/p>\n
root@vps:\/opt# cat test1.txt test2.txt test3.txt\r\nTest file 1\r\nTest file 2\r\nTest file 3\r\n<\/pre>\n<\/span>6. Reverse Displaying of the Content<\/span><\/h2>\nFirst, we will create a file with the following content and order:<\/p>\n
First line.\r\nSecond line.\r\nThird line.\r\n<\/pre>\nTo display the content of the file in reverse order, execute the following command:<\/p>\n
tac order.txt<\/pre>\nYou should receive the following output:<\/p>\n
root@vps:\/opt# tac order.txt\r\nThird line.\r\nSecond line.\r\nFirst line.<\/pre>\n<\/span>7. Append Text to File<\/span><\/h2>\nTo append a new text to the existing file execute the cat >> order.txt<\/b> command, enter the text, and press CTRL + D<\/b> to save the changes.<\/p>\ncat >> order.txt\r\nThis is a new line. This is the fourth line in the file.\r\n<\/pre>\nNow, check the content of the file with appended text.<\/p>\n
cat order.txt<\/pre>\nYou should receive the following output:<\/p>\n
root@vps:\/opt# cat order.txt\r\nFirst line.\r\nSecond line.\r\nThird line.\r\nThis is a new line. This is the fourth line in the file.\r\n<\/pre>\n<\/span>8. Display the head of the file<\/span><\/h2>\nTo display the head of the file, use the following command:<\/p>\n
cat testfile.txt | head -number<\/pre>\nWhere number<\/b> defines how many lines should be displayed from the beginning of the file.<\/p>\ncat testfile.txt | head -2<\/pre>\nYou should receive the following output:<\/p>\n
root@vps:# cat testfile.txt | head -2\r\nLorem Ipsum is simply dummy text of the printing and typesetting industry.\r\nLorem Ipsum has been the industry's standard dummy text ever since the 1500s.<\/pre>\n