How to pass * as part of argument string to script?

Karl Auer kauer at biplane.com.au
Thu Aug 26 10:10:19 UTC 2021


On Thu, 2021-08-26 at 10:33 +0200, Bo Berglund wrote:
> FOUND A SOLUTION!

Well done :-)

But I feel you are making life hard for yourself. The trick is to let
the shell do the work.

If you want a script that will remove all but the first n files in the
current directory based on a pattern you provide on the command line,
this will do it:

   #!/bin/sh
   SKIP=$1
   shift
   shift $SKIP
   rm $*

Call it like this:

   ./myscript 5 202*.mp4

Could do with some error checking I guess :-)

The above has a limitation - the number of filenames that will fit into
the maximum length of a command line (around 2 million characters). But
unless you have many thousands of files in your directories, or your
files have extremely long names, you are unlikely to hit that limit in
practice.

Regards, K.

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)
http://www.biplane.com.au/kauer

GPG fingerprint: 61A0 99A9 8823 3A75 871E 5D90 BADB B237 260C 9C58
Old fingerprint: 2561 E9EC D868 E73C 8AF1 49CF EE50 4B1D CCA1 5170







More information about the ubuntu-users mailing list