Append a string to the last created file using shell scipts

Let’s suppose we would like to append ‘,yes’ to the last csv file that has been created. The content of the folder is given by

userk@dopamine ~$ ls -t
b3.txt  b2.txt  b1.txt  a3.txt  a2.txt  a1.txt
userk@dopamine ~$  ls -lt
total 24
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 b3.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 b2.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 b1.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 a3.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 a2.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 a1.txt

userk@dopamine ~$  ls -ltr
total 24
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 a1.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 a2.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 a3.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 b1.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 b2.txt
-rw-rw-r-- 1 userk userk 2 mar  1 13:01 b3.txt

Please note that we have used in the first command the -t option to sort the output by time modified. So the most recently modified file will show up first. In the second one, we have just used a more exhaustive output. In the third one we used the -r option the reverse the order.

Since we want only the last modified file let’s use the output of ls as the input of another command called head -N where N is the number of rows you want to display.

userk@dopamine ~$ ls -t | head -1
b3.txt
userk@dopamine ~$ ls -t | grep a | head -1
a3.txt

If you want to select the last modified file starting with ‘a’ then just use grep to filter the result of ls.

Append String to File using sed

Suppose we have selected the right file using the commands above. We now want to append a string to each line of the following file.

userk@dopamine ~$ cat a2.csv
when,there,is,a,shell,there,is,a,way
www.whazzo.com,www.primamossa.org,www.userk.co.uk

Sed provides lot of commands to perform operations with the lines in a file. To append a string to each line of a file:

userk@dopamine:~$ sed 's/$/,yes/' -i a2.csv
userk@dopamine:~$ cat a2.csv
when,there,is,a,shell,there,is,a,way,yes
www.whazzo.com,www.primamossa.org,www.userk.co.uk,yes

The above command uses the syntax s/pattern1/pattern2/ which Substitutes the first occurrence of pattern1 with pattern2. In our case pattern 1 is the special character $ which matches the end of lines, and pattern 2 is the string we want to append.

Creating a basic shell script

First of all we need to pass the output of the ls command to sed. We can do this in several ways, here there 2 of them.

userk@dopamine:~$ cat $(ls -t | head -1) 
when,there,is,a,shell,there,is,a,way
www.whazzo.com,www.primamossa.org,www.userk.co.uk
userk@dopamine:~$ sed 's/$/,yes/' -i $(ls -t | head -1) 
userk@dopamine:~$ cat $(ls -t | head -1) 
when,there,is,a,shell,there,is,a,way,yes
www.whazzo.com,www.primamossa.org,www.userk.co.uk,yes

In order to test the second method based on Xargs, we need to remove the ‘,yes’ string from each line.

userk@dopamine:~$ sed "s/,yes//" -i $(ls -t | head -1)
userk@dopamine:~$ ls -t | head -1 | xargs cat 
when,there,is,a,shell,there,is,a,way
www.whazzo.com,www.primamossa.org,www.userk.co.uk
userk@dopamine:~$ ls -t | head -1 | xargs sed 's/$/,yes/' -i 
userk@dopamine:~$ ls -t | grep b | head -1 | xargs cat
when,there,is,a,shell,there,is,a,way,yes
www.whazzo.com,www.primamossa.org,www.userk.co.uk,yes

Ok, now we can create a script with only two lines. The first one indicates the system which program to use to run the file. #!/bin/bash
The second line is the only action performed by this script, which appends the string to each line of the file provided by the ls command.

Create a file the above content. And grant the appropriate permissions. We have used chmod 770, but the permissions depend on the content of your script and the location of your file.

Shell script

If we want to provide the string to append and the name of the file we want to modify, we can use the following approach:

If you want to exclude a pattern in the filename using grep just use the -v option as follows

userk@dopamine:~$ ls -t | grep -v patternToIGNORE | grep $filename | head -1


Reference