Seq Command in Linux

Seq Command in Linux: Linux seq command generates a list of numbers – you can use this in scripts and many other functions. Let’s see the examples.

Basic usage of command
Just typing seq 5 will create 5 numbers

[root@server ~]# seq 10
1
2
3
4
5
[root@server ~]#

If you type two numbers, first number will be start and other one will be end number.

[root@server ~]# seq 21 25
21
22
23
24
25
[root@server ~]#

You can add a third number in between which will work as a step.
for example, if you type seq 5 5 50 it will generate a number from 5 to 50 at the interval of 5 – see the example below

[root@server ~]# seq 5 5 50
5
10
15
20
25
30
35
40
45
50
[root@server ~]#

You can generate numbers in descending order as well by adding the middle number in -ve

[root@server ~]# seq 10 -1 1
10
9
8
7
6
5
4
3
2
1

Practical Usage of seq command
First, let’s see how to create files using the seq command. You will need to use format strings functions i.e. -f

[root@server seq]# touch $(seq -f "file-%g.txt" 1 10)

See the output of the command above.

You can use seq command in your scripting for example While Loops or For Loops.
We will see an example for loop.

[root@server seq]# for i in $(seq 1 10); do echo "Hello $i"; done
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

Here is Man page you can use it to learn something more about it which may be covered in this page.

PS Command Examples

Below are PS Command Examples I will try to add.

Command to the process using highest memory

You can use the below PS command with the sort option which shows the process using highest %MEM usage at the bottom.

 [root@server ~]# ps aux --sort pmem
apache   29798  1.5  6.2 570424 31116 ?        S    05:11   3:32 /usr/sbin/httpd -DFOREGROUND
apache   29831  1.5  6.4 567676 32136 ?        S    05:12   3:29 /usr/sbin/httpd -DFOREGROUND
apache   18426  0.3  6.4 562656 32404 ?        S    08:57   0:01 /usr/sbin/httpd -DFOREGROUND
apache   18364  0.5  6.5 565692 32860 ?        S    08:56   0:01 /usr/sbin/httpd -DFOREGROUND
apache   29809  1.5  6.8 567452 34336 ?        S    05:11   3:31 /usr/sbin/httpd -DFOREGROUND
apache   18356  0.4  6.9 567512 34572 ?        S    08:56   0:01 /usr/sbin/httpd -DFOREGROUND
apache   29792  1.5  7.2 569516 36144 ?        S    05:11   3:33 /usr/sbin/httpd -DFOREGROUND
apache   29825  1.4  7.4 569532 37372 ?        S    05:11   3:27 /usr/sbin/httpd -DFOREGROUND
apache   18014  0.3  8.0 570868 40232 ?        S    08:51   0:02 /usr/sbin/httpd -DFOREGROUND
apache   29791  1.5  8.2 572836 41040 ?        S    05:11   3:36 /usr/sbin/httpd -DFOREGROUND

From the example above we can see that the PID 29791 is using the highest memory. You can note the PID of the process which is using the high memory and then you can see the details using ps.

[root@server ~]# ps ev --pid=18426
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
18426 ?        S      0:03   3847   483 570652 41760  8.3 /usr/sbin/httpd -DFOREGROUND LANG=C PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin NOTIFY_SOCKET=/run/systemd/notify
[root@server ~]#

Note: In the above output, if RSS (resident set size, in KB) increases over time (so would %MEM), it may indicate a memory leak in the application.

Sed Command Examples

Insert a newline in front of pattern

Example your text in the file is as below

Updated number of BobUpdated number of ChrisUpdated number of Peter

If you want format the text as below
Updated number of Bob
Updated number of Chris
Updated number of Peter


Need to use the command as below.

 [root@server ~]# sed 's/Update/\n&/g'  test.txt

Sum of the Values of Column Using AWK.

For example, I have a file called apples.txt with the following content and I want to total them up.

Apples in Box 1 = 100
Apples in Box 2 = 564
Apples in Box 3 = 214
Apples in Box 4 = 124
Apples in Box 5 = 785
Apples in Box 6 = 398
Apples in Box 7 = 125
Apples in Box 8 = 753
Apples in Box 9 = 357
Apples in Box 10 = 951

So just cat the file and use AWK command awk ‘{SUM+=$6}END{print SUM}’ to total them up

make sure to use the right column in awk command, like in our example I want to SUM the column 6 so I used ‘{SUM+=$6}

[root@server ~]# cat  example.txt | awk '{SUM+=$6}END{print SUM}'
4371

Eval Command in Linux

Eval is built in unix command – it’s used to execute arguments as shell commands. It’s useful when you have command stored in variable and you want to execute it.

[root@server Files]# mycommand="ls -ltr"

[root@server Files]# echo $mycommand
ls -ltr

[root@server Files]# eval $mycommand
total 0
-rw-r--r-- 1 root root 0 Nov  5 07:40 file1.txt
-rw-r--r-- 1 root root 0 Nov  5 07:40 file3.txt
-rw-r--r-- 1 root root 0 Nov  5 07:40 file2.txt
[root@server Files]#

Look at the example above, I have stored the command ls – ltr in the variable mycommand so when I do echo it’s just prints what in the variable. But when I do eval followed with the variable – it will actually execute the command.

Eval is powerful command but it’s Evil – it has security issues as well.
You can find the details here