Move file using a file list


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Move file using a file list
# 1  
Old 06-26-2013
Tools Move file using a file list

Hello, I have a text file name "filelist" which stores the file names of the files I want to move (the file names does not include the directory).

Each of the name is in 1 line, and all the file are in directory: /cluster/home/input (along with other unrelated files). I want to move all the files that are listed in the "filelist" to another direcoty: /cluster/home/bad.

I am a newbie so practically I dont know much about this, please help me out Smilie
# 2  
Old 06-26-2013
Code:
cd /cluster/home/input 
mv $(cat /full_path/filelist) /cluster/home/bad

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 06-26-2013
Quote:
Originally Posted by warmboy610
Hello, I have a text file name "filelist" which stores the file names of the files I want to move (the file names does not include the directory).

Each of the name is in 1 line, and all the file are in directory: /cluster/home/input (along with other unrelated files). I want to move all the files that are listed in the "filelist" to another direcoty: /cluster/home/bad.
The cleanest way is to create a small script. Let us first define some variables and set up a basic loop. You should always write scripts in a way that they work regardless from where they are called. On the commandline you can use relative paths like cd .., but in scripts you should always use absolute paths:

Code:
#! /bin/sh

pFileList="/path/to/file/filelist"
pSrcDir="/cluster/home/input"
pTgtDir="/cluster/home/bad"
pFile=""

while read pFile ; do
     echo mv "${pSrcDir}/${pFile}" "${pTgtDir}"
done < "${pFileList}"

exit 0

Now look at the output: are there error messages? (If yes: which ones?) You should see a list similar to the following, with "first_file", etc. replaced by the files in your file "filelist":

Code:
mv /cluster/home/input/first_file /cluster/home/bad
mv /cluster/home/input/second_file /cluster/home/bad
mv /cluster/home/input/third_file /cluster/home/bad
...

Carefully check the list of these commands if they indeed would do what you want to do. If sure, remove the "echo" from the beginning of the line and let it run again.

If you have to do that regularly and not only once you might want to refine the script a bit (replace "<spc>" and "<tab>" with literal tab/space characters when you use this):

Code:
#! /bin/sh

pFileList="/path/to/file/filelist"
pSrcDir="/cluster/home/input"
pTgtDir="/cluster/home/bad"
pFile=""

sed 's/#.*//;s/^[<spc><tab>]*//;s/[<spc><tab>]*$//;/^$/d' "${pFileList}" |\
while read pFile ; do
     if [ ! -f "${pSrcDir}/${pFile}" ] ; then
          echo "ERROR: File ${pFile} not found."
     else
          mv "${pSrcDir}/${pFile}" "${pTgtDir}"
     fi
done

exit 0

Now the script reports possible errors if you mention a file in "filelist" but it isn't there. Further, in your filelist you can use "#" and write commentaries after that. Leading/trailing whitespace and empty lines are ignored. A "filelist" file could look like:

Code:
# this is the file list for june

first_file     # per request of Alan
second_file  # per request of Bill

# these files get also moved:
       third_file
       fourth_file

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 06-27-2013
Hi, thanks for all of your helps. I am writing a script. I think I will try both way to see the result.
Again many thanks to you guys Smilie
# 5  
Old 06-27-2013
RedHat

Code:
#!/bin/sh

source_path=`cat /cluster/home/input/filelist`
dest_path=/cluster/home/bad
for i in $source_path
do
  mv $i  $dest_path
done

SmilieSmilieSmilieSmilieSmilie

Last edited by Scott; 06-27-2013 at 12:25 AM.. Reason: Please use code tags
# 6  
Old 06-27-2013
this you can do on the command line ...
Code:
for file in $(< /cluster/home/input/filelist)
do
    mv $file /cluster/home/bad
done

# 7  
Old 07-01-2013
Thanks guys, it works out fine. But now there is a greater problem since I have to move files with specical characters. So the listfile I made using the grep command:
Code:
 
fgrep -f ${illegalCharFile} $allfile | sed -e 's/^//' >> $listfile

Then I move the file using the command
Code:
mv $(cat $listfile) $bad_name

The illegalCharFile file is the file contains all special characters ( like * , ' , ; ), it does not work and show variable syntax.
I did figure out if I use mv 'filename' new_directory command it would work and I try to add ' at the beginning and the end of each line in the listfile but I have not sucessed.
I would really appreciate if anyone can show me the way. thanks ^^
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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 ... (8 Replies)
Discussion started by: alexcol
8 Replies

2. UNIX for Beginners Questions & Answers

Move the file from one path to another using .sh file in EBS Oracle apps.

Hi All, I just want to move the file from one path to another using .sh file in EBS oracle apps. I have written in .prog but i need in .sh (file.sh) XXC_SAMPLE_FILE.prog #!/bin/bash # XXC_SAMPLE_FILE.prog DATE_TIME=`date | awk {' print $1"_"$2"_"$3"_"$4 '}` echo "parse_parms" ... (4 Replies)
Discussion started by: Mist123
4 Replies

3. Shell Programming and Scripting

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. #!/bin/bash target=/fstest/INVESTIG/Sadiq/TEST_ARCH while read -r line || ]; do mv $line... (19 Replies)
Discussion started by: sadique.manzar
19 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

Remove bad records from file and move them into a file then send those via email

Hi my requirement is that i want pull the bad records from input file and move those records in to a seperate file. that file has to be sent via email.. any suggentions please (1 Reply)
Discussion started by: sxk4999
1 Replies

6. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

7. 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

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

ftp: get list of file to get. Retrieve and move them

Hi, I need to get multiple files from an ftp server. Once the files have been downloaded, I need to move them to a different directory on the ftp server. I don't know of a command that would enable me to get a file and then move it (assuming I don't know the exact file name) within ftp. I think... (10 Replies)
Discussion started by: bbabr
10 Replies

10. Shell Programming and Scripting

File Move Based on 1st Character of File Name

I need some help with a unix script to mv image files to subdirectories based on the 1st character of the filename. See example below... /images/main 1191.jpg 9999.jpg A101.jpg A102.jpg B201.jpg c333.jpg ... Z999.jpg I would like to move to the following: ... (11 Replies)
Discussion started by: srdconsulting
11 Replies
Login or Register to Ask a Question