Move file from one directory and update the list file once moved.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move file from one directory and update the list file once moved.
# 15  
Old 03-01-2018
Regarding the [[-n $line ]] you seem keep to have, what are you trying to achieve?

Are you worried about blank input lines perhaps? If so, maybe this would be better:-
Code:
while read line
do
   [[ -z "$line" ]] && continue                       # Jump round the loop for next item if we have nothing
   mv $line $targetdir  ||  echo "$line >&3"          # rest of processing here
done < input-file 3> failure-list                     # Read input. Collect failure messages to a file

An alternate might be to strip them from your input file as you read it in something like:-
Code:
while read line
do
   mv $line $targetdir  ||  echo "$line" >&3
done < <(grep -Ev "^$" input-file) 3> failure-list    # Feed in every non-blank line. Pattern excluded is <start-of-line><end-of-line>

This might depend on your OS and shell versions.


From RudiC's code (and above) you have a new file listing those that failed to move so that is what you would want. Hopefully it will be empty, unless you have duplicate files listed. If this is the case, you should sort the input file with the -u flag.

Editing the input file whilst still being read it is not only IO intensive for a large input file, but could have unpredictable results.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 16  
Old 03-01-2018
@rbatte1 #post15 for my knowledge could you please expalain (grep -Ev "^$" input-file part of the code.

---------- Post updated at 05:58 AM ---------- Previous update was at 05:56 AM ----------

RudiC, please help me to understand sed 's/\o047\| ->.*$//g; s/^/^/' | grep -vxf- file part of the code.

How do i update my processedfile.list file after successful move command.

So if my list file contains 1000 lines, and if all the lines read and successfully moved. then my list file should be empty. else have only the for the failed lines.
# 17  
Old 03-01-2018
As I tried to point out, and I don't know how to phrase it more explicitly, the output of the script IS THE NEW LISTFILE, containing ALL THE FAILURES (i.e. having deleted the successful moves) and so REPLACING the OLD listfile, GIVEN your other process has not modified the old listfile in the meantime.
See the difference between posts #6 and#11 - from 10 entries in your listfile, 9 worked and have disappeared, one (...003...) failed and its entry is printed. You have to remove the OLD listfile - hopefully unmodified by the other process in the meantime - and put the printout in its place.

The sed mangles the mv -v output for the grep -v to follow which then removes the successful entries. cut does the same, btw.
This User Gave Thanks to RudiC For This Post:
# 18  
Old 03-26-2018
Have 2 files contain:

File1 have lines:

Code:
/fstest/INVESTIG/Sadiq/TEST/file001.bin
/fstest/INVESTIG/Sadiq/TEST/file002.bin
/fstest/INVESTIG/Sadiq/TEST/file003.bin
/fstest/INVESTIG/Sadiq/TEST/file004.bin

File2 have lines:

Code:
/fstest/INVESTIG/Sadiq/TEST/file001.bin
/fstest/INVESTIG/Sadiq/TEST/file002.bin
/fstest/INVESTIG/Sadiq/TEST/file003.bin
/fstest/INVESTIG/Sadiq/TEST/file004.bin
/fstest/INVESTIG/Sadiq/TEST/file005.bin
/fstest/INVESTIG/Sadiq/TEST/file006.bin
/fstest/INVESTIG/Sadiq/TEST/file007.bin
/fstest/INVESTIG/Sadiq/TEST/file008.bin
/fstest/INVESTIG/Sadiq/TEST/file009.bin
/fstest/INVESTIG/Sadiq/TEST/file010.bin

I wan to validate the file and delete the common line from file2, so the file2 should contain only non matching lines.

How can i validate and achieve it ?


Code:
while read -r line;
do
if ! grep -Fxq File1 File2
then
"delete comman for the line in File2"
fi 
done < /File1


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-26-2018 at 05:57 AM.. Reason: Added CODE tags.
# 19  
Old 03-26-2018
man comm
This User Gave Thanks to RudiC For This Post:
# 20  
Old 03-26-2018
how do i delete matching lines of one file from other in loop ?

---------- Post updated at 04:03 AM ---------- Previous update was at 03:58 AM ----------

Code:
comm -12  <(sort File1) <(sort File2)

Output:
/fstest/INVESTIG/Sadiq/TEST/file001.bin
/fstest/INVESTIG/Sadiq/TEST/file002.bin
/fstest/INVESTIG/Sadiq/TEST/file003.bin
/fstest/INVESTIG/Sadiq/TEST/file004.bin


But how do i delete it from File2 one by one ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

3. Shell Programming and Scripting

Move file in to directory- script

Hi In directory /mnt/upload I have about 100 000 files (*.png) that have been created during the last six months. Now I need to move them to right folders. eg: file created on 2014-10-10 move to directory /mnt/upload/20141010 file created on 2014-11-11 move to directory /mnt/upload/20141111... (6 Replies)
Discussion started by: primo102
6 Replies

4. Shell Programming and Scripting

Shell script to get the latest file from the file list and move

Hi, Anybody help me to write a Shell Script Get the latest file from the file list based on created and then move to the target directory. Tried with the following script: got error. A=$(ls -1dt $(find "cveit/local_ftp/reflash-parts" -type f -daystart -mtime -$dateoffset) | head... (2 Replies)
Discussion started by: saravan_an
2 Replies

5. Shell Programming and Scripting

How to create a log file that simply shows the name of a file and what directory it was moved to.

Newbie...Thank you for your help. I am creating my first script that simply generates subdirectories to organize video, music, and text files within those subdirectories from my current working directory. PROBLEM: I am trying to write a log file that contains, for each file, the original file... (0 Replies)
Discussion started by: BartleDoo
0 Replies

6. Shell Programming and Scripting

temporary file to file then move to directory

Ok in my bash script i have 5 options to create a simple html script. I need to create a temporary file and whatever the user types will be stored in that file using html codes. And then I have another option in which that temporary file will be moved to the public_html directory in which the user... (19 Replies)
Discussion started by: gangsta
19 Replies

7. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

8. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

Hello all. I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b: I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused: I have all of my music stored in... (7 Replies)
Discussion started by: marcozd
7 Replies

9. Shell Programming and Scripting

List moved files in text file

Hi. I am actually doing all of this on OSX, but using unix apps and script. I have built my own transparent rsync/open directory/mobility/etc set of scripts for the firm I work at, and it is all almost complete except for ONE THING. I have the classic problem with rsync where if a user... (0 Replies)
Discussion started by: Ashtefere
0 Replies

10. Shell Programming and Scripting

Move a file from windows directory to unix directory

Move a file from windows directory to unix directory, is this possible? if it is, can someone help me on this? Thanks! God bless! (1 Reply)
Discussion started by: kingpeejay
1 Replies
Login or Register to Ask a Question