0

I wrote the following function in my .bashrc for use instead of rm * :

function re() { cp . -R /home/glset/ritwik/trash/ rm * } 

It works .. but it is very slow. Even for small files it takes too long to execute. It is always faster to just fire the two commands one after the other which defeats the purpose of the function in the first place. Am I doing something wrong here ? What makes it take longer ?

8
  • 1
    There is a minor strangeness there: you cp recursively, but not rm recursively. Are you sure that is what you want? Commented Jun 3, 2013 at 6:51
  • 1
    What he really wanted to do was cp * /path/to/trash/ Commented Jun 3, 2013 at 6:57
  • 2
    Any particular reason to copy the files and then remove them in stead of removing them. You could get into trouble doing things this way if this is done on the same filesystem and you are running low on disc-space. (It would be faster in that case as well, but it doesn't explain why your function is slow). Commented Jun 3, 2013 at 7:01
  • 2
    Still one more vote for mv instead. Commented Jun 3, 2013 at 7:06
  • 3
    You can check if the trash command from the trash-cli package might be useful for you. Commented Jun 3, 2013 at 8:43

1 Answer 1

3

Running the function or running the two commands separately does not make any measurable difference in execution time.

My guess would be that you ran the function, found that it was slow, then tried the commands separately.

If you did that, then it's likely that the first time, the data was not present in the disk cache, so what you measured was the time to load the data from the disk. The second time, the data was already in the disk cache (because you had just accessed it), so you measured the time to copy the data around in memory.

To do proper benchmarks, you need to start all measurements in the same conditions: either you know that all the data is in the cache, or you know that all the data has to be loaded from the disk.


Copying the data and then removing the original is a really weird way of doing this. Run mv instead! It's instantaneous if you've moving the files inside the same filesystem.

Furthermore, rm * is a very unusual command. If you don't need a directory anymore, remove the directory: rm -r somedir or mv somedir ~/trash/.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.