Leaving the channel between teams

I am using below command to sometimes delete changed files when using hg.

hg status -n | xargs rm

      

I ran into a problem where if the output is

hg status -n

      

contains any file paths with spaces in the file that will not be found. I usually quote or avoid spaces in filenames, but I'm not sure how to do this with pipe-out. Any help would be great thanks :)

+3


source to share


4 answers


Tell both commands to use NUL as a separator:

hg status -n0 | xargs -0 rm

      




Also be careful: the option -n

will print even files that Mercurial doesn't know about.

Maybe you want this instead?

hg status -mn0 | xargs -0 rm

      




Also, don't forget about hg revert

or hg purge

. Perhaps they do what you want, for example.

hg revert --all --no-backup

      

or

.hgrc

[extensions]
hgext.purge=

      

shell

hg purge

      

+4


source


I have not hg

. So I will do it with ls

:



$ touch 'file A' 'file B'

$ ls -1
file A
file B

$ ls | xargs rm
rm: cannot remove `file': No such file or directory
rm: cannot remove `A': No such file or directory
rm: cannot remove `file': No such file or directory
rm: cannot remove `B': No such file or directory

$ ls | tr '\n' '\0' | xargs -0 rm

$ ls

      

+2


source


Let xargs handle this with the option -I

:

hg status -n | xargs -I FileName  rm FileName

      

-I

improves security, but decreases efficiency, since only one filename will be passed to 'rm'

Example:

$ printf "%s\n" one "2 two" "three 3 3" | xargs printf "%s\n"
one
2
two
three
3
3


$ printf "%s\n" one "2 two" "three 3 3" | xargs -I X printf "%s\n" X
one
2 two
three 3 3

      

+1


source


Next to -0

the newer one xargs

has an option -d

that can help you do things like this:

<command returning \n-separated paths> | xargs -d \\n rm -v

      

0


source







All Articles