In LINUX files can be created by using any of the three methods given below:
1. Cat command (cat - CONCATENATION)
2. Touch command
3. Vi editor
1. cat command:
cat stands for CONCATENATE. This is the basic command when we start learning LINUX/UNIX, as the name suggest it is used to create new file, concatenate files and display the content of the files on the standard output.
Different examples of cat command which will be useful for the beginners.
1. Creating new file
2. View the contents of the file
3. View the contents of multiple files
4. Display the output of a file using page wise
5. cat command without filename arguments
6. Display the content of a file with line numbers
7. Copy the contents of a one file to another file
8. Appending the contents of one file to another file
9. Redirecting the output of multiple files into a single file.
10. Getting input using standard input operator
11. Storing the output of multiple files into a single file
12. Insert $ at end of each line using -E option
13. Show the tab space in the file as '^I' using -T option
14. Squeeze blank repeated lines using -s option
15. View the contents in the reverse order
16. Display non printing characters using -v option
Example:1 Create a new file using ‘cat > {file_name}’
Now remove the blank repeated lines in the output using below command.
[root@linuxtechi ~]# cat -s linux_blank
test
test1
test2
test3
test4
[root@linuxtechi ~]#
Example:15 View the Contents in Reverse Order
tac is the reverse of cat command. tac will display the output in reverse order example is shown below
[root@linuxtechi ~]# tac linux_world
Thanks
Linux always rocks
Hi this is my first file in linux.
[root@linuxtechi ~]#
Example:16 Display non-printing characters using -v option.
-v option in the cat command is used to show the non-printing characters in the output. This option become useful when we are suspecting the CRLF ending lines, in that case it will show ^M at the end of each line.
[root@linuxtechi tmp]# cat test_file
hi there
[root@linuxtechi tmp]# cat -v test_file
hi there^M
[root@linuxtechi tmp]#
cat stands for Concatenate. Cat is the basic command when we start learning Linux/Unix, as the name suggest it is used to create new file ,concatenate files and display the output of files on the standard output.
2. Touch Command
To create a zero byte file
# touch <filename>
To create multiple zero byte files
# touch <first file> <second file> <third file>
To change the timestamp of a file or directory
# touch <directory or filename>
3. Vi Editor
To create file
# vi <filename>
1. Cat command (cat - CONCATENATION)
2. Touch command
3. Vi editor
1. cat command:
cat stands for CONCATENATE. This is the basic command when we start learning LINUX/UNIX, as the name suggest it is used to create new file, concatenate files and display the content of the files on the standard output.
Different examples of cat command which will be useful for the beginners.
1. Creating new file
2. View the contents of the file
3. View the contents of multiple files
4. Display the output of a file using page wise
5. cat command without filename arguments
6. Display the content of a file with line numbers
7. Copy the contents of a one file to another file
8. Appending the contents of one file to another file
9. Redirecting the output of multiple files into a single file.
10. Getting input using standard input operator
11. Storing the output of multiple files into a single file
12. Insert $ at end of each line using -E option
13. Show the tab space in the file as '^I' using -T option
14. Squeeze blank repeated lines using -s option
15. View the contents in the reverse order
16. Display non printing characters using -v option
Example:1 Create a new file using ‘cat > {file_name}’
Let’s suppose i want to create a new file with name ‘linux_world’. Type the following cat command followed by the text you want in to insert in the file. Make sure you type ‘Ctrl-d’ at the end to save the file.
[root@venkat~]# cat > linux_world
Hi this is my first file in linux.
Linux always rocks
Thanks
[root@venkat~]#
Example:2 View the Contents of a File.
To display or view the contents of a file using cat command use the below syntax
# cat {file_name}
Let’s display the contents of linux_world file.
[root@venkat ~]# cat linux_world
Hi this is my first file in linux.
Linux always rocks
Thanks
root@venkat ~]#
Example:3 View the Contents of Multiple Files
[root@venkat ~]# cat linux_world linux_distributions /etc/fstab
Above command will display output of three files on the terminal.
Example:4 Display the output of a file using page wise.
For example if we have a big file whose contents can’t be display at once on the screen , in that case we can use more and less command with cat to view the contents page wise.
[root@venkat ~]# cat /etc/passwd | more
[root@venkat ~]# cat /etc/passwd | less
Example:5 cat command without filename arguments
if we don’t specify any arguments in the cat command then it will read the inputs from the keyboard attached to the system. Type some text after entering the cat command.
[root@venkat ~]# cat
Ubuntu Linux Rocks at desktop Level
Now press ‘Ctrl-d‘ to inform cat that it has reached end of file (EOF). In this case it will display the line of text twice because it copies std input to std output.
[root@venkat ~]# cat
Ubuntu Linux Rocks at desktop Level
Ubuntu Linux Rocks at desktop Level
[root@venkat ~]#
Example:6 Display the contents of a file with Line Numbers
[root@venkat ~]# cat -n linux_world
1 Hi this is my first file in linux.
2 Linux always rocks
3 Thanks
[root@venkat ~]#
In case if your file has blank lines , then above command will also display the number of blank lines as well, so to remove the numbering of blank lines , we can use ‘-b‘ option in place of ‘-n’ in the above command.
Example:7 Copy the contents of One file to Another file.
Using greater than ‘>‘ symbol in cat command we can copy the contents of one file to another , example is shown below :
[root@venkat ~]# cat linux_world > linux_text
[root@venkat ~]#
Example:8 Appending the contents of one file to another.
Using double greater than symbol ‘>>‘ in cat command we can append the contents of one file to another. Example is shown below :
[root@venkat ~]# cat /etc/passwd >> linux_text
[root@venkat ~]#
Above Command will append the contents of /etc/passwd file to linux_text file at the end. Now we can verify the contents of linux_text file.
Example:9 Redirecting the output of multiple files into a Single File.
[root@venkat~]# cat linux_world linux_distributions /etc/fstab > linux_merge_text
Above command will merge the output of 3 files into a single file ‘linux_merge_text’.
Example:10 Getting input using standard input operator.
[root@venkat ~]# cat < linux_distributions
RHEL
CentOS
Fedora
Ubuntu
SuSE
Linux Mint
[root@venkat ~]#
Above cat command is getting input from the file using std input operator ‘<‘
Example:11 Sorting the output of multiple files into a single file
[root@venkat ~]# cat linux_text linux_distributions /etc/passwd | sort > linux_sort
By default sorting will done on the alphabetic order, if you want the sorting on basis of number then use ‘-n’ option in the sort command.
Example:12 Insert $ at end of each line using -E option
[root@linuxtechi ~]# cat -E linux_world
Hi this is my first file in linux.$
Linux always rocks$
Thanks$
[root@linuxtechi ~]#
Above command will insert ‘$’ at the end of each line in the output.
Example:13 Show the tab space in the file as ‘^I’ using -T option.
Let’s create a file with some tab spaces.
Now display these tab spaces as ^I
Example:14 Squeeze blank repeated lines using -s option
Let’s take am example of file ‘linux_blank’ , which consists of multiple repeated blank lines.
[root@linuxtechi ~]# cat -s linux_blank
test
test1
test2
test3
test4
[root@linuxtechi ~]#
Example:15 View the Contents in Reverse Order
tac is the reverse of cat command. tac will display the output in reverse order example is shown below
[root@linuxtechi ~]# tac linux_world
Thanks
Linux always rocks
Hi this is my first file in linux.
[root@linuxtechi ~]#
Example:16 Display non-printing characters using -v option.
-v option in the cat command is used to show the non-printing characters in the output. This option become useful when we are suspecting the CRLF ending lines, in that case it will show ^M at the end of each line.
[root@linuxtechi tmp]# cat test_file
hi there
[root@linuxtechi tmp]# cat -v test_file
hi there^M
[root@linuxtechi tmp]#
cat stands for Concatenate. Cat is the basic command when we start learning Linux/Unix, as the name suggest it is used to create new file ,concatenate files and display the output of files on the standard output.
2. Touch Command
To create a zero byte file
# touch <filename>
To create multiple zero byte files
# touch <first file> <second file> <third file>
To change the timestamp of a file or directory
# touch <directory or filename>
3. Vi Editor
To create file
# vi <filename>
Comments
Post a Comment