开发者

Move all but the five latest files to new directory [closed]

It's difficu开发者_如何学运维lt to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

There is an hypotetical process that is creating a series of zip files in volumeA.

The process is a multithreaded process and is writing 5 zip files at a time.

You are running out of space on volumeA, craft a script that will move all but the 5 latest written zip files from volumeA to volumeB


I can't do your homework for you but I can tell you that you use a combination of the ls and head commands to get the list zip files that you need - ie. all but the most recent 5.

See the man pages for ls and head, ie:

# man ls
# man head


This looks a little like homework, but could be fun. Let's see... Tools that might come in handy:

  • ls -t (list, sorted by modification date, newest on top?)
  • sort (?)
  • cp (duh!)

OK, so here is a plan:

  • ls -t | SKIP 5 | xargs cp ...

You will need to figure out how to skip the first 5 lines and continue with the rest. The rest will then be piped to xargs, which will call cp for each input line.

How to implement SKIP? Let's google "linux skip first n lines": http://knol.google.com/k/justin-crites/skip-first-line-of-input-on-gnu-linux/r0h4brikem0y/5#

OK, that was easy:

  • ls -t | sed 1,5d | xargs cp ...

Next: How does that crummy xargs work again? I never really get around to looking at that...

... luckily, we have a whole internet to look through. So, your script might look like this:

ls -t | sed 1,5d | xargs -i 1 mv volumeA/'{}' volumeB/'{}'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜