3

I have a list of some names/IDs and in the same directory I have original files from that list. I am trying to find how I can move original files to another folder, but only those that appear into list?

So my list/txt file looks like this:

2mtz.pdb
2lbq.pdb
2kad.pdb

and my original files have exactly same ID but I have to find a way how to move only those from the list.

muru
  • 207,228
milan
  • 125

3 Answers3

2

If the list is in the file /tmp/list.txt you can do in the following way:

cat /tmp/list.txt | xargs mv -t /app/dest/

or shorter:

xargs mv -t /app/dest/ < /tmp/list.txt

Is a combination of commands:

mv -t /app/dest/ 

For move all SOURCE arguments into DIRECTORY using the option -t or --target-directory=DIRECTORY

And using the command xargs which execute command lines from standard input, but in this case every line on /tmp/list.txt because we made a redirection using the <

credit to this other Q&A https://unix.stackexchange.com/questions/115734/move-file-by-list-in-file-with-leading-whitespace

manuti
  • 46
  • 6
2

Here are two examples that use while loop.


In the next example, the content of the file called list will be redirected to the while loop, where each line will be read as value of the variable $FILE_NAME, that will be used as an argument in the mv command.

while IFS= read -r FILE_NAME; do mv "$FILE_NAME" "/dest/dir/"; done < /path/to/list

In the next example, the command cat will output the content of the file called list. The output will be piped to the while loop, where each line will be read as value of the variable $FILE_NAME, that will be used as an argument in the mv command.

cat /path/to/list | while IFS= read -r FILE_NAME; do mv "$FILE_NAME" "/dest/dir/"; done

IMO using redirection is the better way. Here is provided more detailed explanation abut this usage of while: While loop only processes the first entry of ssh command.

pa4080
  • 30,621
2

In Unix, if we try hard, filenames may have all sort of unexpected characters. Whenever possible we generally avoid using spaces, '?', '\n', '(', '*', etc in filenames and directories.

If your filenames don't have "spaces" and other strange characters, try:

mv $(cat list/txt) destination/dir/