00101010

My blog on life the universe and everything…

How to delete multiple files from an svn repository with one line

With this post I hope to start a new habit on this blog. Every time I find a cool and useful one-liner or a nice script or if I write one myself and feel it’s good enough for sharing, I want to make a quick blog post about it. So here’s the first one.

If you use svn a lot on your everyday work you might end up with a situation like this. You have a directory – let’s call it “images”- where you store your.. well images🙂

Now say you remove from that directory 5 obsolete images. Considering that the ones you delete from that folder have been at some point in the past commited to the svn repository then if you go on a terminal and type

‘svn st’ , among other things you will see something like this

!       /images/image1.png
!       /images/image2.png
!       /images/image3.png
!       /images/image4.png
!       /images/image5.png

now you need to issue a command like this “svn delete images/image1.png” for evey image in that list so that the svn repository deletes those images from the server. You could copy paste the names of those images after the ‘svn delete’ command seperated by a comma to do this in one go, but that would be tiresome especially if you have deleted hundreds of files at once. Copy pasting is just not gonna cut it for you then. So here’s an easy way of doing this with one line on any unix terminal

> svn st | grep ^! | awk ‘{print $2}’ |xargs svn delete

so what does this do? Let’s break it down and see

the first part [ svn st ] prints the status of the svn status of your working directory.

the second part [ grep ^!] filters the contents of the first part (notice the | in between? that’s a pipe which turns the output of one command to input for another) and only prints the lines that start with an exclamation mark. The ^ in regular expression terms means –starts with-. So ^! means starts with an exclamantion mark. This will essentially print your deleted files only and not all the other svn status messages that might be visible if they weren’t filtered out.So you would see a list like this.

!       /images/image1.png
!       /images/image2.png

…[more]

the third part [awk ‘{print $2’}] prints the second column of the output of grep (our previous part). In the listing above there are two columns, the excalamation mark and the file path ( !     /images/image1.png]). the $2 makes sure awk parses the lines and only prints the second column on your terminal.

finally the fourth part [xargs svn delete] passes the result of the previous command (i.e. the paths for the files) as arguments to the [svn delete] command.

After you run the above one-liner, typing “svn st” on the terminal now will, among other things,  print a list of your deleted files like this:

D      /images/image1.png
D      /images/image2.png
D      /images/image3.png
D      /images/image4.png
D      /images/image5.png

notice that the exclamation mark has now changed to a D. Now if you commit to the svn repository the delete commad will be sent for these files and they will be removed from the server.

One tip. The above one liner can be executed in steps for you to see exactly what is happening. So you can run the first step alone, see what happens then add the second part to it and see what happens then the third and then the fourth.Remember to always separate them using the pipe character |.  Actually it’s a good idea to run this one liner without the fourth part once to make sure you see all the files that you intend to delete before passing them as arguments to [xargs svn delete].

That’s it. I hope someone will find this as useful as I found it when I first discovered it.

 

 

5 responses to “How to delete multiple files from an svn repository with one line

  1. catalternative July 29, 2011 at 3:27 pm

    Awk is really impressive, but requires good knowledge of regural expressions😀

    Nice post🙂

  2. serk01 July 29, 2011 at 3:34 pm

    @catalternative Thanks.

    Regarding regular expressions I think Jamie Zawinski (software developer for the Netscape Navigator browser among other cool things) has said all there is to be said🙂

    “Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems. “

  3. Boyce Stroder July 29, 2011 at 9:09 pm

    Nice post and bookmarked your blog, great!

  4. Anastasia March 30, 2016 at 10:55 am

    You can do it with Dupliacte Files Deleter. It works really well

ξεφόρτωσε εδω

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: