Move a TXT file greater or equal 355 MB with its corresponding .LST file

Good morning, i need your help please
I need to move a .TXT file greater or igual 355 MB and its correspondent .LST file in a non recursive way

The operating system is:

uname -a
SunOS server01c 5.10 Generic_144488-01 sun4u sparc SUNW,SPARC-Enterprise

For example:

rw-r--r--   1 xptol    explotacion    355M Jan 25 03:54 NS6142SCA20200125033624.TXT
-rw-r--r--   1 xptol    explotacion       7 Jan 25 03:54 NS6142SCA20200125033624.LST

I could identify the file with the find command

find . ! -name . -prune -name 'NS6142*' -size +355000000c -print

Output;

NS6142SCA20200125033624.TXT

because every file is transmitted ever 20 minutes i need to loop but i dont know how to get around to move the 355 MB .TXT file which its correspondebt .LST file

while true
do
 sleep 2400
 find . ! -name . -prune -name 'NS6142*' -size +355000000c -exec mv {} target_dir \; 
 --identify .lST file and move to target_dir
done
 

or is there a better way to get around this, i wiil appreciate your help

Thanks for your help in advanced

You can store the filename in a variable and construct the other filename from it.
In case it finds more than one filename use a for loop or while loop:

#!/bin/bash
while true
do
 sleep 2400
 # find big .TXT files
 find . ! -name . -prune -name 'NS6142*.TXT' -size +355000000c -print |
 # raw-read the filenames from the pipe
 while IFS= read -r fn
 do
  # strip .TXT and add .LST
  fn2=${fn%.TXT}.LST
  # move both files to target_dir
  mv "$fn" "$fn2" target_dir
 done
done
1 Like

Hi

And it will not be the same thing without a leading current directory?

find * -prune -name 'NS6142*'

Thank you very much for your help, I tested the script and its working properly, could u mind explain me briefly these lines:

 while IFS= read -r fn

loop ?

 fn2=${fn%.TXT}.LST

var ?

Thanks for your help in advenced

I gave brief explanations as comments in the code.
The following are comprehensive ones:

while condition - do - done
is a loop. The condition is the read command that reads a line from the standard input (here: the pipe i.e. the output from the previous command); the exit status is 0 (true) if the read could read something, then the loop continues, trying to read the next line. Finally, if nothing can be read, the exit code is not 0 (false) and the loop ends.
The read fn command reads the line into the fn variable. By default it strips leading spaces from the input; this is prevented by temporary setting the environment variable IFS to nothing. Further, read by default does special treatment with \ characters in the input; this behavior is turned off with the -r option.
(Compare this loop with the other loop where condition is always true so it never ends.)

The fn2 variable is assigned a string that is a concatenation of ${fn%.TXT} and .LST . The first is the value from variable fn with a modifier. The % modifier strips the following pattern from the end. (Compare with an unmodified value ${fn} (that is in short $fn ).)

3 Likes

Thanks you very much again for your help and explanation, it was really useful

One final question please:
Script did what is supposed to do but I came up a question related what you mentioned previusly:
Compare this loop with the other loop where condition is always true so it never ends

So this script is supposed to end when the condition is true?
I run the scrpt but never ended

#!/bin/bash
while true
do
 # find big .TXT files
 find . ! -name . -prune -name 'NS6142*.TXT' -size +355000000c -print |
 # raw-read the filenames from the pipe
 while IFS= read -r fn
 do
  # strip .TXT and add .LST
  fn2=${fn%.TXT}.LST
  # move both files to target_dir
  mv "$fn" "$fn2" ./pruebas/
 done
 sleep 120 
done

Output:

/produccion/explotacion/xptol/tel/tasacion/RECEP/CENTRAL_142_24012020/pruebas # ls -lrth
total 2909256
-rw-r--r--   1 xptol    explotacion    355M Jan 24 15:40 NS6142SCA20200124152445.TXT
-rw-r--r--   1 xptol    explotacion       7 Jan 24 15:40 NS6142SCA20200124152445.LST
-rw-r--r--   1 xptol    explotacion    355M Jan 24 16:48 NS6142SCA20200124163227.TXT
-rw-r--r--   1 xptol    explotacion       7 Jan 24 16:48 NS6142SCA20200124163227.LST
-rw-r--r--   1 xptol    explotacion    355M Jan 25 01:27 NS6142SCA20200125011130.TXT
-rw-r--r--   1 xptol    explotacion       7 Jan 25 01:27 NS6142SCA20200125011130.LST
-rw-r--r--   1 xptol    explotacion    355M Jan 25 10:39 NS6142SCA20200125102126.TXT
-rw-r--r--   1 xptol    explotacion       7 Jan 25 10:39 NS6142SCA20200125102126.LST

source directory:

/produccion/explotacion/xptol/tel/tasacion/RECEP/CENTRAL_142_24012020 # ls -lrt|head
total 4249458
-rw-r--r--   1 xptol    explotacion 41016367 Jan 24 14:44 NS6142SCA20200124143011.TXT.gz
-rw-r--r--   1 xptol    explotacion      55 Jan 24 14:44 NS6142SCA20200124143011.LST.gz
-rw-r--r--   1 xptol    explotacion 41016367 Jan 24 15:02 NS6142SCA20200124144828.TXT.gz
-rw-r--r--   1 xptol    explotacion      55 Jan 24 15:02 NS6142SCA20200124144828.LST.gz
-rw-r--r--   1 xptol    explotacion 41016367 Jan 24 15:20 NS6142SCA20200124150634.TXT.gz
-rw-r--r--   1 xptol    explotacion      55 Jan 24 15:20 NS6142SCA20200124150634.LST.gz
-rw-r--r--   1 xptol    explotacion 41016367 Jan 24 16:01 NS6142SCA20200124154443.TXT.gz
-rw-r--r--   1 xptol    explotacion      55 Jan 24 16:01 NS6142SCA20200124154443.LST.gz
-rw-r--r--   1 xptol    explotacion 41016367 Jan 24 17:08 NS6142SCA20200124165229.TXT.gz
/produccion/explotacion/xptol/tel/tasacion/RECEP/CENTRAL_142_24012020 # 
/produccion/explotacion/xptol/tel/tasacion/RECEP/CENTRAL_142_24012020 # ls -lrt|tail
-rw-r--r--   1 xptol    explotacion 41016367 Jan 25 09:56 NS6142SCA20200125093943.TXT.gz
-rw-r--r--   1 xptol    explotacion      55 Jan 25 09:56 NS6142SCA20200125093943.LST.gz
-rw-r--r--   1 xptol    explotacion 41016367 Jan 25 10:16 NS6142SCA20200125100137.TXT.gz
-rw-r--r--   1 xptol    explotacion      55 Jan 25 10:16 NS6142SCA20200125100137.LST.gz
-rw-r--r--   1 xptol    explotacion 41016367 Jan 25 10:59 NS6142SCA20200125104305.TXT.gz
-rw-r--r--   1 xptol    explotacion      55 Jan 25 10:59 NS6142SCA20200125104305.LST.gz
-rw-r--r--   1 xptol    explotacion   10213 Jan 26 23:49 ejemplo.txt
-rwxr-xr-x   1 xptol    explotacion     457 Jan 27 01:39 script1.sh
-rwxr-xr-x   1 xptol    explotacion     488 Jan 27 02:00 script.sh
drwxr-sr-x   2 xptol    explotacion     512 Jan 27 02:01 pruebas/

Thans for your help in advanced

The while read loop ends when there is nothing left to read. When it ended, the code after the done is run, i.e. it continues with the sleep command.
The outer while true loop never ends, because the condition is always true. If there were code after the outer done it would never run.

You have two while loops : one - the inner - that ends when all files printed by find are processed, and the other - outer - that never ends as its condition never will be false.