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.
In order to avoid a useless use of
cat
(and if you don't usersync
):This will handle any valid filename, unless it contains a newline, if the files are listed one per line.
rsync has several options that can take a list of files to process(
--files-from
,--include-from
, etc.).For example, this will do the trick:
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!Rsync has the added benefit over the
cp
ormv
commands because it will automatically create folders if they do not exist.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:
Also, you can use
find
command with-exec
option. to copy/move the files.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.
(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
Note - neither of these will work as expected if there happens to be filenames with newlines in them.
I think the rsync answer is a better one, but just for another option:
Try something like:
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.
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:
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/