In this article, we will see the different ways to add a header record or a trailer record to a file. Let us consider a file, file1. $ cat file1 apple orange grapes banana Adding a line to the start of the file (header) 1. To add a header record using sed: $ sed ‘1i […]
Shell Scripting
Shell Script to print pyramid of Stars
The Basics So here we will print the pyramid of stars in two parts as shown below. We will loop through the number provided by the user and print the first half of the stars using a for loop and other half using another for loop. The spaces and new line characters are added in […]
Shell/Bash Script to Find Prime Numbers in Linux
A prime number is a whole number that has exactly 2 different factors, 1 and itself. A number that is not a prime number will be called composite. Except 1 each natural number that is divisible by only 1 and itself is called a prime number. For example: 2,3,5,7,11,13,17,19,23,29… etc. There are total 25 prime […]
How to convert text files to all upper or lower case
As usual, in Linux, there are more than 1 way to accomplish a task. To convert a file (input.txt) to all lower case (output.txt), choose any ONE of the following: To convert a file (input.txt) to all lower case (output.txt) 1. dd: You may have used dd for many other purposes but it can be […]
How to write Bubble sort in Bash
Bubble sort is a simple algorithm that basically bubbles up the elements of the array. This means that it traverses the array multiple times and swaps the adjacent elements if they are in the wrong order, as in the following diagram: Bubble sort Bash Script Here is a simple bash script for bubble sort algorithm. […]
10 Sed (Stream Editor) Command Examples
Sed is a stream editor in a UNIX-like operating system that is used for filtering and transforming text. Sed is derived originally from the basic line editor ‘ed’, an editor you will find on every Unix system but one that is rarely used because of its difficult user interface. How sed Works? As sed is […]
Backtick (`) symbol in Linux Shell Scripting
One of the most useful features of shell scripts is the lowly back quote character, usually called the backtick (`) in the Linux world. Be careful—this is not the normal single quotation mark character you are used to using for strings. Because it is not used very often outside of shell scripts, you may not […]
How to Find and Delete Empty Directories and Files in Linux
1. Find empty directories in the current directory using find -empty: $ find . -type d -empty 2. Use the following command to remove all empty directories under the current directory: $ find . -type d -empty -exec rmdir {} \; 3. Find empty files in the current directory using find -empty: $ find . […]
How to Check if the script is run by root at the start of the script
Check root at start of script Some scripts need to be run as root and you may want to check at the start of that script that it is running as root. This can be done by checking the environment variable $EUID. This variable will hold the value 0 if it’s being run as root. […]
How to Use user-defined Functions in awk
User-Defined Functions You can create user-defined functions in a awk script file using the func or function keywords. Placement in the file is not important; the function definition can occur anywhere in the awk script. The function can take arguments (local to the function only) or use any existing variable. The syntax of a function […]