In last tutorial, we learn how to login/navigating in linux server. If you have not read my article yet I would suggest you do that:
How to SSH or Navigate in Linux?
In this tutorial we are learning some of the following features in Linux:
- create/delete or update a file
- viewing a file
- searching for strings in a file
How to create/update or delete a file in Linux or Ubuntu?
In order to create a new file in linux you can use touch command. Check out following examples:
# syntax: touch <file_name> # create a new file called demo.txt # caution: this override the file if exists touch demo.txt # create a file with "hello world" content # > symbol is used to redirect/overite output to a given file # caution: this override the file if exists echo "hello world" > demo.txt # create a new file demo.txt if it does not exist touch -c demo.txt # create a file with specified time # syntax: touch -t <YYMMDDHHMM> <fileName> # create multiple files touch file1.txt file2.txt file3.txt
In order to update file content you can do following:
- you can open file in choice of your editor and update i.e. nano, vim editor
- you can use >> symbol to append content
# update existing file using nano editor nano demo.txt # append new contents to existing file # >> symbol is used to append some content to given file echo "new content" >> demo.txt
If you want to delete an existing file in linux use following command:
# delete a demo.txt file rm demo.txt # remove all files and folders (recursive) in current directory rm -rf .
How to view file contents in Linux/Ubuntu?
To view file contents of existing file in linux you can use following commands:
- cat: lists all contents of the file
- less: view file contents in scrolling fashion
- tail: view file contents from bottom of the file
- head: view file contents from beginning of the file
In order to understand our example better let's create a new demo.txt file with following contents:
# Types of fruit Apples and pears Citrus – oranges, grapefruits, mandarins and limes Stone fruit – nectarines, apricots, peaches and plums Tropical and exotic – bananas and mangoes Berries – strawberries, raspberries, blueberries, kiwifruit and passionfruit Melons – watermelons, rockmelons and honeydew melons Tomatoes and avocados. # Types of vegetables Leafy green – lettuce, spinach and silverbeet Cruciferous – cabbage, cauliflower, Brussels sprouts and broccoli Marrow – pumpkin, cucumber and zucchini Root – potato, sweet potato and yam Edible plant stem – celery and asparagus Allium – onion, garlic and shallot.
View all file contents using cat command
To view all file contents demo.txt file run following command on your terminal window:
# show all file contents cat demo.txt
Output:
# Types of fruit Apples and pears Citrus – oranges, grapefruits, mandarins and limes Stone fruit – nectarines, apricots, peaches and plums Tropical and exotic – bananas and mangoes Berries – strawberries, raspberries, blueberries, kiwifruit and passionfruit Melons – watermelons, rockmelons and honeydew melons Tomatoes and avocados. # Types of vegetables Leafy green – lettuce, spinach and silverbeet Cruciferous – cabbage, cauliflower, Brussels sprouts and broccoli Marrow – pumpkin, cucumber and zucchini Root – potato, sweet potato and yam Edible plant stem – celery and asparagus Allium – onion, garlic and shallot.
View last n lines of the file using tail command
To view last n numbers of lines from a given file use tail command. Default is 10 lines.
# view last 10 lines from demo.txt file tail -f demo.txt
Output:
Melons – watermelons, rockmelons and honeydew melons Tomatoes and avocados. # Types of vegetables Leafy green – lettuce, spinach and silverbeet Cruciferous – cabbage, cauliflower, Brussels sprouts and broccoli Marrow – pumpkin, cucumber and zucchini Root – potato, sweet potato and yam Edible plant stem – celery and asparagus Allium – onion, garlic and shallot.
View last 2 lines of the file:
# view last 2 lines only tail -2 demo.txt
Output:
Edible plant stem – celery and asparagus Allium – onion, garlic and shallot.
View first n number of lines of the file using head command
To view n number of line from the begining of a file in linux you can use head command.
# view first 10 lines of demo.txt file head demo.txt
Output:
# Types of fruit Apples and pears Citrus – oranges, grapefruits, mandarins and limes Stone fruit – nectarines, apricots, peaches and plums Tropical and exotic – bananas and mangoes Berries – strawberries, raspberries, blueberries, kiwifruit and passionfruit Melons – watermelons, rockmelons and honeydew melons Tomatoes and avocados. # Types of vegetables
View 5 lines of the file from begining
To view first 5 lines of the file you can use following command syntax:
# view first 5 lines from demo.txt file head -5 demo.txt
Output:
# Types of fruit Apples and pears Citrus – oranges, grapefruits, mandarins and limes Stone fruit – nectarines, apricots, peaches and plums Tropical and exotic – bananas and mangoes
Search for specific string from the file in Linux/Ubuntu
If you are a developer often time you have to deal with different types of files. Sometime you want to find specific strings from the file. In order to find some specific strings from the file in linux you can use some of the examples shown below:
# find any lines that contents word: onion grep "onion" demo.txt # or you can use cat demo.txt | grep "onion"
Output:
Allium – onion, garlic and shallot.
Search for specific word and highlight it
Sometime you may want to find some word from given file and also want to highlight them for better output:
# hightlight word on from demo file # filter only those lines that contains word: on grep --color on demo.txt
Output:
Search for specific word in a file and add show line number
Sometime you want to search for specific word in a file but you also want to see line number where this match was found. Use following command to add line number in the begining of the line:
# find word on from the file # show line number where match is found # colorize the found word grep --color -n on demo.txt
Output:
Search for specific word and count matched lines
If you want to see total number of matched lines found when searched for specific keyword you can do that using following command:
# show total line count for word on grep -c "on" demo.txt
Searching specific word in a file
To search for specific word in a file ignoring case you can use following command:
# ignore case -i # show word only -w grep -iw "on" demo.txt
Displaying only the matched pattern
To show only matched word and ignore other words you can use following command:
# ignore case -i # ignore other words -o grep -io "on" demo.txt
Search specific string that starts/ends in begining of the line
Sometimes, you only want to find lines that begins with specific string in a file. You can do that using following command:
# ignore case -i # ^ - begining of the file using regex grep -i "^root" demo.txt
Similarly, to search for specific word that ends the line you can use:
# ignore case -i # $ - end of the line using regex grep -i "yam$" demo.txt
Search multiple words in a file
To perform multi keyword search using grep command you can use following example:
grep -e pumpkin -e garlic demo.txt
View specific lines from the file
Sometimes, when file is too large and you want to view specific range of lines you can use sed command:
# show lines 2,3,4 only sed '2,4!d' demo.txt # or you can use sed -n '2,4p' demo.txt
Hope this tutorial is helpful for those seeking to gain some advanced linux skills. Please share and like this article and stay tune for more.