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.
# 1  
Old 02-28-2018
Move file from one directory and update the list file once moved.

Dears,

I have a listfile contains list of files path.
i need to read the line of the listfile
mv the file to other directory
and update the listfile by deleting the lines of the listfile.

Code:
#!/bin/bash
target=/fstest/INVESTIG/Sadiq/TEST_ARCH
while read -r line || [[ -n $line ]];
do
mv $line $target
done < /fstest/INVESTIG/Sadiq/processedfile.txt



Regards,
Sadique
# 2  
Old 02-28-2018
You need to take into account that the mv command can fail. So you need to keep track of failed files.
Code:
#!/bin/bash
target=/fstest/INVESTIG/Sadiq/TEST_ARCH
> /tmp/errfile  # create an empty file
while read -r line || [[ -n $line ]];
do
     mv "$line" $target || echo "$line" >> /tmp/errfile
done < /fstest/INVESTIG/Sadiq/processedfile.txt
mv /tmp/errfile /fstest/INVESTIG/Sadiq/processedfile.txt
# /fstest/INVESTIG/Sadiq/processedfile.txt will be empty if no mv failures occurred
# otherwise the names of files that were not moved still remain in the original input file

filenames with spaces may be a problem, too - why the " quotes surround the $line
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-28-2018
We've already pointed out the pointlessness of the || [[ -n $line ]] construct - you might consider to get rid of it.

Does your mv version provide the -v option? Does your grep offer the -x option? Would, then, this come close to what you need?
Code:
while read line; do mv -v $line $target; done <file 2>/dev/null | sed 's/\o047\| ->.*$//g; s/^/^/'  | grep -vxf- file

This User Gave Thanks to RudiC For This Post:
# 4  
Old 03-01-2018
Hello RudiC,

As per your suggestion, i removed the [[-n $line ]] after the moved from source to target, I still find the those lines inside the filelist.

yes mv version provide the -v option.
so do grep offer the -x.

its Linux Redhat vesrsion 7.2
Kindly suggest.

Regards,
sadique

Last edited by sadique.manzar; 03-01-2018 at 02:25 AM..
# 5  
Old 03-01-2018
Code:
#!/bin/bash
target=/fstest/INVESTIG/Sadiq/TEST_ARCH
while read -r line     # is added && [[ -n $line ]];  better?
do
mv -fb $line $target && echo $line>>/fstest/INVESTIG/Sadiq/processedfile_MOVED.txt || echo $line>>/fstest/INVESTIG/Sadiq/processedfile_NOTMOVED.txt
done < /fstest/INVESTIG/Sadiq/processedfile.txt


Last edited by abdulbadii; 03-01-2018 at 06:33 AM..
This User Gave Thanks to abdulbadii For This Post:
# 6  
Old 03-01-2018
Dear Abdul,

My concern is that i want to update the listfile after each path(line) moved from it can be delete from the processedfile_.txt filelist.

Say:
processedfile_.txt filelist contains:

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

when reading each line it shud move to target directory.
but after moved, script should able to update filelist by deleting the line of the path which has been moved.

Last edited by RudiC; 03-01-2018 at 03:05 AM..
# 7  
Old 03-01-2018
The output of above IS the reduced file list. Capture it and mv to original file list.
This User Gave Thanks to RudiC For This Post:
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