This week, we will review the commands which handle content.
They are useful when you need to display the content of files or variables easily in a Terminal.
Command | Description |
echo | Displays a simple text string on the screen |
printf | Displays text with precise formatting |
cat | Displays the entire content of a file |
head | Displays the first lines of a file |
tail | Displays the last lines of a file |
less | Allows interactive viewing of a file’s content |
more | Displays the content of a file page by page |
zcat | Displays the content of a gzip-compressed file |
echo is a basic command to display a text or a variable in the Terminal.
For instance, if you would like to display the current user type:
echo $USER
You can also display any other message to the Terminal and in addition redirect the result to a file like below:
echo "Welcome to Haiku!" >welcome.txt
This command is not a well known command - compared to echo - but when you need to display a message in a specific format, this is the one to use:)
In case you want to display a welcome message to the current user, in a Terminal type:
printf "Hello %s! \n" $USER
The "%s" will indicate to display a string variable which is contained in the "$USER" variable.
You can also decide to display information like Name and Age with a specific width and align to the left:
This commands stands for "concatenate".
It's needed when you want to display the content of a file like below:
cat /boot/home/config/settings/boot/UserSetupEnvironment.sample
In case the content of the file is higher than the height of the Terminal, you can use the "more" command at the end:
cat /boot/home/config/settings/boot/UserSetupEnvironment.sample | more
In that case, the file will be displayed on a page by page basis.
You can also decide to use the cat command to concatenate 2 files into one:
cat welcome.txt welcome.txt >new_welcome.txt
In the above example, the content of the file "welcome.txt" is displayed twice and the result is put inside the new file "new_welcome.txt".
You can also use the "cat" command with the "-n" option to display the line numbers:
cat -n new_welcome.txt
This is a nice and simple command to display the first lines of a file.
If you need to display the first 5 lines of the system log file, type in a Terminal:
head -n 5 /var/log/syslog
The tail command is the opposite of the head one: it will display the last 10 lines of a file.
In case you need to display the last lines of the system log file, type:
tail -f /var/log/syslog
The "-f" option - which means follow - will allow to continue the display when new lines are created like below:
A really useful command:)
The less command allows interactive viewing of a file’s content.
You can move forward or backward in the file and search for content with the "/" character.
The percentage of the content of the file is displayed at the bottom line:
And when you reach the end of the file, the below status is displayed:
In case you search for the word "file", type "/file", and the results will be highlighted as below:
You can quit the display with the "q" character.
more is supposed to be a page by page display like less, but the file will be loaded completely (where less will load the file on the fly).
The display of a page will be like below:
Not sure if both less and more are different, because when you check for the help on more:
more --help
It indicates "less" commands:
In case you would like to read the content of a gzip file, the zcat command is quite useful.
Let's archive the previous "welcome.txt" file into gzip format:
gzip welcome.txt
Now let's verify if the zcat can read the content of this archived message:
zcat welcome.txt.gz
Yes!
It's working as expected.
Any other content command you would like to share?
If so, you can put a comment below.