Lecture # 14 - Text Files (Basic Commands)

Lecture # 14 - Text Files (Basic Commands)

Working with Text Files. Going through some basic commands.

Some basic commands used for text files are:

  • touch: The touch command is used to create empty files or update the timestamp of existing files.

    -> To create a file touch [file1-name] [file2-name] [file3-name] is used.

    -> To change the timestamp of a file touch -t [time-stamp] [file-name] is used

  • cat: The cat command is used to create, concatenate and display the contents of files.

    -> To create a file cat > [file-name] is used. By this command you can also write something into the file. When you're done writing press ctrl + c keys to save.

    -> To display the contents of a file cat [file-name] is used.

    -> To concatenate two files cat [file1-name] [file2-name] is used. This will concatenate the two files and show the combined output on terminal.

    -> To combine two files and save into a new file cat [file1-name] [file2-name] > [combined-file-name] is used.

    -> To display a file with line numbers cat -n [file-name] is used.

  • less: The less command is used to view the contents of a text file or the output of another command in a more user-friendly way. It allows you to view text files one page or one screen at a time, making it easier to navigate through large files. This command is written as less [file-name] . To get the ending view of the file less +G [file-name] is used. You can navigate through the file using the following keys:

    • Arrow keys: Move up and down line by line

    • Page Up and Page Down keys: Move up and down one screen at a time

    • Spacebar: Move forward one screen

    • B: Move backward one screen

    • G: Go to the beginning of the file

    • End key: Go to the end of the file

    • /: Search for a pattern (type the pattern and press Enter)

    • n: Move to the next occurrence of the search pattern

    • N: Move to the previous occurrence of the search pattern

    • q: Quit

  • more: The more command is used to view the contents of a text file one screen at a time. It is similar to less but with fewer features. This command is written as more [file-name] . You can navigate through the file using the following keys:

    • Spacebar: Move forward one page

    • Enter key: Move forward one line

    • Arrow keys: Move up and down one line at a time

    • q: Quit

  • head: The head command is used in Unix and Unix-like operating systems (such as Linux) to display the first few lines of a text file.

    -> To display first few lines of a file head [file-name] is used.

    -> To specify number of lines to be displayed head -n [number-of-lines] [file-name] is used.

    -> To specify number of characters to be displayed head -c [number-of-characters] [file-name] is used.

    -> To display a file name before the first few lines of the file head -v [file-name] is used.

  • tail: The tail command is used to display the last few lines of a text file.

    -> To display last few lines of a file tail [file-name] is used.

    -> To specify number of lines to be displayed tail -n [number-of-lines] [file-name] is used.

    -> To specify number of characters to be displayed tail -c [number-of-characters] [file-name] is used.

    -> To display a file name before the last few lines of the file tail -v [file-name] is used.

  • sort: The sort command is used to sort the lines of text files or the output of other commands alphabetically or numerically. This command is written as sort [file-name] .

    -> To sort in reverse order sort -r [file-name] is used.

    -> To sort the file and save into a file cat -u [file-name] > [new-file-name] .

  • comm: The comm command is used to compare two sorted files line by line and display the lines that are common, unique to the first file, and unique to the second file.

    -> To display the common lines of two sorted files comm [file1-name] [file2-name] is used. This will display three columns. The first column will show Lines unique to the first file. The second column will show lines unique to the second file and the third column will show the common lines of the both files.

    -> We can display the columns of our choice

    • comm -1 [file1-name] [file2-name]: Suppress lines unique to the first file.

    • comm -2 [file1-name] [file2-name]: Suppress lines unique to the second file.

    • comm -3 [file1-name] [file2-name]: Suppress lines common to both files.

    • comm -13 [file1-name] [file2-name]: Display only the lines that are unique to the first file.

    • comm -23 [file1-name] [file2-name]: Display only the lines that are unique to the second file.

  • fold: The fold command is used to wrap lines of text to a specified width. This command is written as fold [file-name] .

    -> We can specify line width by fold -w [number-of-columns] [file-name] .

    -> To break lines at spaces or within words to fit within the specified width,

    fold -s [file-name] is used.

  • wc: The wc command is used to count the number of words, lines, and characters in a file.

    -> To count words use wc -w [file-name] .

    -> To count characters use wc -m [file-name] .

    -> To count lines use wc -l [file-name] .

    -> To display lines, words, and characters together use wc [file-name] . The first one is number of lines, second is number of words and third one is number of characters.