[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: "cut" command not working as expected



Thus spake David selby:
> I need to get the first two file names from a directory ...
> My code
> 
> directory=$(ls -r --format=single-column)
> 
> works perfect and gives me ...
> 
> 20030617Jun17.tar.gz 20030616Jun16.tar.gz 20030615Jun15.tar.gz 
> 20030301Mar01.tar.gz 20030222Feb22.tar.gz 20030215Feb15.tar.gz 
> 20030208Feb08.tar.gz 20030205Feb05.tar.gz
> 
> I want to cut the first two file names from the list ... To my way of 
> thinking this should be easy ...
> 
> cut -d' ' -f2 $directory

The problem is that when you do that, cut is interpreting the contents
of $directory to be a list of files to operate on.  This means that cut
will actually *perform the cut* on say, the contents of
20030205Feb05.tar.gz

files=$(/bin/ls -1 | tail -2)

Will leave $files containing the last 2 filenames.  It's probably best
to use it in some sort of loop,

for file in $(/bin/ls -1 | tail -2); do
  # do something with each file here
done


-- 
Nathan Poznick <poznick@conwaycorp.net>

"This is wild! I've never killed a guy like *this* before. Neat!" -Joel
(as Hercules). #410

Attachment: pgpgds5wnfT6j.pgp
Description: PGP signature


Reply to: