Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a directory of many files, something like 50,000 pdf's and other files on a server. I need to move specific ones to another directory. I can generate a list of the files that need to be moved either in csv or any other text format.

What I need to do is run a bash script and move or copy the files that are listed in the text file to another directory.

Is there an easy way of doing this? Any suggestions or resources would be greatly appreciated.

share|improve this question
    
Also, the files are named randomly when they are added to the directory. There is no naming convention or any logic that could be used to move specific files. –  Jestep Dec 14 '10 at 14:09

9 Answers 9

up vote 9 down vote accepted

rsync has several options that can take a list of files to process(--files-from, --include-from, etc.).

share|improve this answer
    
Specifically, the following worked for me: –  John Mark Mitchell Jul 26 '12 at 21:38

The following worked for me where I needed to copy all the PNG files from a specific path (and all subdirs) to a new location while preserving the directory structure:

rsync -av --prune-empty-dirs --include='*/' --include='*.png' --exclude='*' source/ destination/

Because RSYNC creates the mirror of the directory structure first, then syncs the files, you can end up with excess folders that are empty. I used the --prune-empty-dirs flag to remove these empty directories.

I have no affiliation, but thought it right to give credit to the source that inspired this solution: http://techblog.zabuchy.net/2011/transfer-only-selected-file-types-with-rsync/

share|improve this answer

Try something like:

cat list.txt | while read line; do mv "$line" /images; done
share|improve this answer

I accidentally copied the full contents of a directory into a destination directory instead of moving the full directory. This resulted in a cluttered destination directory instead of the origin directory being added to the directory.

To fix this, I did the following:

ls -rt /path/to/cluttered/destination/directory/ > /opt/dircheck/filestomove The above command creates the filestomove file that will be a list of all contents of the destination directory, reverse sorted by time, meaning oldest to newest.

Then I created a sub-directory of the now cluttered destination directory to move the stuff into.

mkdir /path/to/cluttered/destination/directory/newsubdirectory

Then I repeated the directory listing, except listing to screen and showing more details.

ls -lrht /path/to/cluttered/destination/directory/ This line lists the directory, sorted by date ascending (reverse sort by time) and shows more information, including the date/time stamp of each file in the now cluttered destination directory. I refer to this, starting at the top to show what directories and files I want to keep where they were. There will be a gap in the date/time stamp of the files where all the new files start that shouldn't be there.

Then I edited the filestomove file created in the first step above (that is sorted by date) and deleted the few from the list that were there previously that I want to stay from the original directory.

vim /opt/dircheck/filestomove Delete from the top, all files you don't want to move.

Then I used the command listed previously in this post to move the files in my list to the new directory I made.

sudo xargs -a /opt/dircheck/filestomove mv -t /path/to/cluttered/destination/directory/newsubdirectory

This moved all the files in a split second. (Note: you may not need the sudo at the beginning, this is a matter of file permissions).

Now my original destination directory is nice and clean and contains a new directory with all the files and directories that were cluttering it.

share|improve this answer
for file in `cat listoffiles`; do mv "$file" /path/of/destination ; done

assuming bash, plus listoffiles containing one file per line. My only objection to the rsync route is that the OP asks for a method of moving the files, not copying them. rsync has more flags than a royal wedding, so I'm sure it can be further modified to do that, but sometimes simpler is better!

share|improve this answer

In order to avoid a useless use of cat (and if you don't use rsync):

xargs -a file_list.txt mv -t /path/to/dest

This will handle any valid filename, unless it contains a newline, if the files are listed one per line.

share|improve this answer
2  
One disadvantage of this approach is that if the files in the list contain path names (i.e. they are not all in the same directory), the use of mv will collapse all of them into the same destination directory. The rsync approach in the accepted answer doesn't suffer from this limitation. –  Jason R May 4 '12 at 16:10
    
Also, I tried this command and xargs would break on any files that contained a single quote ("unmatched single quote") –  James Beninger Aug 17 '12 at 1:30

This depends on the format of the text file you have. For example, if you have the list of files written such that Each file is located on a new line. You can use xargs like:

$ cat your_text_file | xargs cp -t /path/to/destination

Also, you can use find command with -exec option. to copy/move the files.

share|improve this answer

I think the rsync answer is a better one, but just for another option:

tar -cf - -T FILE_OF_FILENAMES_TO_MOVE.txt |(cd /path/to/new/dir && tar -xvf -)
share|improve this answer

If (and only if), you don't have any nasty characters in the filenames (spaces, newlines, which would confuse xargs on how to break things into individual arguments), and you generate a list of files separated by newlines (one file per line), you could do something like.

cat filenames.txt | xargs mv -t /path/to/move/files/to

(In general, see man xargs, it's awesome)

If your particular mv doesn't have the -t option, you could also do some trickery like

( cat filenames.txt; echo /path/to/move/files/to ) | xargs mv
share|improve this answer
    
However; see Ignacio's answer, rsync is sort-of made for this stuff. –  Kjetil Joergensen Dec 14 '10 at 14:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.