Linux (mv or cp) specific files from a text list of files?

2,865

Solution 1

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:

rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory

Solution 2

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.

Solution 3

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!

Solution 4

rsync --files-from=file_list.txt /path/to/source/ /path/to/dest/

Rsync has the added benefit over the cp or mv commands because it will automatically create folders if they do not exist.

Solution 5

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:
2,865

Related videos on Youtube

Doug Fir
Author by

Doug Fir

Updated on September 17, 2022

Comments

  • Doug Fir
    Doug Fir almost 2 years

    I have a column with many thousands of rows. The column is meant to be date and is of the format dd/mm/yyyy

    But, when I try to do formulas based on the dates, something is clearly amiss.

    For example, if you try to apply autofilter on the dates, some of them are grouped as a year with the expandable boxes while others appear as their own items.

    For each record I tried a formula to parse it apart.=DATE(RIGHT(A2,4),MID(A2,4,2),LEFT(A2,2))

    That did not help.

    I also selected the column and switched it from general to date format

    I really don't know how to ask the question any clearer. I can tell you that with a date of the format 1/11/2013 when I run =year(right(A1,4)) I get 1903 instead of 2013. When I run =date(right(A1,4),mid(A1,3,2),left(A1,2)) the formula returns 2/10/3192

    • Admin
      Admin over 13 years
      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.
  • Kjetil Joergensen
    Kjetil Joergensen over 13 years
    However; see Ignacio's answer, rsync is sort-of made for this stuff.
  • Jason R
    Jason R about 12 years
    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.
  • Doug Fir
    Doug Fir over 10 years
    Thanks for the info. Can you clarify your sentence: "Btw, if you'd like to get correct format for dates, you can add formula in some empty column (but before set date format for this column): " Does this mean that if I do this I do not need to enter the formula you provided?
  • Dmitry Pavliv
    Dmitry Pavliv over 10 years
    @DougFirr, it's much easier to use my macro. Or you can do following: let your date be in A2:A100. Let your column Z is empty. Select column Z and apply dates format for it. Than write in Z2 formula =A2*1 and drag it down until Z100. Than select column A and apply dates format as well. Copy values from Z2:Z100 and make paste special->paste values in A2:A100. Thats all)
  • Dmitry Pavliv
    Dmitry Pavliv over 10 years
    if you have additional questions, please ask:)
  • Dmitry Pavliv
    Dmitry Pavliv over 10 years
    and yes, there is no need to use this awful formula: =DATE(RIGHT(TEXT(A2,"dd/mm/yyyy"),4),MID(TEXT(A2,"dd/mm/yyyy‌​"),4,2),LEFT(TEXT(A2‌​,"dd/mm/yyyy"),2))
  • Doug Fir
    Doug Fir over 10 years
    Thanks again @simoco. So I tried what you said in your last comment. In column Z I format as date, then write =G2*1 which produces #value. In G2 there is 26/10/13 and it's format is general (I had not yet reached the step where I change it to date format). Am I missing something?
  • Dmitry Pavliv
    Dmitry Pavliv over 10 years
    Does your G2 has some spaces or " ? it shouldn't return #value
  • Dmitry Pavliv
    Dmitry Pavliv over 10 years
    lets try =TRIM(G2)*1. Is it return #VALUE too?
  • Doug Fir
    Doug Fir over 10 years
    Same reult I'm afraid :-(
  • Dmitry Pavliv
    Dmitry Pavliv over 10 years
    ok, try to use =DATE(RIGHT(TEXT(G2,"dd/mm/yyyy"),2),MID(TEXT(G2,"dd/mm/yyyy‌​"),4,2),LEFT(TEXT(G2‌​,"dd/mm/yyyy"),2))
  • Doug Fir
    Doug Fir over 10 years
    Thank you very much it works now! If I interpret it right, we have converted each component of the =date formula to text, then custom formatted each one? How would you word it? Thanks again
  • Dmitry Pavliv
    Dmitry Pavliv over 10 years
    yes, you're right! we can't apply RIGHT(G2,2) directly, because dates in excel are double values and we have to convert it to the text.
  • PseudoNoise
    PseudoNoise about 9 years
    This worked for me because I actually wanted the files from different source paths in the same target directory.
  • Spamwich
    Spamwich almost 9 years
    This solutions strikes me as the most flexible. I ended up using it with rsync instead of cp or mv just because I needed the relative file paths option.
  • ignivs
    ignivs almost 9 years
    could have been while read line; do mv $line /images; done < list.txt as a single command
  • user1182474
    user1182474 over 8 years
    Nice, just I had problems with spaces, so I modified it to: while read -r file; do mv "$file" /path/of/destination ; done < listoffiles
  • MadHatter
    MadHatter over 8 years
    @user1182474 Thanks, and an elegant use of input redirection. The traditional method of showing satisfaction with an answer is to upvote, by the way!
  • Charlie Gorichanaz
    Charlie Gorichanaz almost 8 years
    @user1182474 thank you for that! last step in many modifications had to somewhat blindly make to port my OSX backup utilities to my Android!
  • bzero
    bzero about 7 years
    Why is /path/to/source needed when it takes the paths from the file list?
  • anneb
    anneb over 5 years
    example: rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory
  • Admin
    Admin over 5 years
    cp, mv and the like do not have -t option on bsd's, and the second example with subshell did not work for me (/path/to/move is appended as the last filename)
  • DreadfulWeather
    DreadfulWeather over 5 years
    @bzero maybe list of files can contain relative paths?
  • kristianp
    kristianp almost 5 years
    This is good for a Dockerfile, as tar is available in the ubuntu base image, but not rsync.
  • yahol
    yahol almost 4 years
    If files list contains absolute paths, use / as source directory.
  • Ruslan
    Ruslan over 3 years
    For GNU Tar you can replace the subshell with tar xvf - -C /path/to/new/dir.
  • thouliha
    thouliha about 3 years
    You also must use --no-relative if you don't want to keep the path, and have it be flat.
  • pob
    pob about 3 years
    -a normally implies -r, but not when using the "--files-from" option. For recursive behavior, you need to explicitly add the -r option also, as in "rsync -ar /source/director --files-from=/full/path/to/listfile /destination/directory"