Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bash script that retain the last 5 copies in AIX like this:

rm `ls -lt /path/the_file_* | tail -n +6`

But I get this error:

rm: Not a recognized flag: w


The ls command return this:

-rw-r--r--    1 user   group1           14 Mar 11 09:54 the_file_20130311.bz2

Any clue on this?

share|improve this question

What you say is essentially

rm -rw-r--r-- 1 user group1 14 Mar 11 09:54 the_file_20130311.bz2

In other words. Remove recursively files named:


If there was a -w flag to rm and the other oddities in -rw-r--r--

A better option could be to use ls -t. Do not worry about it not being on separate lines as ls detect whether it's output is to a tty or not.

Demonstrated by e.g.:

ls -t | cat

But then another problem arises, and that is the fact you are not in the correct directory. So what you say is actually remove files in current directory which is named the same as the 6 last in /path/.

So ensure you are in correct path first, or, another option if you can install the GNU find on your system would be something in the direction of:

find /path/to/files -maxdepth 1 -type f -name 'the_file_*' -printf '%T@ %p\n' |
  sort -nr | head -n6 | cut -f2- -d" "

Note that you need the full path to get correct listing.

Then; if that looks OK add it to a rm routine.

share|improve this answer

What does the ls ... command return? Remember that that command is run, and the output is given to rm as arguments. Nothing makes the output be "file names". If there really should be a -w file in there, perhaps -- ends flags on AIX, and it should be rm -- ...


Oh, brain slip. The ls -l will give not only the file names, but also permissions, e.g. -rw..., and (as explained above) rm takes this as flags. You should do:

rm `ls -t /path/the_file_* | tail -n +6`

And some commands (it's a GNU convention many others have picked up) take -- meaning "flags end here". So to delete a file called -rf you can do rm -- -rf, an in general to not pick up spurious flags, you can say cmd -flags -x -y -- anything goes here.

share|improve this answer
I have new update on what ls is return in my question. I'm not really sure what is -- command? Any sample of usage? – huahsin68 Mar 11 '13 at 2:58
-- is simply an argument that many commands interpret as a signal to stop processing options, so that any further arguments that begin with - are treated as regular arguments, not options. – chepner Mar 11 '13 at 15:01
@vonbrand I don't think -- originated with GNU, and it's mandated by POSIX (with a few exceptions such as echo for historical reasons). – Gilles Mar 11 '13 at 23:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.