cat
The cat command allows us to create single or multiple files, view contents of file, concatenate files and redirect output in terminal or files.
- Viewing contents of a single file.
# cat file_name
- Viewing contents of multiple files.
# cat file1 file2
- Creating a new file.
# cat > file_name
<content of file>
Ctrl+D - to save & exit.
- Appending data to file.
# cat >> file_name
<new content>
Ctrl+D - to save & exit.
Note: "cat > file_name" will be dangerous if previously the file exits with the same name. So be careful about that. Make sure that the file does not exist before. Otherwise, your previous data will be lost.
- Redirecting contents of multiple files into a single file.
# cat file1 file2 > new_file
- Display line numbers with file content.
# cat -n file_name
- Display the end of line ($).
# cat -e file_name
- Display TAB characters.
# cat -T file_name
- Display file content in Reverse.
# tac file_name
cat command disadvantages:
- can not edit the file later.
- if file content is more than the screen size, can not view all content.
- can not create multiple file.
Note: All these consequences can be kick out by introducing vi-editor.
tee
With the help of tee command we can create multiple files at a time.The tee command reads from standard input, and writes to standard output and to files. It is used to store and view (both at the same time) the output of any other command.The command writes the output both to the screen (STDOUT) and to the file.
- Creating a file.
# tee file_name //same procedure like cat
- Creating multiple file with same content at a time.
# tee file1 file2 file3
- Appending new content to the existing file.
# tee -a file_name
- Appending the same content to multiple existing files.
# tee -a file1 file2 file3
- Write output to STDOUT and also to a file.
# ls -l | tee file_name
- Write output to STDOUT and also to multiple files at same time.
# ls -l | tee file1 file2
-Write output to STDOUT and append to a file.
# who | tee file_name
- Write output to STDOUT and append to multiple files at same time.
# who | tee file_name
touch
The touch command is the easiest way to create new, empty files. It is also used to change the timestamps (i.e., dates and times of the most recent access and modification) on existing files and directories.
- Creating an empty file.
# touch file_name
- Creating multiple empty files.
# touch file1 file2 file3
- Avoid creating new files.
# touch -c file_name
If the file doesn't exist, touch will not create the file.
- Change the file modification time only.
# touch -m file.txt
- Change the file access time only.
# touch -a file.txt
- Explicitly change the Access & Modification time.
# touch -am file.txt
# touch -a -m -t 201406181125.20 file.txt
# touch -d '1 May 2005 10:22' file.txt
# touch -d '14 May' file.txt
# touch -d '14:24' file.txt
# touch -d "2014-04-05 12:12:12.000000000 +0530" file.txt
- Creating a file using a specified time.
# touch -t YYYYMMDDHHMM.SS file_name
# touch -t 201312101420.30 tmp.txt
The time stamp of tmp.txt will change to 14:20:30 PM on December 10th 2013.
- Use the time stamp of another file.
# touch -r file_old file_new
The -r (i.e., reference) option followed directly by a space and then by a file name tells touch to use that file's time stamps instead of current time.
# touch -r file_new -B 30 file_new
# touch -r file_new -F 30 file_new
The -B option modifies the timestamps by going back the specified number of seconds, and the -F option modifies the time by going forward the specified number of seconds. For example, the above two command would make file_new 30 seconds older than & 30 seconds newer than file_old respectively.
- To check the time stamp of file
# ls -l file_name
or # stat file_name
more
The more command is to view (but not modify) the contents of a file one screen at a time (terminal pager). It is a filter for paging through text one screenful at a time.
It can be taken as input for an output exceeding the screen.
- To view content of file.
# more /path/to/file
- To take as input of an output that exceeding the screen.
# ls -l /usr/bin/ | more
Note: Press <Space Bar> or F key to move one page forward and <Enter> key to move one line forward. We can not perform backward movement with the help of more command. In middle of execution press Q key to come back to command line.
less
The less command is used to view (but not change) the contents of a file one screen at a time. It is similar to more, but has the extended capability of allowing both forward and backward navigation through the file. Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi.
- To view content of a file.
# less /path/to/file
- To view from an output that exceeding screen.
# ls -l /usr/bin | less
To traverse the file, less has more options as following,
Down Arrow or <Enter> - scrolls down one line
Up Arrow - scrolls up one line
d - scrolls down half a page
b - scrolls up half a page
Space Bar / F - scrolls one page
G – go to the end of file
g – go to the start of file or ZZ – exit the less pager
10j – 10 lines forward.
10k – 10 lines backward.
CTRL+G – show the current file name along with line, byte and percentage statistics.
head
The head command is used to print the first desired lines from a file to the terminal. head by default, prints the first 10 lines of each file to standard output.
- To display first 10 lines.
# head file_name
- To display first n lines.
# head -n2 file_name
# head -2 file_name
-To skip the last n lines & display rest of all.
# head -n -5 file_name
# head -n-5 file_name
- To display first n lines of multiple files.
# head -10 file1 file2 file3
- Print the first n bytes.
# head -c 5 file_name
# head -c5 file_name
- Skip printing last n bytes.
# head -c -10 file_name
# head -c-10 file_name
- Print the first n kilobytes.
# head -k 5 file_name
# head -k5 file_name
- Skip printing last n kilobytes.
# head -k -10 file_name
# head -k-10 file_name
- Passing output of other command to head input.
# ls -l | head -20
# history | head -100
tail
The tail command is used to print the last N lines from the file on the terminal. The tail by default, prints the last 10 lines of each file to standard output. tail command is especially used with log files to read the last few lines to know about the error messages.
- To display last 10 lines.
# tail file_name
- To display last n lines.
# tail -n2 file_name
# tail -2 file_name
- To print lines starting from nth line to the last.
# tail -n +20 file_name
# tail +20 file_name
- To display first n lines of multiple files.
# tail -10 file1 file2 file3
- To print the last n bytes.
# tail -c 10 file_name
# tail -c10 file_name
- To print characters from the nth byte.
# tail -c+40 file_name
# tail -c+40 file_name
- Print last lines from dynamically changing files. e.g. log files.
# tail -f log_file
- Passing output of other command to head input.
# ls -l | tail -10
Note: Display specific lines (based on line number) of a file using head and tail command
e.g. I have a file having 100 lines. I want to display from 50th line to 70th line. With alone head or tail, I can not do this. So, here is the solution.
# cat file_name | head -70 | tail -21
or # head -70 file_name | tail -21
The cat command allows us to create single or multiple files, view contents of file, concatenate files and redirect output in terminal or files.
- Viewing contents of a single file.
# cat file_name
- Viewing contents of multiple files.
# cat file1 file2
- Creating a new file.
# cat > file_name
<content of file>
Ctrl+D - to save & exit.
- Appending data to file.
# cat >> file_name
<new content>
Ctrl+D - to save & exit.
Note: "cat > file_name" will be dangerous if previously the file exits with the same name. So be careful about that. Make sure that the file does not exist before. Otherwise, your previous data will be lost.
- Redirecting contents of multiple files into a single file.
# cat file1 file2 > new_file
- Display line numbers with file content.
# cat -n file_name
- Display the end of line ($).
# cat -e file_name
- Display TAB characters.
# cat -T file_name
- Display file content in Reverse.
# tac file_name
cat command disadvantages:
- can not edit the file later.
- if file content is more than the screen size, can not view all content.
- can not create multiple file.
Note: All these consequences can be kick out by introducing vi-editor.
tee
With the help of tee command we can create multiple files at a time.The tee command reads from standard input, and writes to standard output and to files. It is used to store and view (both at the same time) the output of any other command.The command writes the output both to the screen (STDOUT) and to the file.
- Creating a file.
# tee file_name //same procedure like cat
- Creating multiple file with same content at a time.
# tee file1 file2 file3
- Appending new content to the existing file.
# tee -a file_name
- Appending the same content to multiple existing files.
# tee -a file1 file2 file3
- Write output to STDOUT and also to a file.
# ls -l | tee file_name
- Write output to STDOUT and also to multiple files at same time.
# ls -l | tee file1 file2
-Write output to STDOUT and append to a file.
# who | tee file_name
- Write output to STDOUT and append to multiple files at same time.
# who | tee file_name
touch
The touch command is the easiest way to create new, empty files. It is also used to change the timestamps (i.e., dates and times of the most recent access and modification) on existing files and directories.
- Creating an empty file.
# touch file_name
- Creating multiple empty files.
# touch file1 file2 file3
- Avoid creating new files.
# touch -c file_name
If the file doesn't exist, touch will not create the file.
- Change the file modification time only.
# touch -m file.txt
- Change the file access time only.
# touch -a file.txt
- Explicitly change the Access & Modification time.
# touch -am file.txt
# touch -a -m -t 201406181125.20 file.txt
# touch -d '1 May 2005 10:22' file.txt
# touch -d '14 May' file.txt
# touch -d '14:24' file.txt
# touch -d "2014-04-05 12:12:12.000000000 +0530" file.txt
- Creating a file using a specified time.
# touch -t YYYYMMDDHHMM.SS file_name
# touch -t 201312101420.30 tmp.txt
The time stamp of tmp.txt will change to 14:20:30 PM on December 10th 2013.
- Use the time stamp of another file.
# touch -r file_old file_new
The -r (i.e., reference) option followed directly by a space and then by a file name tells touch to use that file's time stamps instead of current time.
# touch -r file_new -B 30 file_new
# touch -r file_new -F 30 file_new
The -B option modifies the timestamps by going back the specified number of seconds, and the -F option modifies the time by going forward the specified number of seconds. For example, the above two command would make file_new 30 seconds older than & 30 seconds newer than file_old respectively.
- To check the time stamp of file
# ls -l file_name
or # stat file_name
more
The more command is to view (but not modify) the contents of a file one screen at a time (terminal pager). It is a filter for paging through text one screenful at a time.
It can be taken as input for an output exceeding the screen.
- To view content of file.
# more /path/to/file
- To take as input of an output that exceeding the screen.
# ls -l /usr/bin/ | more
Note: Press <Space Bar> or F key to move one page forward and <Enter> key to move one line forward. We can not perform backward movement with the help of more command. In middle of execution press Q key to come back to command line.
less
The less command is used to view (but not change) the contents of a file one screen at a time. It is similar to more, but has the extended capability of allowing both forward and backward navigation through the file. Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi.
- To view content of a file.
# less /path/to/file
- To view from an output that exceeding screen.
# ls -l /usr/bin | less
To traverse the file, less has more options as following,
Down Arrow or <Enter> - scrolls down one line
Up Arrow - scrolls up one line
d - scrolls down half a page
b - scrolls up half a page
Space Bar / F - scrolls one page
G – go to the end of file
g – go to the start of file or ZZ – exit the less pager
10j – 10 lines forward.
10k – 10 lines backward.
CTRL+G – show the current file name along with line, byte and percentage statistics.
head
The head command is used to print the first desired lines from a file to the terminal. head by default, prints the first 10 lines of each file to standard output.
- To display first 10 lines.
# head file_name
- To display first n lines.
# head -n2 file_name
# head -2 file_name
-To skip the last n lines & display rest of all.
# head -n -5 file_name
# head -n-5 file_name
- To display first n lines of multiple files.
# head -10 file1 file2 file3
- Print the first n bytes.
# head -c 5 file_name
# head -c5 file_name
- Skip printing last n bytes.
# head -c -10 file_name
# head -c-10 file_name
- Print the first n kilobytes.
# head -k 5 file_name
# head -k5 file_name
- Skip printing last n kilobytes.
# head -k -10 file_name
# head -k-10 file_name
- Passing output of other command to head input.
# ls -l | head -20
# history | head -100
tail
The tail command is used to print the last N lines from the file on the terminal. The tail by default, prints the last 10 lines of each file to standard output. tail command is especially used with log files to read the last few lines to know about the error messages.
- To display last 10 lines.
# tail file_name
- To display last n lines.
# tail -n2 file_name
# tail -2 file_name
- To print lines starting from nth line to the last.
# tail -n +20 file_name
# tail +20 file_name
- To display first n lines of multiple files.
# tail -10 file1 file2 file3
- To print the last n bytes.
# tail -c 10 file_name
# tail -c10 file_name
- To print characters from the nth byte.
# tail -c+40 file_name
# tail -c+40 file_name
- Print last lines from dynamically changing files. e.g. log files.
# tail -f log_file
- Passing output of other command to head input.
# ls -l | tail -10
Note: Display specific lines (based on line number) of a file using head and tail command
e.g. I have a file having 100 lines. I want to display from 50th line to 70th line. With alone head or tail, I can not do this. So, here is the solution.
# cat file_name | head -70 | tail -21
or # head -70 file_name | tail -21
No comments:
Post a Comment