Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I 'm using the following script to merge few pcap files into one using mergecap command. But when I run it it gives me a 'basename: extra operand /mnt/md0/capture/DCN/dcn_2014_02_04_00_11_47_598.pcap' error The script is as follows

#find last 15 files older than +5 days
FILES=$(find /mnt/md0/capture/DCN/ -maxdepth 1 -type f -name "*.pcap"  -mtime +5 -print0 | xargs -0 ls -lt | tail -15 | awk '{print $8}')

TAG1=$(basename ${FILES[0]} | sed 's/.pcap//')
TAG2=$(basename ${FILES[$N-1]} | sed 's/.pcap//')
#merge the files
mergecap -w /mnt/md0/capture/DCN/"${TAG1}_to_${TAG2}".pcap ${FILES[@]}
sudo chmod +rw /mnt/md0/capture/DCN/"${TAG1}_to_${TAG2}".pcap 
#delete originals
sudo rm  ${FILES[@]}
share|improve this question
Shouldn't it be awk '{print $9}'? – Abdul Feb 5 '14 at 8:43
@Abdul yes I thought so, but it seems like in this system $8 is giving me the file names. I tested it manually to be sure – Jishnu U Nair Feb 5 '14 at 8:45
tag1=$(basename "${FILES[0]}" .pcap. Remember that leaving a variable unquoted is the split+glob operator in shells. – Stéphane Chazelas Feb 5 '14 at 9:47

1 Answer 1

up vote 3 down vote accepted

FILES=$(command) is going to set a variable named FILES to a scalar that contains the output of command.

${FILES[0]} is going to be the contents of that scalar variable, in your case, a string containing 15 blank-delimited filenames, which will then be broken up into 15 fields.

It looks like you want FILES to be an array; for that, use FILES=( $(command) ) .

It's also good practice to surround shell variable accesses with double quotes - "${FILES[0]}" instead of ${FILES[0]} - so that the shell doesn't break the contents into fields if there's any whitespace in them.

share|improve this answer
Note that FILES=($(command)) is only fine when the file names don't contain whitespace or \[?*. – Gilles Feb 5 '14 at 23:31

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.