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
  • 197,895
  • 55
  • 485
  • 740
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
  • Hm, I found my answer is also provides within the link you've shared :) It could be nice to add some explanations of what actually xargs does. Whether the -t option is related to the mv command or it is related to xargs. – pa4080 Jan 16 '18 at 17:19
  • OK, edited to improve the answer, but all the credit is for the original Q&A. – manuti Jan 16 '18 at 23:10
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
  • 29,831
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/
  • 1
    They dont have. They are all number followed by three letter characted, but no spaces for sure! – milan Jan 16 '18 at 18:13
  • 1
    I'd like to add that the path separator / isn't allowed, anyway, in filenames. This and the null byte are the only characters prohibited in Unix filenames. – Stefan Hamcke Jan 16 '18 at 18:24
  • @StefanHamcke, thank you for the correction. It makes sense! (I will fix it) –  Jan 16 '18 at 22:09
  • @StefanHamcke Yeah, that is true; whenever I run the script and the input is a file that has some of these characters It cant be read. – milan Jan 17 '18 at 01:36
  • @milan, the command presented is only applicable to known reasonable content! In hostile environment this is obviously unsafe!! (ex: filelist containing "f ; command" -- comand would be executed) –  Jan 17 '18 at 08:23