[Centos] help with xargs and mv

Dag Wieers dag at wieers.com
Tue Feb 8 22:14:55 UTC 2005


On Tue, 8 Feb 2005, James B. Byrne wrote:

> I am trying to move a group of sendmail queue files into a special 
> area and am developing s script to assist.  The manual steps are:
> 
> # Identify which messages to move
> mailq -qR<domain> > file1
> 
> # Select only lines with message ID strings
> grep '^[[:alpha:]][[:alnum:]]\{13\}' file1 > file2
> 
> # extract only the messages ID
> cut -b -14 file2 > file3
> 
> # prepend '*' to message IDs
> sed "s/^/\*/" file3 > file4
> 
> All of this works the way that I expect.  What I now want to do is 
> to mv all of the related files listed in file4 in the form 
> "*messageid" to another directory.  Using xargs I expected 
> (naively) that the following construction would work:
> 
> cat file4 | 	xargs mv /var/spool/mqueue/'{}' \
> 			/var/spool/mqueue/offline 
> 
> (note that in the original this is all one line.)
> 
> However, when I do this I get the error:
> mv: when moving multiple files, last argument must be a directory
> Try `mv --help' for more information.
> 
> There is obviously something about xargs that I do not understand.  
> In my imagination I see this xargs construction expanding to this:
> 
> mv /var/spool/mqueue/*messageid1 /var/spool/mqueue/offline
> mv /var/spool/mqueue/*messageid2 /var/spool/mqueue/offline
> .
> .
> .
> mv /var/spool/mqueue/*messageidn /var/spool/mqueue/offline
> 
> so that the qf and df files for each message are moved into the 
> subdirectory offline.  But this is obviously incorrect.  Can anyone 
> here point out to me what my misunderstanding is and how to get 
> this to work?  If this is not the forum for this kind of question 
> then can someone with more experience point me to a mailing list 
> that would be more suitable?

Compare this:

	echo -e "file1\nfile2\nfile3" | xargs echo '{}' blah

with:

	echo -e "file1\nfile2\nfile3" | xargs -i echo '{}' blah

What you require is -i to make '{}' work. xargs by default appends the 
input as a list of arguments. The manpage says:

       --replace[=replace-str], -I replace-str, -i[replace-str]
              Replace occurences of replace-str in the initial  arguments  with  names
              read  from standard input.  Also, unquoted blanks do not terminate argu-
              ments.  If replace-str is omitted, it defaults to "{}" (like  for  'find
              -exec').  Implies -x and -L 1.

Kind regards,
--   dag wieers,  dag at wieers.com,  http://dag.wieers.com/   --
[all I want is a warm bed and a kind word and unlimited power]



More information about the CentOS mailing list